Class ServerChannel

java.lang.Object
org.spiderwiz.core.ServerChannel
All Implemented Interfaces:
Runnable

public abstract class ServerChannel
extends Object
implements Runnable
This is a base class for implementations of server channels. A server channel waits for requests arriving over the network from implementations of Channel. Based on the request, it creates the server side of the channel and opens it for communication.

Spiderwiz framework comes out of the box with implementations of predefined servers, such as a TCP/IP server and a WebSockets server. The type of the server to use is configured in the application's configuration file. These are all implemented by extending the ServerChannel class.

In addition to the predefined servers users can write plugins with their own custom server implementations. You develop a custom server by extending ServerChannel, and use your implementation class by assigning its fully qualified name to the class tag in the configuration file.

All implementations of this class shall implement its configure() method to configure the server, open() to activate its mechanism, accept() to listen to and establish connections and close() to close the server when its not needed any more. Also implement getGateID() to identify the server for logging and administration purposes.

Here is a full example taken from Spiderwiz implementation of a TCP/IP server channel:

public class TcpServerSocket extends ServerChannel {
    private ServerSocket serverSocket = null;
    private int port;

    @Override
    protected boolean configure(Map<String, String> configParams, int type, int n) {
        final String NO_TCP_PARAMS = "No port number specified for %1$s-%2$d";
        final String TYPES[] = {"import", "producer", "consumer"};
        port = ZUtilities.parseInt("port");
        if (port == 0)
            Main.getInstance().sendExceptionMail(null, String.format(NO_TCP_PARAMS, TYPES[type], n), null, true);
        return port > 0;
    }
    
    @Override
    public String getGateID() {
        return "port:" + port;
    }

    @Override
    protected void open() throws IOException {
        serverSocket = new ServerSocket(port);
    }

    @Override
    protected Channel accept() throws IOException {
        if (serverSocket == null)
            return null;
        Socket socket = serverSocket.accept();
        if (socket == null)
            return null;
        TcpSocket channel = new TcpSocket();
        channel.setSocket(socket);
        return channel;
    }

    @Override
    protected void close() throws IOException {
        if (serverSocket != null)
            serverSocket.close();
    }
}
See Also:
Channel
  • Constructor Details

  • Method Details

    • configure

      protected abstract boolean configure​(Map<String,​String> configParams, int type, int n)
      Configures the server.

      An abstract method that you must implement to configure a server channel from parameters specified by either [producer server-n] or [consumer server-n] properties in the application's configuration file. The values of these properties must be a list of pairs key=value concatenated by a semicolon. For instance, let's say you implement a custom TCP/IP server socket, then its configuration may look something like:

           class=com.mydomain.myplugins.MyTcpIpServer;port=31415
           
      The framework will then instantiate your class com.mydomain.myplugins.MyTcpIpServer and call its configure() method with a map in configParams containing the following pairs (order not guarantee):
           class=com.mydomain.myplugins.MyTcpIpServer
           port=31415
           
      Parameters:
      configParams - a map of key=value configuration parameters.
      type - type of server - 1:producer, 2:consumer.
      n - the n value of producer server-n or consumer server-n.
      Returns:
      true if and only if configuration is successful.
    • open

      protected abstract void open() throws Exception
      Opens the server for arriving connection requests.

      An abstract method that you must implement to start listening to clients.

      Throws:
      Exception
    • accept

      protected abstract Channel accept() throws Exception
      Listens for a connection to be made to this server and accepts it.

      An abstract method that you must implement to listen for a connection to be made to this server and accept it. Your implementation should block until a connection is made. When it is, you need to create an instance of your Channel implementation that relates to this server and return it.

      Returns:
      a Channel object that represents the established connection, or null if this could not be done.
      Throws:
      Exception
    • close

      protected abstract void close() throws Exception
      Closes the server.

      An abstract method that you must implement to to stop listening to clients and close the server.

      Throws:
      Exception
    • getGateID

      public abstract String getGateID()
      Returns the identification of this server.

      An abstract method that you must implement to return server identification, used for logging and administration purposes.

      Returns:
      the identification of this server.
    • run

      public final void run()
      Implementation of Runnable.run().

      Used internally by the framework. Do not call or override.

      Specified by:
      run in interface Runnable
    • cleanup

      public void cleanup()
      Cleans up object resources on exit.

      You may override this method if you need to do some cleanup tasks when the object is not used any more. Do not forget to call super.cleanup().