Package server

Source Code of server.ServerThread

package server;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import org.apache.log4j.Logger;
import commoms.CommandIF;
import commoms.ProtocolConstants;

public class ServerThread extends Thread {
 
  private static final Logger log = Logger.getLogger("chat-server");
  // The Server that spawned us
  private Server server;
  // The Socket connected to our client
  private Socket socket;
  private Tracer tracer = new Tracer();

  // Constructor.
  public ServerThread(Server server, Socket socket) {
    // Save the parameters
    this.server = server;
    this.socket = socket;
    // Start up the thread
    start();
  }

  // This runs in a separate thread when start() is called in the
  // constructor.
  public void run() {
    String senderName = "";
    try {
      // Create a DataInputStream for communication; the client
      // is using a DataOutputStream to write to us
      ObjectInputStream din = new ObjectInputStream(socket.getInputStream());
      // Over and over, forever ...
      while (true) {
        // ... read the next message ...
        CommandIF comm = null;
        try {
          comm = (CommandIF)din.readObject();
        } catch (ClassNotFoundException e) {
          log.error("Class cast breaking up. Try some packaging stuff");
          e.printStackTrace();
        } catch (IOException ioe) {
          // seems like socket is reset
          // lets update list
          //server.removeConnection(senderName, socket);
           }
        // ... tell the world ...
        server.updatePeerList(comm, socket);
        /*
         * extraction
         */
        String message = comm.getMessage();
        System.out.println("Sending " + message);
        int i = message.indexOf(":");
        String pureMessage = message.substring(i);

        // String sock = socket.toString();
        InetAddress inet = socket.getInetAddress();
        String sock = inet.getHostAddress();
        senderName = message.substring(0, i).trim();
        ServerThread.verifyList(senderName);
        tracer.tracerDb(sock, comm.getMsgFrom(), pureMessage, comm.getRecipientNames());
        // ... and have the server send it to all clients
        if(comm.getCommand().equals(ProtocolConstants.COMMAND_SEND)) {
           if(comm.getRecipientNames().get(0).equals(ProtocolConstants.SEND_TO_ALL)) {
             server.sendToAll(comm);
           } else {
             // surely user is trying to send message but not to all peers
             //TODO see if message sending can be limited to selected peers.
             server.sendToAll(comm);
           }
        }
       
      }// while
    } catch (EOFException ie) {
      // This doesn't need an error message
    } catch (IOException ie) {
      ie.printStackTrace();
    } finally {
      // The connection is closed for one reason or another,
      // so have the server dealing with it
      server.removeConnection(senderName, socket);
    }
  }
 
  public static void verifyList(String senderName) {
    List<String> list = Server.getClientNames();
    if(!list.contains(senderName)) {
      list.add(senderName);
      Server.updateList = true;
    }
  }
 
}
TOP

Related Classes of server.ServerThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.