360 likes | 575 Views
Client/Server Distributed Systems. 240-322, Semester 1, 2005-2006. Objectives discuss a client/server based chat system mention two other ways of chatting. 12. Java Chat. Contents. 1. Chat Characteristics 2. Threaded TCP Client/Server Chat 3. Other Approaches. 1. Chat Characteristics.
E N D
Client/Server Distributed Systems 240-322, Semester 1, 2005-2006 • Objectives • discuss a client/server based chat system • mention two other ways of chatting 12. Java Chat
Contents 1. Chat Characteristics 2. Threaded TCP Client/Server Chat 3. Other Approaches
1. Chat Characteristics • Client/Server or P2P server messages clients clients continued
Any number of clients. • Clients can join/depart at any time. • There's no ordering to the communication • anyone can talk at any time • a message can arrive at a client at any time • a message can be sent by a client at any time
1.1 Chatting Compared to Games • Typical games are chess, monopoly, tic-tac-toe. These types of games have: • a fixed number of players • the game starts only when all the players are present • the players take fixed turns continued
But chatting is similar to multiplayer online games (MMOGs) such as City Of Heroes, Ragnarok. • The chat communication model is used by MMOGs.
1.2. Advantages of a Server Model • Client control • e.g. a server can filter client messages • Management • e.g. login/logout, message routing, billing • Resource Storage • e.g. message logs, images, movies, sounds • These features are harder to support in P2P.
1.3. Disadvantages of a Server Model • A single point of failure. • A communications bottleneck. • Increased message latency. • Many of these problems are solved by using P2P, or by using multiple servers.
1.4. More Information • The examples in these slides come from: Killer Game Programming in Java (KGPJ)Chapter 30http://fivedots.coe.psu.ac.th/~ad/jg/ch19 • A draft of the chapter, and all the code, can be found there.
2. Threaded TCP Client/Server Chat continued
2.1. Client Classes • ChatClient • maintains the GUI, processes the user's input, and sends messages to the server over a TCP link • ChatWatcher • waits for messages from the server, and displays them in ChatClient's GUI text area
2.2. Client-side Messages • ChatClient can send the following messages: • who • bye • any text message messages server client
2.3. Server Classes • ChatServer • the top-level server, which spawns a ChatServerHandler thread when a new client connects to it • ChatServerHandler • handles the communication with a client • ChatGroup • holds client details (e.g. input streams), accessible via synchronized methods
2.4. Server-side Messages • WHO$$ cliAddr1 & port1 & ... cliAddrN & portN & • sent back to a client in response to a "who" message • (cliAddr, port): message • broadcast to all the clients messages server clients
2.5. ChatClient sends the bye message shows messages received from the server sends a text message sends the who message
Contacting the Server // globals// server detailsprivate static final int PORT = 1234; private static final String HOST = "localhost";private Socket sock;private PrintWriter out; // output to the server continued
private void makeContact(){ try { sock = new Socket(HOST, PORT); BufferedReader in = new BufferedReader( new InputStreamReader( sock.getInputStream()) ); out = new PrintWriter(sock.getOutputStream(),true ); new ChatWatcher(this, in).start(); // watch for server msgs } catch(Exception e) { System.out.println(e); }}
2.6. ChatWatcher Thread from server run() { // wait for message // process it // show in GUI} WHO$$, broadcast msg looping ChatWatcher (on client-side) continued
while ((line = in.readLine()) != null) { if ((line.length() >= 6) && // "WHO$$ " (line.substring(0,5).equals("WHO$$"))) showWho( line.substring(5).trim() ); // remove WHO$$ keyword and spaces else // show immediatelyclient.showMsg(line + "\n");}
2.7. ChatServer port1234 client connections // wait for client connection // create handler for client looping ChatServer (on the server-side) continued
public ChatServer(){ cg = new ChatGroup(); try { ServerSocket serverSock = new ServerSocket(PORT); Socket clientSock; while (true) { System.out.println("Waiting for a client..."); clientSock = serverSock.accept(); new ChatServerHandler(clientSock, cg).start(); } } catch(Exception e) { System.out.println(e); }}
2.8. ChatServerHandler • A handler is created for each client that contacts the server. • The handler is a thread that processes all the clients interaction with the server. continued
public void run(){ try { // Get I/O streams from the socket BufferedReader in = new BufferedReader( new InputStreamReader( clientSock.getInputStream())); PrintWriter out = new PrintWriter( clientSock.getOutputStream(), true); cg.addPerson(cliAddr, port, out); // add client to ChatGroupprocessClient(in, out); // interact with client : continued
// the client has finished when execution // reaches here cg.delPerson(cliAddr, port); // remove client details clientSock.close(); System.out.println("Client (" + cliAddr + ", " + port + ") connection closed\n"); } catch(Exception e) { System.out.println(e); }}
processClient() • A loop which: • waits for a message from the client • processes the message, using doRequest() • if a response (or broadcast) is needed, it is carried out by the ChatGroup object continued
private void doRequest(String line, PrintWriter out){ if (line.trim().toLowerCase().equals("who")) { System.out.println("Processing 'who'"); out.println( cg.who() ); } else // use ChatGroup object to broadcast messagecg.broadcast( "("+cliAddr+", "+port+"): " + line);} cg is the ChatGroup object
2.9. ChatGroup • Stores client details in Chatter objects • 1 Chatter object per client • details include client input stream • Answers "who" messages. • Handles broadcasting. • ChatGroup uses synchronized methods to control access to the clients streams. continued
A ChatGroup Method synchronized public void broadcast(String msg){ Chatter c; for(int i=0; i < chatPeople.size(); i++) { c = (Chatter) chatPeople.get(i); c.sendMessage(msg); }} // end of broadcast()
2.10. The Chatter Class • A Chatter object manages a client's address, port and a PrintWriter stream going to the client. private PrintWriter out; // globalpublic void sendMessage(String msg){ out.println(msg); }
3. Other Approaches • Chapter 30 in KGPJ contains two other chat implementations, using: • UDP multicasting (a form of P2P) • a servlet server
Not pure P2P -- it uses a login name server. • NameServer deals with who messages • reduces the packets volume in the multicast group • Main problem: there's no easy way to control access to the multicast group.
3.2. Servlet as a Server continued
The key difference between this approach and the standard client/server is that ChatServlet cannot start communication with a client. • URLChatWatcher must periodically poll the ChatServlet for messages • Since the servlet is contacted using HTTP, it will work through most firewalls.