Re: A real jini service
Gregg Wonderly <[email protected]>
| Newsgroups | gmane.comp.java.sun.jini |
|---|---|
| Message-ID | <[email protected]> |
Mark Brouwer wrote:
> While I believe there is no formal definition of what represents a "Jini
> service" I would say that if your chat service is specified by a (Java)
> interface (representing the chat API) and that some service code
> implements that interface and the service is able to find a lookup
> service and register the code with the lookup service you have a Jini
> service. It doesn't matter whether you implement the communication
> protocol using RMI or plain sockets, or something else for that matter.
For example, the service interface might be something like
public interface ChatService extends Remote {
public ChatConnection connect( UserID id ) throws RemoteException;
}
and ChatConnection would then wrap up the logic that you need for connecting and
running the session as in
public class ChatConnection implements Serializable, Runnable {
private String host;
private int port;
private Uuid sessionId;
private volatile boolean done;
public ChatConnection( String host, int port, Uuid id ) {
this.host = host;
this.port = port;
this.sessioinId = id;
}
public void addSessionListener( SessionListener lis ) {
// add more listeners
}
public void connect( SessionListener lis ) throws IOException {
if( s != null ) {
throw new IOException( "Already Connected" );
}
s = new Socket( host, port );
addSessionListener( lis );
login();
new Thread(this).start();
}
private void login() {
// send sessionId to authenticate
}
public void run() {
while( !done ) {
try {
// receive events on socket
// and send to listeners
} catch( IOException ex ) {
// log exception
// send event to listeners
// maybe try reconnect
// maybe just break;
}
}
// close socket and set s to null
}
... other needed methods ...
}
Gregg Wonderly
--------------------------------------------------------------------------
Getting Started: http://www.jini.org/wiki/Category:Getting_Started
Community Web Site: http://jini.org
jini-users Archive: http://archives.java.sun.com/archives/jini-users.html
Unsubscribing: email "signoff JINI-USERS" to [email protected]