objectdraw
Class Server

java.lang.Object
  extended by objectdraw.Server
All Implemented Interfaces:
Runnable

public class Server
extends Object

A Server runs on one computer and allows other computers (clients) to connect to it. The clients can then send messages to the server and receive messages back. The server also announces its presence across the local area network (LAN) so that computers at the same site can see that a game is happening.

You will never create a Server object directly. Instead, write a class that implements ServerListener and pass an object of that class to Network.configure(). This will result in that object being informed when server-related events happen.

The Server class contains methods to send messages either to a particular client, or to all the clients at once. Your ServerListener will be passed a Server object with each event; you could also keep it in an instance variable so that you can send messages whenever you want.

Author:
Russell Zahniser (russell@zahniser.net)
See Also:
ServerListener, Network.configure(String, ServerListener, ClientListener)

Method Summary
 String getName()
          Get the server name.
 boolean isRunning()
          Check if the server is still running.
 void sendMessage(String message)
          Send a message to every client connected to the server.
 void sendMessage(String message, int channelID)
          Send a message to a single client.
 void shutDown()
          Shut down the server, disconnecting all clients.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Method Detail

getName

public String getName()
Get the server name. This is the name that the user entered in the "Host a network game" window.

Returns:
the server name

isRunning

public boolean isRunning()
Check if the server is still running.

Returns:
true if the server is running, false otherwise.

sendMessage

public void sendMessage(String message)
Send a message to every client connected to the server. This will result in the client listener getting a messageReceived() event on all the computers connected to this server.

Parameters:
message - The message to send.

sendMessage

public void sendMessage(String message,
                        int channelID)
Send a message to a single client. This will result in the client listener on that computer getting a messageReceived() event. The channel ID is the number identifying that client that is passed to the listener along with client-specific events.

Parameters:
message - The message to send.
channelID - The ID of the client to send the message to.

shutDown

public void shutDown()
Shut down the server, disconnecting all clients. Each client listener will receive a clientDisconnected() message, and the server listener will receive a channelClosed() event for every client, followed by a serverStopped() event. Quitting the program will automatically shut down the server.