ewe.net
Class ClientServerConnector

java.lang.Object
  extended byewe.sys.TaskObject
      extended byewe.net.ClientServerConnector
All Implemented Interfaces:
Runnable, Task

public abstract class ClientServerConnector
extends TaskObject

This can be used to easily create a Client/Server connection over TCP/IP or through the EweSync RemoteConnection.


Field Summary
 boolean acceptOnlyOneConnection
          If this is set true then after the initial connection, no more connections will be accepted and the server socket will be closed.
protected  Handle connecting
           
 boolean dontShowCancel
          Set this true if you don't want to show the cancel box.
 String hostOrService
           
static int INFRA_RED
          This is a type to be used in the constructor.
 boolean isClient
          This will be set if a connection is made and this connector is considered the client.
 boolean isServer
          This will be set if a connection is made and this connector is considered the server.
 Frame parentFrame
          You can optionanlly set this.
 int port
           
static int REMOTE_SERVICE
          This is a type to be used in the constructor.
static int REMOTE_TCP_SOCKET
          This is a type to be used in the constructor.
 ServerSocket server
          This is the server that will be listened to after the initial connection.
 boolean showSelectionScreen
          Set this true to let the user change the connection settings.
protected  SocketMaker socketMaker
          This will be created during the connection setup.
static int TCP_SOCKET
          This is a type to be used in the constructor.
 int timeoutInSeconds
          This defaults to 30 seconds.
 int type
           
 
Fields inherited from class ewe.sys.TaskObject
handle, interruptThreadToStop, myThread, napIterations, napTime, shouldStop
 
Constructor Summary
ClientServerConnector(boolean amServer, int port)
          This is for Infra-Red connections only - it creates a client-server connection over the infra-red port where this entity will act as either the server or client as specified by the amServer parameter.
ClientServerConnector(boolean amServer, int type, String hostOrService, int port)
          This creates a client-server connection where this entity will act as either the server or client as specified by the amServer parameter.
ClientServerConnector(int port)
          This is for Infra-Red connections only - it creates a negotiated client-server connection over the infra-red port.
ClientServerConnector(int type, String hostOrService, int port)
          Create a negotiated client-server connection between two initially peer entities.
 
Method Summary
protected abstract  void client(Socket s)
          This gets called on the first connection and if it is determined that I am the client.
 Handle connect()
          Start the connection process and immediately return a Handle to indicate its progress.
 Socket connectToServer()
          This is used to make another connection to the server process on the same server port where the initial connection was made.
protected  void doRun()
          Override this method if you want to use an mThread to run your Task.
protected  void doStop(int reason)
          This method, by default, sets the shouldStop variable to true.
protected abstract  void server(Socket client, boolean firstConnection)
          This gets called every time the client connects - including the initial connection.
 void stopServer()
          If the connector is waiting for extra client connections this will close the server socket.
 boolean waitOnConnection()
          Start the connecton and wait until the connection is made.
 
Methods inherited from class ewe.sys.TaskObject
checkAbortFail, checkFailure, doStart, exec, getHandle, nap, run, setNapTime, show, sleep, start, startTask, stopTask, waitOn, waitOnAny, waitOnSuccess, yield, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

TCP_SOCKET

public static final int TCP_SOCKET
This is a type to be used in the constructor.

See Also:
Constant Field Values

REMOTE_TCP_SOCKET

public static final int REMOTE_TCP_SOCKET
This is a type to be used in the constructor.

See Also:
Constant Field Values

INFRA_RED

public static final int INFRA_RED
This is a type to be used in the constructor.

See Also:
Constant Field Values

REMOTE_SERVICE

public static final int REMOTE_SERVICE
This is a type to be used in the constructor.

See Also:
Constant Field Values

type

public int type

hostOrService

public String hostOrService

port

public int port

showSelectionScreen

public boolean showSelectionScreen
Set this true to let the user change the connection settings.


timeoutInSeconds

public int timeoutInSeconds
This defaults to 30 seconds.


dontShowCancel

public boolean dontShowCancel
Set this true if you don't want to show the cancel box.


parentFrame

public Frame parentFrame
You can optionanlly set this. If it is false then the cancel box will be displayed in its own window.


acceptOnlyOneConnection

public boolean acceptOnlyOneConnection
If this is set true then after the initial connection, no more connections will be accepted and the server socket will be closed.


server

public ServerSocket server
This is the server that will be listened to after the initial connection. This will only be valid if this entity is the server.


connecting

protected Handle connecting

isClient

public boolean isClient
This will be set if a connection is made and this connector is considered the client.


isServer

public boolean isServer
This will be set if a connection is made and this connector is considered the server.


socketMaker

protected SocketMaker socketMaker
This will be created during the connection setup.

Constructor Detail

ClientServerConnector

public ClientServerConnector(int type,
                             String hostOrService,
                             int port)
Create a negotiated client-server connection between two initially peer entities.

This type of connection will be initiated at both entities and a negotiation will be done when both connect to decide which will act as the server and which as the client. This is not recommended on all platforms since the TCP/IP implementation on some (e.g. Win98) is flawed and this may not always work. The type of connection where one is definitely the client and the other the server is usually better.

Note that doing this over the REMOTE_SERVICE will usually work correctly since in this case the desktop will always be the server and the mobile device will always be the client.

Parameters:
type - either TCP_SOCKET, REMOTE_TCP_SOCKET, REMOTE_SERVICE or INFRA_RED
hostOrService - for TCP_SOCKET or REMOTE_TCP_SOCKET it should be the host name, for REMOTE_SERVICE (over the EweSync connection) it should be the service name, and for INFRA_RED it should be null.
port - The port number to use. This should not be 0.

ClientServerConnector

public ClientServerConnector(int port)
This is for Infra-Red connections only - it creates a negotiated client-server connection over the infra-red port.


ClientServerConnector

public ClientServerConnector(boolean amServer,
                             int type,
                             String hostOrService,
                             int port)
This creates a client-server connection where this entity will act as either the server or client as specified by the amServer parameter.

Parameters:
amServer - if this is true then this entity is the server, otherwise it is the client.
type - either TCP_SOCKET, REMOTE_TCP_SOCKET, REMOTE_SERVICE or INFRA_RED
hostOrService - for TCP_SOCKET or REMOTE_TCP_SOCKET it should be the host name, for REMOTE_SERVICE (over the EweSync connection) it should be the service name, and for INFRA_RED it should be null.
port - The port number to use. This should not be 0.

ClientServerConnector

public ClientServerConnector(boolean amServer,
                             int port)
This is for Infra-Red connections only - it creates a client-server connection over the infra-red port where this entity will act as either the server or client as specified by the amServer parameter.

Parameters:
amServer - if this is true then this entity is the server, otherwise it is the client.
port - the port number to use (which should not be 0).
Method Detail

stopServer

public void stopServer()
If the connector is waiting for extra client connections this will close the server socket. It is safe to call this even if this is the client.


doStop

protected void doStop(int reason)
Description copied from class: TaskObject
This method, by default, sets the shouldStop variable to true. The Task should therefore always check this variable when coming out of a sleep() to see whether it should continue or stop.

You can override this to provide a different method of stopping the task.

Overrides:
doStop in class TaskObject
Parameters:
reason - some task specific reason for stopping. There are no pre-defined reasons.

connectToServer

public Socket connectToServer()
                       throws IOException
This is used to make another connection to the server process on the same server port where the initial connection was made.

Returns:
A connected Socket.
Throws:
IOException - if a connection could not be made.
IllegalStateException - if this connector is not the client.

connect

public Handle connect()
Start the connection process and immediately return a Handle to indicate its progress.


waitOnConnection

public boolean waitOnConnection()
                         throws IOException
Start the connecton and wait until the connection is made.

Returns:
true if this becomes the server, false if the client.
Throws:
IOException - if the connection failed.

client

protected abstract void client(Socket s)
This gets called on the first connection and if it is determined that I am the client. You can make further connections to the server by calling connectToServer(), but this method will not be called again.


server

protected abstract void server(Socket client,
                               boolean firstConnection)
This gets called every time the client connects - including the initial connection.

Parameters:
client - The client that is connected.
firstConnection - this is true if this is the initial connection.

doRun

protected void doRun()
Description copied from class: TaskObject
Override this method if you want to use an mThread to run your Task. Otherwise if you are using timers or callbacks, override the doStart() method.

Overrides:
doRun in class TaskObject