soc.server.genericServer
Class LocalStringServerSocket

java.lang.Object
  extended by soc.server.genericServer.LocalStringServerSocket
All Implemented Interfaces:
StringServerSocket

public class LocalStringServerSocket
extends java.lang.Object
implements StringServerSocket

Clients who want to connect, call connectTo and are queued. (Thread.wait is used internally) Server-side calls accept to retrieve them.

  1.0.0 - 2007-11-18 - initial release
  1.0.3 - 2008-08-08 - add change history; no other changes in this file since 1.0.0
  1.0.4 - 2008-09-04 - no change in this file
  1.0.5 - 2009-05-31 - no change in this file
  1.0.5.1- 2009-10-26- no change in this file

Version:
1.0.5.1
Author:
Jeremy D. Monin

Field Summary
static int ACCEPT_QUEUELENGTH
          Length of queue for accepting new connections; default 100.
protected  java.util.Vector<LocalStringConnection> acceptQueue
          Waiting client connections (client-peer sides); Added by connectClient, removed by accept method
protected  java.util.Vector<LocalStringConnection> allConnected
          Server-peer sides of connected clients; Added by accept method
protected static java.util.Hashtable<java.lang.String,LocalStringServerSocket> allSockets
           
(package private)  boolean out_setEOF
           
private  java.lang.String socketName
           
private  java.lang.Object sync_out_setEOF
           
 
Constructor Summary
LocalStringServerSocket(java.lang.String name)
           
 
Method Summary
 StringConnection accept()
          For server to call.
 java.util.Enumeration<LocalStringConnection> allClients()
           
 void broadcast(java.lang.String msg)
          Send to all connected clients.
 void close()
          Close down server socket immediately: Do not let inbound data drain.
static LocalStringConnection connectTo(java.lang.String name)
          Find and connect to stringport with this name.
static LocalStringConnection connectTo(java.lang.String name, LocalStringConnection client)
          Find and connect to stringport with this name.
 void disconnectEOFClients()
          If our server won't receive any more data from the client, disconnect them.
 java.lang.String getSocketName()
           
 boolean isOutEOF()
          Have we closed our outbound side?
protected  LocalStringConnection queueAcceptClient(LocalStringConnection client)
          Queue this client to be accepted, and return their new server-peer; if calling this from methods initiated by the client, check if accepted.
 void setEOF()
          Close down server socket, but don't disconnect anyone: Accept no new inbound connections.
protected  void setEOF(boolean forceDisconnect)
          Close down server socket, possibly disconnect everyone; For use by setEOF() and close().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

allSockets

protected static java.util.Hashtable<java.lang.String,LocalStringServerSocket> allSockets

ACCEPT_QUEUELENGTH

public static int ACCEPT_QUEUELENGTH
Length of queue for accepting new connections; default 100. Changing it here affects future calls to connectTo() in all instances.


allConnected

protected java.util.Vector<LocalStringConnection> allConnected
Server-peer sides of connected clients; Added by accept method


acceptQueue

protected java.util.Vector<LocalStringConnection> acceptQueue
Waiting client connections (client-peer sides); Added by connectClient, removed by accept method


socketName

private java.lang.String socketName

out_setEOF

boolean out_setEOF

sync_out_setEOF

private java.lang.Object sync_out_setEOF
Constructor Detail

LocalStringServerSocket

public LocalStringServerSocket(java.lang.String name)
Method Detail

connectTo

public static LocalStringConnection connectTo(java.lang.String name)
                                       throws java.net.ConnectException,
                                              java.lang.IllegalArgumentException
Find and connect to stringport with this name. Intended to be called by client thread. Will block-wait until the server calls accept(). Returns a new client connection after accept.

Parameters:
name - Stringport server name to connect to
Throws:
java.net.ConnectException - If stringport name is not found, or is EOF, or if its connect/accept queue is full.
java.lang.IllegalArgumentException - If name is null

connectTo

public static LocalStringConnection connectTo(java.lang.String name,
                                              LocalStringConnection client)
                                       throws java.net.ConnectException,
                                              java.lang.IllegalArgumentException
Find and connect to stringport with this name. Intended to be called by client thread. Will block-wait until the server calls accept().

Parameters:
name - Stringport server name to connect to
client - Existing unused connection object to connect with
Returns:
client parameter object, connected to a LocalStringServer
Throws:
java.net.ConnectException - If stringport name is not found, or is EOF, or if its connect/accept queue is full.
java.lang.IllegalArgumentException - If name is null, client is null, or client is already peered/connected.

queueAcceptClient

protected LocalStringConnection queueAcceptClient(LocalStringConnection client)
                                           throws java.lang.IllegalStateException,
                                                  java.lang.IllegalArgumentException,
                                                  java.net.ConnectException,
                                                  java.io.EOFException
Queue this client to be accepted, and return their new server-peer; if calling this from methods initiated by the client, check if accepted. If not accepted yet, call Thread.wait on the returned new peer object. Once the server has accepted them, it will call Thread.notify on that object.

Parameters:
client - Client to queue to accept
Returns:
peer Server-side peer of this client
Throws:
java.lang.IllegalStateException - If we are at EOF already
java.lang.IllegalArgumentException - If client is or was accepted somewhere already
java.net.ConnectException - If accept queue is full (ACCEPT_QUEUELENGTH)
java.io.EOFException - If client is at EOF already
See Also:
accept(), ACCEPT_QUEUELENGTH

accept

public StringConnection accept()
                        throws java.net.SocketException,
                               java.io.IOException
For server to call. Blocks waiting for next inbound connection. (Synchronizes on accept queue.)

Specified by:
accept in interface StringServerSocket
Returns:
The server-side peer to the inbound client connection
Throws:
java.net.SocketException - if our setEOF() has been called, thus new clients won't receive any data from us
java.io.IOException - if a network problem occurs (Which won't happen with this local communication)

allClients

public java.util.Enumeration<LocalStringConnection> allClients()
Returns:
Server-peer sides of all currently connected clients (LocalStringConnections)

broadcast

public void broadcast(java.lang.String msg)
Send to all connected clients.

Parameters:
msg - String to send
See Also:
allClients()

disconnectEOFClients

public void disconnectEOFClients()
If our server won't receive any more data from the client, disconnect them. Considered EOF if the client's server-side peer connection inbound EOF is set. Removes from allConnected and set outbound EOF flag on that connection.


getSocketName

public java.lang.String getSocketName()
Returns:
Returns the socketName.

setEOF

public void setEOF()
Close down server socket, but don't disconnect anyone: Accept no new inbound connections. Send EOF marker in all current outbound connections. Continue to allow data from open inbound connections.

See Also:
close()

setEOF

protected void setEOF(boolean forceDisconnect)
Close down server socket, possibly disconnect everyone; For use by setEOF() and close(). Accept no new inbound connections. Send EOF marker in all current outbound connections.

Parameters:
forceDisconnect - Call disconnect on clients, or just send them an EOF marker?
See Also:
close(), LocalStringConnection.disconnect(), LocalStringConnection.setEOF()

isOutEOF

public boolean isOutEOF()
Have we closed our outbound side?

See Also:
close(), setEOF()

close

public void close()
           throws java.io.IOException
Close down server socket immediately: Do not let inbound data drain. Accept no new inbound connections. Send EOF marker in all current outbound connections. Like java.net.ServerSocket, any thread currently blocked in accept() will throw a SocketException.

Specified by:
close in interface StringServerSocket
Throws:
java.io.IOException
See Also:
setEOF()