package connection;
import handlers.HandlerSet;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.LinkedList;
import main.Config;
import common.ClientServerProtocol;
import common.connection.AbstractCommunicationSet;
import common.connection.AbstractConnection;
import common.connection.ConnectionWriterInterface;
import common.handlers.AbstractHandlerSet;
import static main.ServerMain.getFileManager;
import static main.ServerMain.getConnectionManager;
import static main.ServerMain.getQueryManager;
public class ClientConnection extends AbstractConnection
{
private static final HandlerSet HANDLER_SET = new HandlerSet();
private int clientId;
private volatile int port;
private volatile String nickname;
private static final int MAXIMUM_NUMBER_OF_QUERIES = 10;
private LinkedList<Integer> queries;
public ClientConnection(AbstractCommunicationSet source) throws IOException
{
super(source);
port = ClientServerProtocol.DEFAULT_CLIENT_PORT;
nickname = "Guest";
queries = new LinkedList<Integer>();
}
public void initializeConnection() throws IOException
{
clientId = getConnectionManager().add(this);
askFileList();
if (!Config.server.enableChat)
{
sendChatMessage(Config.server.name,
"The global chat channel is disabled on this server");
}
}
private void askFileList() throws IOException
{
int protocolId = ClientServerProtocol.PROTOCOL_FILES;
int opcode = ClientServerProtocol.Files.OP_GET_LIST;
send(protocolId, opcode);
}
public void finalizeConnection()
{
clearQueries();
getFileManager().removeSharer(clientId);
getConnectionManager().remove(clientId);
}
private void clearQueries()
{
for (int queryId : queries)
{
getQueryManager().removeQuery(queryId);
}
}
protected AbstractHandlerSet getHandlerSet()
{
return HANDLER_SET;
}
protected void processMessage(int protocolId, int opcode) throws IOException,
ClassNotFoundException
{
HandlerSet.handlers[protocolId][opcode].execute(this, is, os);
os.flush();
}
public int getClientId()
{
return clientId;
}
public int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public String getNickname()
{
return nickname;
}
public void setNickname(String newName)
{
this.nickname = newName;
}
public int addQuery(int cookie, Collection<String> keywords)
{
if (queries.size() >= MAXIMUM_NUMBER_OF_QUERIES)
{
getQueryManager().removeQuery(queries.removeLast());
}
int queryId = getQueryManager().addQuery(clientId, keywords, cookie);
queries.add(queryId);
return queryId;
}
public void sendChatMessage(final String sender, final String message)
{
int protocolId = ClientServerProtocol.PROTOCOL_CHAT;
int opcode = ClientServerProtocol.Chat.OP_MESSAGE_POSTED;
send(protocolId, opcode, new ConnectionWriterInterface()
{
public void run(ObjectOutputStream os) throws IOException
{
os.writeUTF(sender);
os.writeUTF(message);
}
});
}
public void sendKeywords(final int cookie, final Collection<String> keywords)
{
int protocolId = ClientServerProtocol.PROTOCOL_FILES;
int opcode = ClientServerProtocol.Files.OP_KEYWORDS;
send(protocolId, opcode, new ConnectionWriterInterface()
{
public void run(ObjectOutputStream os) throws IOException
{
os.writeInt(cookie);
os.writeObject(keywords);
}
});
}
public void sendSearchResults(final int cookie, final Object results)
{
int protocolId = ClientServerProtocol.PROTOCOL_FILES;
int opcode = ClientServerProtocol.Files.OP_RESULTS;
send(protocolId, opcode, new ConnectionWriterInterface()
{
public void run(ObjectOutputStream os) throws IOException
{
os.writeInt(cookie);
os.writeObject(results);
}
});
}
}