package pachalen.socketconnection.server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import pachalen.javadebug.JavaDebugTool;
import pachalen.socketconnection.MessageType;
import pachalen.socketconnection.SocketAct;
import pachalen.socketconnection.SocketMessage;
/**
* ��� ������ ������ ��Ÿ���� Ŭ����.<br>
* {@link ServerState}�� �����Ͽ���.<br>
* 2013. 6. 4. ����<br>
* ��õ����б� �Ƿ�IT���а� MSDL & Team Pachalen<br>
* @author �ּ���(Niklane)
* @version 1.0
*/
public class ServerWaitState implements ServerState{
private JavaDebugTool D;
SocketServer serv = SocketServer.INST;
public ServerWaitState(){
try {
D = new JavaDebugTool("ServerWaitState");
} catch (Exception e) {}
}
private class ServerHandler implements Runnable{
ServerSocket serverSocket;
public ServerHandler(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void run(){
try {
while(true){
Socket clientSocket = serverSocket.accept();
ClientHandler clientThread = new ClientHandler(clientSocket);
clientThread.start();
D.Debug("Ŭ���̾�Ʈ ����� IP" + clientSocket.getInetAddress());
}
} catch (IOException e) {
D.Error("Ŭ���̾�Ʈ ���� �� �̻��");
D.Error(e);
}
}
}
private class ClientHandler extends Thread{
String username;
Socket sock;
ObjectInputStream reader;
ObjectOutputStream writer;
public ClientHandler(Socket clientSocket){
sock = clientSocket;
try {
writer = new ObjectOutputStream(clientSocket.getOutputStream());
reader = new ObjectInputStream(clientSocket.getInputStream());
D.Debug("Ŭ���̾�Ʈ���� ��θ� �������ϴ�. IP" + sock.getInetAddress());
} catch (IOException e) {
D.Error("Ŭ���̾�Ʈ���� ������θ� ���µ��� �����߽��ϴ�.");
e.printStackTrace();
closeAll();
}
}
public void closeAll(){
try {
writer.close();
reader.close();
sock.close();
D.Error(username + ", IP" + sock.getInetAddress() + "�� ���ῡ ���� ��� ��ü�� �ݽ��ϴ�.");
this.join();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
D.Error(username + ", IP" + sock.getInetAddress() + "�� ���ῡ ���� ��ü�� �ݴ��� ���� ��");
e.printStackTrace();
}
}
public void run(){
SocketMessage message;
SocketMessage.MsgType type;
try{
while(true){
message = (SocketMessage)reader.readObject();
D.Debug(username + "���� ���� ���� ���� - " + message);
type = message.getMsgType();
switch(type){
case NO_ACT:
break;
case LOGIN:
username = serv.currentState.login(message, writer);
break;
case LOGOUT:
serv.currentState.logout(message);
break;
case CLIENT_MSG:
serv.currentState.act(message, writer);
break;
default:
D.Error("Ŭ���̾�Ʈ���� �� �� ���� ���� ����");
break;
}
}
}catch(Exception e){
if(e.toString().contains("Connection reset")){
D.Debug(username + ", IP" + sock.getInetAddress() + " �� ������ ����Ǿ����ϴ�.");
closeAll();
}
else{
D.Error(e);
e.printStackTrace();
}
}
}
}
@Override
public void setUp() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(serv.socket);
serv.threadConnectionWaiting = new Thread(new ServerHandler(serverSocket));
serv.threadConnectionWaiting.start();
serv.currentState = serv.serverRunState; //���º�ȯ
D.Debug("������ �۵��Ǿ����ϴ�.");
} catch (Exception e) {
D.Error("���� ���� �߿� �̻��");
D.Error(e);
}
}
@Override
public void setDown() {
D.Debug("������ ������϶��� ���� �� �����ϴ�.");
}
@Override
public void addAct(MessageType msg, SocketAct act) {
// TODO Auto-generated method stub
serv.MessageTypes.put(msg, act);
D.Debug(msg + "�� �ش��ϴ� �ൿ�� ������ �Ҵ�Ǿ����ϴ�. �ൿ �� : " + serv.MessageTypes.size());
}
@Override
public void removeAct(MessageType msg) {
// TODO Auto-generated method stub
serv.MessageTypes.remove(msg);
D.Debug(msg + "�� �ش��ϴ� �ൿ�� �������� ���ŵǾ����ϴ�. �ൿ �� : " + serv.MessageTypes.size());
}
@Override
public synchronized String login(SocketMessage message, ObjectOutputStream writer) {
D.Debug("������ ������϶��� �α��� �� �� �����ϴ�.");
return null;
}
@Override
public synchronized void logout(SocketMessage message) {
D.Debug("������ ������϶��� �αƿ� �� �� �����ϴ�.");
}
@Override
public synchronized void act(SocketMessage message, ObjectOutputStream writer) {
D.Debug("������ ������϶��� �۵��� �� �����ϴ�.");
}
}