Showing posts with label Server Side Extensions. Show all posts
Showing posts with label Server Side Extensions. Show all posts

Thursday, 25 November 2010

Linking SmartFoxServer and MySQL

How to get a zone set up to use a specific database is actually rather well covered in the documentation. Here's my set up after doing that:
The important bits to change are the connection string, which will need the right port for the MySQL instance that you want, and the database name (test_db in the example above). The username and password are for the database. I have used the oh-so-insecure root with no password, but normally you would want to create a specific user with a strong password who only has access to that database.

The test SQL is used by the server when it sets up the zone to check that the server connection is good. If your test SQL is broken it will refuse to connect, but it doesn't really matter what the result of your test is. Short and sweet might be good! It will need to be specific for each database.

The code side of talking to the database is more convoluted and we will probably end up wrapping some of our common actions. It is dealt with in the server-side code. It relies on us having access to the Zone object,  which has a DBManager property that can access the database using the connection settings above.

This is an example of the code that could be used in a request handler class that extends the BaseClientRequestHandler:

    SFSExtension parentExtension = this.getParentExtension();
    Zone parentZone = parentExtension.getParentZone();
    IDBManager dbManager = parentZone.getDBManager();
    try {
        dbManager.executeUpdate("INSERT  INTO log VALUES ('"+sender.getName()+"',     
        'input two numbers to add')");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
That will successfully write data to the table in the database linked to in the zone manager when that request handler is fired. 

Thursday, 18 November 2010

Setting up SmartFox server-side extensions

SmartFoxServer provides a client API (we'll be using the AS3 API) and a set of classes to create server-side extensions in Java. The server is set up with a series of zones, each of which acts as a separate application. A client is configured to connect to a particular zone by name. The details of which IP address, port and which zone are all kept in an XML file, and read by the client code the first time the client is asked to connect to the server. 

The diagram below is a really crude picture of what happens when the client sends a request to the server. 


The client packages up the name of the request it's trying to call, and the parameters needed for that call. Then it effectively lobs them over to the server in a bundle, along with the information about which zone it is supposed to be talking to. The server then uses the zone set up information to call the extension, which is set up with a list of RequestHandler classes. It uses the name request name passed through from the client to work out which RequestHandler class should be called, and passes the parameter data through to that class. 

That request handler can then use as much or as little extra backend code as you like, and optionally passes a response back to the client. The client can be a single user object (which can either be the generic SFS user object or a custom one that extends that object), or a list of users to alert. The client needs to use the events supplied with the client API to be notified of any responses, as the connection is asynchronous. 

Setting up the extension for the zone is pretty straightforward. Using the inbuilt Zone Configurator (is that really a word?), go to the tab labelled 'Zone extension'. The name field contains the folder that the extension jar is in within the extensions folder on the server. The type field defaults to Java, which is what we're using. And the file is the class within your jar (package included) that extends the SFSExtension class.

I haven't tried adding Room Extensions yet, but I suspect that will work along the same lines. 


There are other features that might become handy, such as multi-handlers which would allow us to break our code down into smaller chunks - ie. a list of requests for each area in a separate handler, rather than a long list of request handlers for everything in the first class. If/when we use them I'll revisit!

Thursday, 11 November 2010

SmartFoxServer 2X Server Side Extensions - Eclipse Setup

To create server side extensions for SFS 2X several libraries must be added to the project build path. The main archive files required are sfs2x.jar and sfs2x-core.jar. If logging features are needed log4j-1.2.15.jar, slf4j-api-1.5.10.jar and slf4j-log4j12.jar should also be added as external jars. All files are located under the {install-dir}/SFS2X/lib folder.

When writing java files you'll need to include various import statements such as:

import com.smartfoxserver.v2.extensions.SFSExtension;
import com.smartfoxserver.v2.entities.*;
import com.smartfoxserver.v2.core.*;


and the like. Also add "extends SFSExtension" to the declaration of classes that will call the API methods and classes.

http://docs2x.smartfoxserver.com/AdvancedTopics/server-side-extensions