package server.model;
import java.awt.HeadlessException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import server.ServerPanel;
public class ServerSocketUtil {
public static boolean isExit = false;
static ServerSocket server = null;
public static void startServer(int port) throws IOException {
server = new ServerSocket(port);
ServerSocketThread serverThread = new ServerSocketThread(server);
serverThread.start();
}
public static void closeServer() throws IOException {
if(ServerSocketThread.socket != null) ServerSocketThread.socket.close();
if(server == null) return;
server.close();
}
}
class ServerSocketThread extends Thread {
ServerSocket server = null;
static Socket socket = null;
public ServerSocketThread(ServerSocket server) {
this.server = server;
}
@Override
public void run() {
while(true) {
try {
socket = server.accept();
new Thread() {
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
while(true) {
NetMsg msg = (NetMsg) ois.readObject();
System.out.println("hhhhhhhhhhhh"+msg.getContent());
switch(msg.getType()) {
case NetMsg.TYPE_FIND_GOODS: {
String goodsId = (String)msg.getContent();
Goods goods = GoodsMgr.findGoods(goodsId);
NetMsg tempMsg = new NetMsg(NetMsg.TYPE_FIND_GOODS_REPLY,goods);
oos.writeObject(tempMsg);
oos.flush();
break;
}
case NetMsg.TYPE_LOGIN: {
String tempStr = (String) msg.getContent();
String[] str = tempStr.split(",");
String cushierId = str[0];
String cushierPwd = str[1];
Cushier tempCushier = new Cushier(cushierId);
int k = CushierMgr.getCushierList().indexOf(tempCushier);
NetMsg tempMsg3 = null;
if(k == -1) {
//System.out.println("未找到用户!");
tempMsg3 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, "wrongid");
oos.writeObject(tempMsg3);
oos.flush();
continue;
}
Cushier cushier = CushierMgr.getCushierList().get(k);
if(cushier.getPassword().equals(cushierPwd)) {
NetMsg tempMsg2 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, cushier);
ServerPanel.count++;
ServerPanel.labCushierCount.setText(ServerPanel.count+"人在线");
oos.writeObject(tempMsg2);
oos.flush();
} else {
tempMsg3 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, "wrongpwd");
oos.writeObject(tempMsg3);
oos.flush();
}
break;
}
case NetMsg.TYPE_PAY:
double money = (Double)msg.getContent();
//System.out.println("money"+money);
MoneyMgr.add(money);
String date = MoneyMgr.sdf.format(new Date())+"";
if(MoneyMgr.getMoneyMap().size()>0 && MoneyMgr.getMoneyMap().get(MoneyMgr.sdf.format(new Date())) != null) {
ServerPanel.labDayTurnover.setText("¥"+(MoneyMgr.getMoneyMap().get(date))+"元");
} else {
ServerPanel.labDayTurnover.setText("¥ 0 元");
}
break;
}
}
} catch (HeadlessException e) {
e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
//一个客户端退出系统时,让主界面在线的人数减一
if(ServerPanel.count>0) {
ServerPanel.count--;
ServerPanel.labCushierCount.setText(ServerPanel.count+"人在线");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}.start();
} catch (IOException e) {
//e.printStackTrace();
//System.out.println("服务已停止!");
ServerPanel.labServerInfo.setText("未启动");
return;
}
}
}
}