Class User
[ Source
| Index
| Back to GNS
]
- public class User
- extends Thread
User - Stores user data and provides the user with an connection
to interact with.
Each users is given it's own thread to run in. If the user fails to
send data to the server in 5 mins the connection will automatically close.
This will handle all the commands sent from the client, and handle
sending all the data from the server back to the client.
-
active
- Indicated if the players connection is active.
-
gameID
- The game or client ID that the user is using.
-
id
- The users ID number.
-
in
- Allows the server to communicate with the client.
-
log
- Used to log major commands to
PATH/log/access_log.
-
mode
- Used to hold information set by
-
myGroup
- Instnace of the current users group.
-
name
- The users name or handle.
-
out
- Allows the client to communicate with the server.
-
server
- Instance of the server so we can perform server level actions.
-
soc
- Open socket connecting the client and server.
-
type
- Type of incoming data.
-
User(Socket, logger, gnsd)
- Creates a new instance of a User.
-
getAddress()
- Gets the host address of the client's socket.
-
getGameID()
- Gets the users Game/Client ID number.
-
getID()
- Gets the users ID number.
-
getMode()
- Gets the users mode.
-
getUserName()
- Gets the users name.
-
handle(String)
- Handles input from the client when it's presented with data.
-
handleCommand(String)
- Handles all the commands the client could posible send.
-
handleData(String)
- Handles broadcasting of data to all members of a group.
-
join(Group)
- Allows the user to join a group.
-
join(int)
- Uses the server level commands to join a group.
-
joinNew(String)
- Allows the user to make a new group.
-
login(String)
- Allows the user to Login to the network and become authinticated.
-
logout(String)
- Causes the client connection to be closed.
-
part()
- Allows a user to leave a channel.
-
run()
- Handles the execution of the users thread.
-
send(String)
- Sends a message to the client.
-
sendGameCommand(String)
- Broadcast game information to all clients in a group.
-
sendPrivMessage(String)
- Sends a private message to a user.
-
toInt(String)
- Converts a String to an int;
soc
private Socket soc
- Open socket connecting the client and server. This is used for all
incoming and out going traffic.
in
private DataInputStream in
- Allows the server to communicate with the client.
out
private DataOutputStream out
- Allows the client to communicate with the server.
log
private logger log
- Used to log major commands to
PATH/log/access_log. This
will log @connect, @login, @join, @joinnew, @list, @part, and @logout.
Logs are timestamped via time_t, information about the user's host,
command, and other useful information is given.
id
private int id
- The users ID number. This is set to -1 if the user is not authenticated.
name
private String name
- The users name or handle. This will be null until the user has
authenticated.
gameID
private int gameID
- The game or client ID that the user is using. This will be null until the
user has authenticated. Used so a number of different clients can connet
to the server without them conflicting.
mode
private String mode
- Used to hold information set by @setmode. This can store information
to be transmited on joining the network, this can be used for control
reasons, or just to transmit extra data.
type
private char type
- Type of incoming data. Used to see if there is a leading @ or #.
server
private gnsd server
- Instance of the server so we can perform server level actions. This
includes joining groups, getting group lists, getting user counts, etc.
myGroup
private Group myGroup
- Instnace of the current users group. This is so the user can send
data to the group, can leave the group, etc. This will be null until
the user has joined a channel.
active
private boolean active
- Indicated if the players connection is active.
User
public User(Socket s,
logger l,
gnsd g) throws IOException
- Creates a new instance of a User. Sets up the input stream and output
stream, through buffers, from the client's socket. Also sets the timeout
to 5 minutes.
- Parameters:
- s - the client's socket.
- l - Instance of logger, use to log commands.
- g - Instance of the server so we can perform server
level actions.
run
public void run()
- Handles the execution of the users thread. We sit in an infinate loop
and read data from the input stream. The thread is only borken when
the client's connection is broken. Server is hardcoded to read UTF
data only.
- Overrides:
- run in class Thread
send
public void send(String msg)
- Sends a message to the client. Data is sent to the client in UTF format.
- Parameters:
- msg - the message to send.
join
public void join(Group g)
- Allows the user to join a group. A User can only be in one group at a
time, so the server is carefully crafted to make sure part is called
before we join a group.
- Parameters:
- g - the group to join.
getUserName
public String getUserName()
- Gets the users name.
- Returns:
- the name of the user.
getAddress
public String getAddress()
- Gets the host address of the client's socket. This is used for logging.
Later versions of the server may allow a @whois command so direct client
to client communication can be used.
- Returns:
- the host address of the client.
getID
public int getID()
- Gets the users ID number.
- Returns:
- the users ID number.
getMode
public String getMode()
- Gets the users mode.
- Returns:
- the users mode.
getGameID
public int getGameID()
- Gets the users Game/Client ID number.
- Returns:
- the users game/client ID number.
handle
private void handle(String message)
- Handles input from the client when it's presented with data. The
String needs to begin with @ (a command) or # (broadcast) for the
server to handle the information. Depending on which type of data
is sent the proper handling function is called. If the String does
not start with a known char, an error is returned to the client.
- See Also:
- handleCommand, handleData
handleCommand
private void handleCommand(String data)
- Handles all the commands the client could posible send. For a list of
all clients visit http://www.netdragons.com/games/24/gns/commands/. If
the command isn't understood, an error is returned to the client.
- Parameters:
- data - data given from the client.
handleData
private void handleData(String data)
- Handles broadcasting of data to all members of a group. If the
user is not authenticated, or is not in a group, and error is returned
to the client.
- Parameters:
- data - the data to broadcast.
sendGameCommand
private void sendGameCommand(String data)
- Broadcast game information to all clients in a group. This is used
to transmit informational data, such as a ship type, weapon change, etc.
most data will be send through handleData.
- Parameters:
- data - the data to broadcast.
- See Also:
- handleData
sendPrivMessage
private void sendPrivMessage(String data)
- Sends a private message to a user. This only works when the user is
in the same group that you are.
- Parameters:
- data - the data to private message
join
private void join(int cID)
- Uses the server level commands to join a group. If the user is not
authenticated, an error is returned to the client. If the group
does not exist, an error is returned to the client. To make sure
the user is not in a group, the part() function is called.
- Parameters:
- cID - group ID to join.
- See Also:
- part
joinNew
private void joinNew(String data)
- Allows the user to make a new group. This works much the same
way as the join command, and returns errors the same. To make
sure we don't create two group with the same ID, we check to
make sure the group doesn't exist. If it does already exist, it is
just like using join(id);
- Parameters:
- data - new channel information
- See Also:
- join
part
private synchronized void part()
- Allows a user to leave a channel. Makes sure that the user is in a group
to begin with, and if they are, they are removed from it. myGroup is
set to null to finish the operation.
- See Also:
- myGroup
login
private void login(String data)
- Allows the user to Login to the network and become authinticated. Uses
a server level command, and checks to make sure the name is valid, i.e.
not "logout". If authintication fails, an error is returned to the
client and they are disconnected. If the login work, id, gameID, and
and name are all set.
- Parameters:
- data - user data, "userID`password`gameID"
- See Also:
- id, gameID, name
logout
private void logout(String msg)
- Causes the client connection to be closed. If posible the client
will be notified that the connection is being closed.
- Parameters:
- msg - Message to send to client.
toInt
private int toInt(String s)
- Converts a String to an int;
- Parameters:
- s - The String to be converted
- Returns:
- an
int value from s
|