-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueManager.java
105 lines (102 loc) · 3.92 KB
/
QueueManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.rmi.RemoteException;
import java.util.*;
public class QueueManager {
private static QueueManager manager = new QueueManager();
private Hashtable<String, ConversationManager> activeUsers;
private final List<Set<ConversationManager>> rooms;
private List<ConversationManager> queue;
public RoomTable roomTable;
private QueueThread queueThread;
public void ExitRoom(ConversationManager manager){
int count = 0;
for(Set<ConversationManager> room: rooms) {
if(room.contains(manager)){
roomTable.DeleteRoom(manager.GetId());
Iterator<ConversationManager> iterator = room.iterator();
while(iterator.hasNext()) {
ConversationManager cv = iterator.next();
cv.setConversant(null);
activeUsers.remove(cv.GetId());
}
rooms.remove(count);
break;
}
count++;
}
}
public static QueueManager GetInstance(){
return manager;
}
public void AddToQueue(ConversationManager manager) throws Exception{
if(manager.IsInRoom()){
System.out.println(manager.GetId() + " is already in a room.");
throw new Exception("User is already in a room");
}
if(queue.contains(manager)){
System.out.println(manager.GetId() + " is already in the queue.");
throw new Exception("User is already in the queue");
}
try {
manager._listener.EnterQueueNotice();
queue.add(manager);
queueThread.Wakeup();
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
private QueueManager(){
queue = new ArrayList<ConversationManager>();
activeUsers = new Hashtable<String, ConversationManager>();
rooms = new ArrayList<Set<ConversationManager>>();
queueThread = new QueueThread();
queueThread.start();
}
private class QueueThread extends Thread{
public synchronized void run(){
while(true){
if(queue.size()>1){
ConversationManager conversant = queue.get(0);
ConversationManager manager = queue.get(1);
try {
Set<ConversationManager> room = new HashSet<ConversationManager>();
try{
conversant.AcceptQueue();
}catch(Exception e){
queue.remove(conversant);
continue;
}
try{
manager.AcceptQueue();
}catch(Exception e){
queue.remove(manager);
continue;
}
queue.remove(conversant);
queue.remove(manager);
room.add(manager);
room.add(conversant);
rooms.add(room);
activeUsers.put(manager.GetId(), manager);
activeUsers.put(conversant.GetId(), conversant);
manager.setConversant(conversant);
conversant.setConversant(manager);
roomTable.PushRoom(conversant.GetId(), manager.GetId());
System.out.println("Room is ready for Id "+manager.GetId()+" and Id "+conversant.GetId());
} catch (Exception e) {
System.out.println("error: " + e.getMessage());
ExitRoom(manager);
}
}else{
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public synchronized void Wakeup(){
this.notify();
}
}
}