Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-322 Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.

Similar presentations


Presentation on theme: "240-322 Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting."— Presentation transcript:

1 240-322 Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting 240-322, Semester 1, 2005-2006 12. Java Chat

2 240-322 Cli/Serv.: Chat/122 Contents 1.Chat Characteristics 2.Threaded TCP Client/Server Chat 3.Other Approaches

3 240-322 Cli/Serv.: Chat/123 1. Chat Characteristics v Client/Server or P2P server clients messages clients continued

4 240-322 Cli/Serv.: Chat/124 v Any number of clients. v Clients can join/depart at any time. v 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

5 240-322 Cli/Serv.: Chat/125 1.1 Chatting Compared to Games v 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

6 240-322 Cli/Serv.: Chat/126 v But chatting is similar to multiplayer online games (MMOGs) such as City Of Heroes, Ragnarok. v The chat communication model is used by MMOGs.

7 240-322 Cli/Serv.: Chat/127 1.2. Advantages of a Server Model v Client control –e.g. a server can filter client messages v Management –e.g. login/logout, message routing, billing v Resource Storage –e.g. message logs, images, movies, sounds v These features are harder to support in P2P.

8 240-322 Cli/Serv.: Chat/128 1.3. Disadvantages of a Server Model v A single point of failure. v A communications bottleneck. v Increased message latency. v Many of these problems are solved by using P2P, or by using multiple servers.

9 240-322 Cli/Serv.: Chat/129 1.4. More Information v The examples in these slides come from: Killer Game Programming in Java (KGPJ) Chapter 30 http://fivedots.coe.psu.ac.th/~ad/jg/ch19 v A draft of the chapter, and all the code, can be found there.

10 240-322 Cli/Serv.: Chat/1210 2. Threaded TCP Client/Server Chat continued

11 240-322 Cli/Serv.: Chat/1211 2.1. Client Classes v ChatClient –maintains the GUI, processes the user's input, and sends messages to the server over a TCP link v ChatWatcher –waits for messages from the server, and displays them in ChatClient's GUI text area

12 240-322 Cli/Serv.: Chat/1212 2.2. Client-side Messages v ChatClient can send the following messages: – –who – –bye – –any text message server messages client

13 240-322 Cli/Serv.: Chat/1213 2.3. Server Classes v ChatServer –the top-level server, which spawns a ChatServerHandler thread when a new client connects to it v ChatServerHandler –handles the communication with a client v ChatGroup –holds client details (e.g. input streams), accessible via synchronized methods

14 240-322 Cli/Serv.: Chat/1214 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 server messages clients

15 240-322 Cli/Serv.: Chat/1215 2.5. ChatClient sends the bye message sends the who message sends a text message shows messages received from the server

16 240-322 Cli/Serv.: Chat/1216 Contacting the Server // globals // server details private static final int PORT = 1234; private static final String HOST = "localhost"; private Socket sock; private PrintWriter out; // output to the server continued

17 240-322 Cli/Serv.: Chat/1217 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); } }

18 240-322 Cli/Serv.: Chat/1218 2.6. ChatWatcher Thread run() { // wait for message // process it // show in GUI } from server WHO$$, broadcast msg ChatWatcher (on client-side) continued looping

19 240-322 Cli/Serv.: Chat/1219 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 immediately client.showMsg(line + "\n"); }

20 240-322 Cli/Serv.: Chat/1220 2.7. ChatServer // wait for client connection // create handler for client port 1234 ChatServer (on the server-side) client connections continued looping

21 240-322 Cli/Serv.: Chat/1221 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); } }

22 240-322 Cli/Serv.: Chat/1222 2.8. ChatServerHandler v A handler is created for each client that contacts the server. v The handler is a thread that processes all the clients interaction with the server. continued

23 240-322 Cli/Serv.: Chat/1223 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 ChatGroup processClient(in, out); // interact with client : continued

24 240-322 Cli/Serv.: Chat/1224 // 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); } }

25 240-322 Cli/Serv.: Chat/1225 processClient() v 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

26 240-322 Cli/Serv.: Chat/1226 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 message cg.broadcast( "("+cliAddr+", "+port+"): " + line); } cg is the ChatGroup object

27 240-322 Cli/Serv.: Chat/1227 2.9. ChatGroup v Stores client details in Chatter objects –1 Chatter object per client –details include client input stream v Answers "who" messages. v Handles broadcasting. v ChatGroup uses synchronized methods to control access to the clients streams. continued

28 240-322 Cli/Serv.: Chat/1228

29 240-322 Cli/Serv.: Chat/1229 Why is ChatGroup Synchronized?

30 240-322 Cli/Serv.: Chat/1230 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()

31 240-322 Cli/Serv.: Chat/1231 2.10. The Chatter Class v A Chatter object manages a client's address, port and a PrintWriter stream going to the client. private PrintWriter out; // global public void sendMessage(String msg) { out.println(msg); }

32 240-322 Cli/Serv.: Chat/1232 3. Other Approaches v Chapter 30 in KGPJ contains two other chat implementations, using: –UDP multicasting (a form of P2P) –a servlet server

33 240-322 Cli/Serv.: Chat/1233 3.1. UDP Multicasting Clients and a Name Server continued

34 240-322 Cli/Serv.: Chat/1234 v Not pure P2P -- it uses a login name server.  NameServer deals with who messages –reduces the packets volume in the multicast group v Main problem: there's no easy way to control access to the multicast group.

35 240-322 Cli/Serv.: Chat/1235 3.2. Servlet as a Server continued

36 240-322 Cli/Serv.: Chat/1236 v 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 v Since the servlet is contacted using HTTP, it will work through most firewalls.


Download ppt "240-322 Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting."

Similar presentations


Ads by Google