Class server
java.lang.Object
|
+----sun.net.NetworkServer
|
+----server
class server extends NetworkServer
Constructors
- server - public server()
- The main server class. This class contains all of the arrays and variables used to keep track of the clients. The following arrays are used to keep track of information within the server;
- user - stores user names.
- net_input - tracks client network input data stream.
- net_output - tracks client network output data stream.
- channel_list - the list of channels available on the server.
- channel_count - the number of users on each channel in channel_list.
- user_type - stores the type of the user: L = locally connected client. R = remotely connected client (through another server). S = connection to another server.
- user_channel - the channel the user is on.
- user_filter - the filtered users list for each client.
All of these arrays keep track of information for all clients, indexed by the client's index number. The information in the arrays is available to all client sessions on the server. These arrays form a table of information about the clients. Other variables in the server keep track of the number of channels, the connection to another server (if one exists), and when to update the server's table.
When the server is started several initialization routines are called. read_info is called to read the startup channels from startup.channels, then read_config scans the server.config file to read in the port the server should start on, and the other server to connect to. After the startup information has been read the startServer class is invoked (part of the NetworkServer class). This binds the server to the port specified in the config file and sets it to listen for connections. The server will then try to connect to another server if the parameters are found in the config file. The server is then ready to receive connections. Any connection to the server starts the serviceRequest class, which handles the connection. There is also a while loop in the server that contains the cleanup code for the channel table. When the update flag is set (by another thread or class) the server class scans the channel table for empty user-created channels. These channels are removed from the table and client lists. When the update flag is set the server sends the latest channel list to the clients, including any changes to the number of users on a channel. This loop also calls the write_info class to update the files used by the CGI programs.
Methods
- main - public static void main(String args[])
- Used to start the server class. A main class is required in all java programs (not applets).
- serviceRequest - public void serviceRequest() Overrides: serviceRequest in class NetworkServer
- serviceRequest is called by the server when a connection is made to the server from a client. The serviceRequest class gets the client's network input and output data streams from the server class (a function of the NetworkServer class). It places these values in the table after searching for the first empty place in the table. This class reads the username and channel from the client and places this information in the table. serviceRequest then checks to see if this client is another server and updates the table accordingly. If the client is started on a new channel the class adds the channel to the table and informs other clients of the new channel. The serviceRequest class then starts a reader thread for the client.
- read_net_input - String read_net_input()
- A simple function that returns one line of input from the network data stream.
- write_net_output - void write_net_output(String string)
- The complement to the above function, writes one line of text to the network, flushing the stream to assure the information is sent.
- read_info - void read_info() throws IOException
- Opens the file startup.channels in the current directory and reads the initial channel list. These channels are permanent and will not be removed.
- write_info - void write_info() throws IOException
- Opens the three files used by the CGI programs. The current active channels are written to channel.list . The current users on the Qchat server are written to user.list . The current users and the channels they are on are written to user_on_channel.list . The end of these files are filled with blank lines to eliminate any old information.
- read_config - void read_config() throws IOException
- Opens and scans the file for the startup information. port # specifies the server's port number. server servername and server_port # specify another server/port combination to connect to at startup.
- display - public void display()
- Similar to the display function in the client, this display calls other functions to update the internal table based on messages received from another server. Sets the update flag to ensure the server's table is correct.
- connect - boolean connect()
- Connect opens the network connection to another server and initializes this server's socket connection to the other server. Connect handles the exceptions from creating a network connection. It also initializes the input and output data streams.
- adduser - void adduser(String input)
- Parses the ADDUSER command received from the other server. Adds the user to the local table as a remote user, informing other clients of the changes in channels and user counts.
- removeuser - void removeuser(String input)
- Interprets the REMOVEUSER command received from the other server. Removes the remote user from the local table and informs local clients of the changes in channels and users.
- text - void text(String input)
- Forwards TEXT messages received from the other server to the correct users.