Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/zazof/jtegServer/ClientConnection.java


1   package org.zazof.jtegServer;
2   
3   import java.net.*;
4   import java.util.*;
5   import java.io.*;
6   import org.apache.log4j.Category;
7   
8   /**
9    * This class is a socket connection to a client teg/jteg application
10   */
11  
12  public class ClientConnection implements Runnable{
13  
14    public ClientConnection(Socket clientSocket, ConnectionController controller) throws IOException{
15      $socket = clientSocket;
16      $player = new Player(this);
17      $controller = controller;
18      $out = new BufferedWriter(new OutputStreamWriter($socket.getOutputStream()));
19      $in = new BufferedReader(new InputStreamReader($socket.getInputStream()));
20      $thread = new Thread(this);
21      $thread.start();
22      log.info("created ClientConnection from IP " + $socket.getInetAddress());
23    }
24  
25    public void run(){
26      String input;
27      StringTokenizer st;
28      String message;
29      try{
30        while(true){
31          // this call can block
32          input = read();  
33          log.debug("read from " + $player.getName() + ", message: " + input);
34          // check if the client closed his connection
35          if (input == null){
36            log.info("Client closed his connection");
37            throw new IOException();
38          }
39          // a client can send more than 1 message, each message is separated by a ";"
40          st = new StringTokenizer(input, ";");
41          while(st.hasMoreTokens()){
42            message = st.nextToken();
43            if (message.indexOf("null") != -1){
44              log.debug("Received message containing null: " + message);
45            }else{
46              $controller.processMessage(message, $player);
47            }
48          }
49          st = null;
50        }
51      }
52      catch (IOException exc){
53        // we close the connection and inform the ConnectionController
54        try{
55          $socket.close();
56          $controller.removeConnection(this);
57        }
58        catch (IOException ioExc){
59          // the conenction close gave troubles
60          $controller.removeConnection(this); 
61        }
62      }
63    }
64  
65    /**
66     * reads a message from the client
67     * blocking call
68     */
69    public String read() throws IOException{
70      return $in.readLine();
71    }
72  
73    /**
74     * sends a message to the client
75     */
76    public void write(String message){
77      try{
78        $out.write(message);
79        $out.newLine();
80        $out.flush();
81        log.debug("write to " + $player.getName() + ", message: " + message);
82      }
83      catch (IOException exc){
84        $controller.removeConnection(this);
85      }
86    }
87  
88    private BufferedWriter $out;
89    private BufferedReader $in;
90    private Socket $socket;
91    private Thread $thread;
92    private ConnectionController $controller;
93    private static final boolean $debug = true;
94    private Player $player;
95  
96    static Category log = Category.getInstance("network");
97  }