/*- ServerThread.java ---------------------------------------------+
| |
| Copyright (C) 2008-2009 Prateek Jain, pchat |
| prateekjainaa@gmail.com |
| http://prateekjainaa.blogspot.com |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| A copy of the GNU General Public License may be found in the |
| installation directory named "GNUGPL.txt" |
| |
+-----------------------------------------------------------------+
*/
package server;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.CommandImpl;
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();
private ObjectInputStream din;
/**
* @return the din
*/
public ObjectInputStream getDin() {
return din;
}
// 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 ObjectInputStream for communication; the client
// is using a ObjectOutputStream to write to us
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);
String pureMessage = message;
InetAddress inet = socket.getInetAddress();
String sock = inet.getHostAddress();
senderName = comm.getMsgFrom();
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
if (!comm.isSendingFile()) {
server.sendToAll(comm);
} else {
// user is trying to send file
CommandIF fileCommand = handleFileRequest(comm);
server.sendToAll(fileCommand);
}
}
} else if (comm.getCommand().equals(
ProtocolConstants.DELETE_FILE)) {
deleteSavedFile(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;
}
}
CommandIF handleFileRequest(CommandIF comm) {
boolean flag = false;
byte[] b = comm.getFile();
String fileName = comm.getFileName();
String docRoot = Server.documentRoot;
File file = new File(docRoot + "/chat/Upload/" + comm.getMsgFrom()
+ fileName);
try {
FileOutputStream out = new FileOutputStream(file);
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
CommandImpl impl = new CommandImpl();
impl.setCommand(ProtocolConstants.DOWNLOAD_FILE);
impl.setPrivateMessage(true);
impl.setConfirmation(false);
impl.setFileName(fileName);
impl.setMsgFrom(comm.getMsgFrom());
impl.setPeerListChanged(false);
impl.setRecipientNames(comm.getRecipientNames());
impl.setSendingFile(false);
return impl;
}
boolean deleteSavedFile(CommandIF comm) {
boolean flag = true;
String filePath = Server.documentRoot + "/chat/Upload/"
+ comm.getFileName();
try {
File file = new File(filePath);
file.delete();
} catch (Exception e) {
flag = false;
log.error("unable to delete file " + filePath);
e.printStackTrace();
}
return flag;
}
}