package com.netQueue;
//服务器
import java.io.IOException;
import java.net.*;
import com.queue.Queue;
import java.util.*;
public class QueueServer implements Runnable {
private Queue vipQueue,normQueue;
private DatagramSocket socket;
public static final SocketAddress ADDRESS =
new InetSocketAddress("127.0.0.1",8888);
//------------对地址管理--广播----------------
//对三中客户端的地址统一管理,目的是为了广播(群发)
//key "客户端种类,客户id",value "客户端地址"
private Map<String,SocketAddress> orderMap =
new HashMap<String,SocketAddress>();
private Map<String,SocketAddress> counterMap =
new HashMap<String,SocketAddress>();
private Map<String,SocketAddress> mainMap =
new HashMap<String,SocketAddress>();
public QueueServer(){
vipQueue = new Queue();
normQueue = new Queue();
try{
socket = new DatagramSocket(8888);
}catch(Exception e){}
(new Thread(this)).start();
}
//对取号器的处理
public void orderMethod(String sub[]) throws Exception{
//操作 队列"vip","norm"
if("vip".equals(sub[2])){
vipQueue.add();
int value = vipQueue.getLastValue();
int size = vipQueue.size();
String sendStr = "你是第"+value+"号vip客户,前面有"+(size-1)+"顾客等待";
sendStr(sendStr,orderMap.get(sub[0]+","+sub[1]),socket);
//向所有的vip柜台群发信息
if(size==1){
Set<String> keys = counterMap.keySet();
for(String key:keys){
String subS[] = key.split(",");
if("vip".equals(sub[2])){
sendStr("您是否可以为value号vip顾客办理业务",
counterMap.get(key),socket);
}
}
}
}
if("norm".equals(sub[2])){
normQueue.add();
int value = normQueue.getLastValue();
int size = normQueue.size();
String sendStr = "你是第"+value+"norm客户,前面有"+(size-1)+"等待";
sendStr(sendStr,orderMap.get(sub[0]+","+sub[1]),socket);
//向普通柜台群发信息
if(size==1){
Set<String> keys = counterMap.keySet();
for(String key:keys){
String subS[] = key.split(",");
if("norm".equals(sub[2])){
sendStr("您是否可以为value号norm顾客办理业务",
counterMap.get(key),socket);
}
}
}
}
}
//柜台处理
public void counterMethod(String sub[]) throws Exception{
if("vip".equals(sub[2])){
String sendStr="欢迎光临本行!";
int size = vipQueue.size();
if(size==0){
Set<Map.Entry<String, SocketAddress>> entries = mainMap.entrySet();
for(Map.Entry<String, SocketAddress> entry:entries){
sendStr(sendStr,entry.getValue(),socket);
}
//向当前柜台群发信息
sendStr = "欢迎光临";
sendStr(sendStr,
counterMap.get(sub[0]+","+sub[1]+","+sub[2]),
socket);
return;
}
//抽象出去.*******************8
int value = vipQueue.getLastValue();
vipQueue.remove();
//向主显示屏群发信息
sendStr = "请"+value+"号vip客户到"+sub[1]+"号vip柜台办理业务!";
Set<Map.Entry<String, SocketAddress>> entries = mainMap.entrySet();
for(Map.Entry<String, SocketAddress> entry:entries){
sendStr(sendStr,entry.getValue(),socket);
}
//向当前柜台群发信息
sendStr = "本柜台正在为"+value+"号vip 客户办理业务";
sendStr(sendStr,
counterMap.get(sub[0]+","+sub[1]+","+sub[2]),
socket);
//****************************没完善
}
if("norm".equals(sub[2])){
int value = normQueue.getLastValue();
normQueue.remove();
//向主显示屏群发信息
String sendStr = "请"+value+"号norm客户到"+sub[1]+"号norm柜台办理业务!";
Set<Map.Entry<String, SocketAddress>> entries =
mainMap.entrySet();
for(Map.Entry<String, SocketAddress> entry:entries){
sendStr(sendStr,entry.getValue(),socket);
}
//向当前柜台群发信息
sendStr = "本柜台正在为"+value+"号norm 客户办理业务";
sendStr(sendStr,
counterMap.get(sub[0]+","+sub[1]+","+sub[2]),
socket);
//****************************没完善
}
}
//***************flag******************
//服务器启动
public void start(){}
//服务器终止
public void stop(){}
//***************flag*****************
public void run(){
while(true){//处理******************************
//接受
try {
byte[] inArray = new byte[50];
DatagramPacket inPacket = new DatagramPacket(
inArray,0,inArray.length);
socket.receive(inPacket);
String inStr = new String(inArray,0,inPacket.getLength());
//对字符串处理-->分解
String sub[] =inStr.split(",");
String key = sub[0]+","+sub[1];
//管理地址
SocketAddress addressValue = inPacket.getSocketAddress();
if(sub.length==3 && "order".equals(sub[0])){
orderMap.put(key, addressValue);
try {
orderMethod(sub);
} catch (Exception e) {
e.printStackTrace();
}
}
if(sub.length==3 && "counter".equals(sub[0])){
//"counter,id,vip"用这个做key,
key = inStr;
counterMap.put(key, addressValue);
try {
counterMethod(sub);
} catch (Exception e) {
e.printStackTrace();
}
}
if("main".equals(sub[0])){
mainMap.put(key, addressValue);
}
//test
System.out.println(orderMap+"/ "+counterMap+"/"+mainMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//发送str 到address
public static void sendStr(String sendStr,SocketAddress address,
DatagramSocket socket) throws Exception
{
byte outArray[] = sendStr.getBytes();
DatagramPacket outPacket =
new DatagramPacket(
outArray,0,outArray.length, address);
socket.send(outPacket);
}
public static void main(String[] args) {
new QueueServer();
}
}