soc.client
Class SOCPlayerClient.ClientNetwork

java.lang.Object
  extended by soc.client.SOCPlayerClient.ClientNetwork
Enclosing class:
SOCPlayerClient

public static class SOCPlayerClient.ClientNetwork
extends java.lang.Object

Helper object to encapsulate and deal with network connectivity.

Local practice server (if any) is started in startPracticeGame(String, Map).
Local tcp server (if any) is started in initLocalServer(int).
Network shutdown is disconnect() or dispose().

Before v2.0.00, most of these fields and methods were part of the main SOCPlayerClient class.

Since:
2.0.00
Author:
Paul Bilnoski <paul@bilnoski.net>
See Also:
SOCPlayerClient.getNet()

Nested Class Summary
(package private) static class SOCPlayerClient.ClientNetwork.NetReadTask
          A task to continuously read from the server socket.
(package private)  class SOCPlayerClient.ClientNetwork.SOCPlayerLocalStringReader
          For practice games, reader thread to get messages from the practice server to be treated and reacted to.
 
Field Summary
(package private)  SOCPlayerClient client
           
(package private)  boolean connected
          Are we connected to a TCP server (remote or localTCPServer)? practiceServer is not a TCP server.
(package private)  java.lang.Exception ex
          Any network error (TCP communication) received while connecting or sending messages in putNet(String), or null.
(package private)  java.lang.Exception ex_P
          Practice-server error (stringport pipes), or null
private  java.lang.String host
          Hostname we're connected to, or null
(package private)  java.io.DataInputStream in
           
protected  java.lang.String lastMessage_N
          For debug, our last messages sent, over the net or practice server (pipes)
protected  java.lang.String lastMessage_P
          For debug, our last messages sent, over the net or practice server (pipes)
private  SOCServer localTCPServer
          Client-hosted TCP server.
(package private)  java.io.DataOutputStream out
           
private  int port
          TCP port we're connected to; default is SOC_PORT_DEFAULT.
protected  SOCServer practiceServer
          Server for practice games via prCli; not connected to the network, not suited for hosting multi-player games.
protected  StringConnection prCli
          Client connection to practice server.
(package private)  java.lang.Thread reader
           
(package private)  java.net.Socket s
           
static int SOC_PORT_DEFAULT
          Default tcp port number 8880 to listen, and to connect to remote server.
 
Constructor Summary
SOCPlayerClient.ClientNetwork(SOCPlayerClient c)
           
 
Method Summary
 boolean anyHostedActiveGames()
          Look for active games that we're hosting (state >= START1A, not yet OVER).
 void connect(java.lang.String chost, int sPort)
          Attempts to connect to the server.
protected  void disconnect()
          Disconnect from the net (client of remote server).
 void dispose()
          Shut down the local TCP server (if any) and disconnect from the network.
 java.lang.String getHost()
          Hostname of the tcp server we're a client of
 int getLocalServerPort()
          Get the tcp port number of the local server.
 int getPort()
          Port number of the tcp server we're a client of; default is SOC_PORT_DEFAULT.
 boolean initLocalServer(int tport)
          Create and start the local TCP server on a given port.
 boolean isConnected()
          Are we connected to a tcp server?
 boolean isRunningLocalServer()
          Are we running a local tcp server?
 boolean putLeaveAll()
          For shutdown - Tell the server we're leaving all games.
 boolean putNet(java.lang.String s)
          write a message to the net: either to a remote server, or to localTCPServer for games we're hosting.
 boolean putPractice(java.lang.String s)
          write a message to the practice server.
 void resendNet()
          resend the last message (to the network)
 void resendPractice()
          resend the last message (to the practice server)
 void shutdownLocalServer()
          Shut down the local TCP server.
 void startPracticeGame(java.lang.String practiceGameName, java.util.Map<java.lang.String,SOCGameOption> gameOpts)
          Start a practice game.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOC_PORT_DEFAULT

public static final int SOC_PORT_DEFAULT
Default tcp port number 8880 to listen, and to connect to remote server. Should match SOCServer.SOC_PORT_DEFAULT.

8880 is the default SOCPlayerClient port since jsettlers 1.0.4, per cvs history.

Since:
1.1.00
See Also:
Constant Field Values

client

final SOCPlayerClient client

host

private java.lang.String host
Hostname we're connected to, or null


port

private int port
TCP port we're connected to; default is SOC_PORT_DEFAULT.


localTCPServer

private SOCServer localTCPServer
Client-hosted TCP server. If client is running this server, it's also connected as a client, instead of being client of a remote server. Started via SOCPlayerClient.GameAwtDisplay.startLocalTCPServer(int). practiceServer may still be activated at the user's request. Note that SOCGame.isPractice is false for localTCPServer's games.


s

java.net.Socket s

in

java.io.DataInputStream in

out

java.io.DataOutputStream out

reader

java.lang.Thread reader

ex

java.lang.Exception ex
Any network error (TCP communication) received while connecting or sending messages in putNet(String), or null. If ex != null, putNet will refuse to send.

The exception's toString() including its getMessage() may be displayed to the user by SOCPlayerClient.dispose(); if throwing an error that the user should see, be sure to set the detail message.


ex_P

java.lang.Exception ex_P
Practice-server error (stringport pipes), or null


connected

boolean connected
Are we connected to a TCP server (remote or localTCPServer)? practiceServer is not a TCP server.

See Also:
ex

lastMessage_N

protected java.lang.String lastMessage_N
For debug, our last messages sent, over the net or practice server (pipes)


lastMessage_P

protected java.lang.String lastMessage_P
For debug, our last messages sent, over the net or practice server (pipes)


practiceServer

protected SOCServer practiceServer
Server for practice games via prCli; not connected to the network, not suited for hosting multi-player games. Use localTCPServer for those. SOCMessages of games where SOCGame.isPractice is true are sent to practiceServer.

Null before it's started in SOCPlayerClient.startPracticeGame().


prCli

protected StringConnection prCli
Client connection to practice server. Null before it's started in SOCPlayerClient.startPracticeGame().

Last message is in lastMessage_P; any error is in ex_P.

Constructor Detail

SOCPlayerClient.ClientNetwork

public SOCPlayerClient.ClientNetwork(SOCPlayerClient c)
Method Detail

dispose

public void dispose()
Shut down the local TCP server (if any) and disconnect from the network.


startPracticeGame

public void startPracticeGame(java.lang.String practiceGameName,
                              java.util.Map<java.lang.String,SOCGameOption> gameOpts)
Start a practice game. If needed, create and start practiceServer.

Parameters:
practiceGameName - Game name
gameOpts - Game options

getLocalServerPort

public int getLocalServerPort()
Get the tcp port number of the local server.

See Also:
isRunningLocalServer()

shutdownLocalServer

public void shutdownLocalServer()
Shut down the local TCP server.


initLocalServer

public boolean initLocalServer(int tport)
Create and start the local TCP server on a given port. If startup fails, show a NotifyDialog with the error message.

Returns:
True if started, false if not

getPort

public int getPort()
Port number of the tcp server we're a client of; default is SOC_PORT_DEFAULT.


getHost

public java.lang.String getHost()
Hostname of the tcp server we're a client of


isConnected

public boolean isConnected()
Are we connected to a tcp server?


connect

public void connect(java.lang.String chost,
                    int sPort)
             throws java.lang.IllegalStateException
Attempts to connect to the server. See isConnected() for success or failure. Once connected, starts a reader thread. The first message over the connection is our version, and the second is the server's response: Either SOCRejectConnection, or the lists of channels and games (SOCChannels, SOCGames).

Since user login and authentication don't occur until a game or channel join is requested, no username or password is needed here.

Before 1.1.06, the server's response was first, and version was sent in reply to server's version.

Parameters:
chost - Server host to connect to, or null for localhost
sPort - Server TCP port to connect to; the default server port is SOC_PORT_DEFAULT.
Throws:
java.lang.IllegalStateException - if already connected
See Also:
SOCServer.newConnection1(StringConnection)

disconnect

protected void disconnect()
Disconnect from the net (client of remote server). If a problem occurs, sets ex.

See Also:
dispose()

isRunningLocalServer

public boolean isRunningLocalServer()
Are we running a local tcp server?

See Also:
getLocalServerPort(), anyHostedActiveGames()

anyHostedActiveGames

public boolean anyHostedActiveGames()
Look for active games that we're hosting (state >= START1A, not yet OVER).

Returns:
If any hosted games of ours are active
See Also:
SOCPlayerClient.GameAwtDisplay.findAnyActiveGame(boolean)

putNet

public boolean putNet(java.lang.String s)
write a message to the net: either to a remote server, or to localTCPServer for games we're hosting.

If ex != null, or ! connected, putNet returns false without attempting to send the message.

This message is copied to lastMessage_N; any error sets ex and calls SOCPlayerClient.dispose() to show the error message.

Parameters:
s - the message
Returns:
true if the message was sent, false if not
See Also:
SOCPlayerClient.GameManager.put(String, boolean)

putPractice

public boolean putPractice(java.lang.String s)
write a message to the practice server. localTCPServer is not the same as the practice server; use putNet(String) to send a message to the local TCP server. Use putPractice only with practiceServer.

Before version 1.1.14, this was putLocal.

Parameters:
s - the message
Returns:
true if the message was sent, false if not
See Also:
SOCPlayerClient.GameManager.put(String, boolean)

resendNet

public void resendNet()
resend the last message (to the network)


resendPractice

public void resendPractice()
resend the last message (to the practice server)


putLeaveAll

public boolean putLeaveAll()
For shutdown - Tell the server we're leaving all games. If we've started a practice server, also tell that server. If we've started a TCP server, tell all players on that server, and shut it down.

Since no other state variables are set, call this only right before discarding this object or calling System.exit.

Returns:
Can we still start practice games? (No local exception yet in ex_P)