datum API Documentation

datum.protocol
Class Server

java.lang.Object
  |
  +--datum.protocol.Server
All Implemented Interfaces:
Receiver, Runnable

public final class Server
extends Object
implements Runnable, Receiver

A datum.protocol server that supports unlimited Connection's, as well as basic multicasting. To use the Server class, call its constructor, passing it a class that implemented the Receiver interface. The Server will immediately be listening for connections from datum.protocol clients. The Receiver interface class will be sent events coming from all of the connected clients. The sendAll methods can be used for basic multicasting purposes. Limits such as maximum number of clients, and read-only clients need to be implemented by the application using the Server class. You can get references to all the individual clients connected to the Server by saving the connection parameter on the connected() method of Receiver.

Example:

 import datum.protocol.*;
 import java.io.*;
 import java.util.*;

 public class ServerTest implements Receiver {
 	Server s;

 	public ServerTest() {
 		String[][] definitions = {{"TypeA", "FieldA1", "FieldA2"},
 				{"TypeB", "FieldB1", "FieldB2"}};
 		try {s = new Server(definitions, this);}
 		catch(IOException e) {System.out.println(e);}
 	}

 	public void error(Connection connection, String errorProtocol) {}

 	public void connected(Connection connection) {
 		Object[] message = {"TypeA", "Hello there!", new Integer(s.getNumClients())};
 		connection.send(message);
 	}

 	public void receivedData(Connection connection, Hashtable data) {}

 	public void disconnected(Connection connection) {}
 }

See Also:
Connection, Receiver, sendAll(java.lang.Object[])

Constructor Summary
Server(OutputStream outputStream)
          Creates a new datum.protocol server that writes messages sent to the sendAll method to the given OutputStream.
Server(Receiver receiver)
          Creates a new datum.protocol server that listens on the default port.
 
Method Summary
 void connected(Connection connection)
          Do not use.
 void disconnectAll()
          Disconnects all clients from the Server.
 void disconnected(Connection connection)
          Do not use.
 void error(Connection connection, String errorProtocol)
          Do not use.
 int getNumClients()
          Returns the number of clients currently connected to the Server.
 void receivedData(Connection connection, Hashtable data)
          Do not use.
 void run()
          Do not use.
 void sendAll(Object[] message)
          Sends a message built from an array of fields to all the clients connected to the server.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Server

public Server(Receiver receiver)
       throws IOException
Creates a new datum.protocol server that listens on the default port. The server immediately starts listening when the constructor is called.

Parameters:
definitions - An array of message field name arrays. Each array representing a message definition should be an array of field names, the first field should be the message type.
receiver - The class that will receive events from clients which connect to the server.
Throws:
IOException - If a server socket can not be opened.

Server

public Server(OutputStream outputStream)
Creates a new datum.protocol server that writes messages sent to the sendAll method to the given OutputStream.

Parameters:
definitions - An array of message field name arrays. Each array representing a message definition should be an array of field names, the first field should be the message type.
outputStream - A output stream that will be written to by sendAll()
See Also:
sendAll(java.lang.Object[])
Method Detail

run

public void run()
Do not use.
Specified by:
run in interface Runnable

getNumClients

public int getNumClients()
Returns the number of clients currently connected to the Server.

Returns:
the number of clients

sendAll

public void sendAll(Object[] message)
Sends a message built from an array of fields to all the clients connected to the server. The toString() method is used to convert Object to String before sending.

Parameters:
message - An array of fields which are delimated and concatenated into a single message. The first field should be the message name.

disconnectAll

public void disconnectAll()
Disconnects all clients from the Server. The Server must be really pissed to do this.

error

public void error(Connection connection,
                  String errorProtocol)
Do not use.
Specified by:
error in interface Receiver
Following copied from interface: datum.protocol.Receiver
Parameters:
connection - The connection which this error is coming from.
errorMessage - A description of the error that occured.
See Also:
Receiver.disconnected(Connection)

connected

public void connected(Connection connection)
Do not use.
Specified by:
connected in interface Receiver
Following copied from interface: datum.protocol.Receiver
Parameters:
connection - The connection which has just been established.
See Also:
Connection.send(java.lang.Object[])

receivedData

public void receivedData(Connection connection,
                         Hashtable data)
Do not use.
Specified by:
receivedData in interface Receiver
Following copied from interface: datum.protocol.Receiver
Parameters:
connection - The connection which received a message.
data - The Hashtable which contains field name-value pairs from the message.
See Also:
Connection.getLibrary()

disconnected

public void disconnected(Connection connection)
Do not use.
Specified by:
disconnected in interface Receiver
Following copied from interface: datum.protocol.Receiver
Parameters:
connection - The connection which has just been dropped.

Up to datum