forked from bboyairwreck/PieMessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
196 lines (155 loc) · 6.42 KB
/
Server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
/**
* Created by eric on 11/7/15.
*/
public class Server {
public static Server instance;
public PrintWriter osxOutgoingOutput;
public BufferedReader osxOutgoingInput;
public PrintWriter osxIncomingOutput;
public BufferedReader osxIncomingInput;
HashSet<ClientThread> setOfMobileOutgoingThreads;
HashSet<ClientThread> setOfMobileIncomingThreads;
HashSet<ClientThread> setOfOSXOutgoingThreads;
HashSet<ClientThread> setOfOSXIncomingThreads;
public static void main(String[] args) {
new Server();
}
public static Server getInstance() {
return instance;
}
public Server() {
if (instance == null) {
instance = this;
try {
ServerSocket sSocket = new ServerSocket(5000); // using port 5000 for our server socket
System.out.println("Server started at: " + new Date());
setOfMobileOutgoingThreads = new HashSet<ClientThread>();
setOfMobileIncomingThreads = new HashSet<ClientThread>();
setOfOSXOutgoingThreads = new HashSet<ClientThread>();
setOfOSXIncomingThreads = new HashSet<ClientThread>();
// loop that runs server functions
while(true) {
// Wait for a client to connect
Socket socket = sSocket.accept();
// Create a new custom thread to handle connection
ClientThread cT = new ClientThread(socket);
// Create thread and pass ID
Thread thread = new Thread(cT);
cT.setThread(thread);
// Start the thread!
thread.start();
}
} catch (IOException exception) {
System.out.println("Error: " + exception);
}
} else {
System.out.println("ERROR: You tried to create more than 1 Server");
}
}
public void setOsxOutgoingOutput(PrintWriter osxOutgoingOutput) {
System.out.println("Server - Setting osxOutgoingOutput");
this.osxOutgoingOutput = osxOutgoingOutput;
}
public void setOsxOutgoingInput(BufferedReader osxOutgoingInput) {
System.out.println("Server - Setting osxOutgoingInput");
this.osxOutgoingInput = osxOutgoingInput;
}
public void setOSXIncomingInput(BufferedReader osxIncomingInput) {
System.out.println("Server - Setting osxIncomingInput");
this.osxIncomingInput = osxIncomingInput;
}
public void setOsxIncomingOutput(PrintWriter osxIncomingOutput) {
System.out.println("Server - Setting osxIncomingOutput");
this.osxIncomingOutput = osxIncomingOutput;
}
public boolean hasOsxOutgoingOutput() {
return this.osxOutgoingOutput != null;
}
public boolean hasOsxOutgoingInput() {
return this.osxOutgoingInput != null;
}
public boolean hasOsxIncomingOutput() {
return this.osxIncomingOutput != null;
}
public boolean hasOsxIncomingInput() {
return this.osxIncomingInput != null;
}
public void addOSXOutgoingThread(ClientThread clientThread) {
this.setOfOSXOutgoingThreads.add(clientThread);
}
public void addOSXIncomingThread(ClientThread clientThread) {
this.setOfOSXIncomingThreads.add(clientThread);
}
public void removeOSXOutgoingThread(ClientThread clientThread) {
this.setOfOSXOutgoingThreads.remove(clientThread);
}
public void removeOSXIncomingThread(ClientThread clientThread) {
this.setOfOSXIncomingThreads.remove(clientThread);
}
public boolean notifyOSXOutgoingThread(JSONObject jsonObject) {
ArrayList<ClientThread> removeList = new ArrayList<>();
boolean didSend = false;
for (ClientThread osxThread : setOfOSXOutgoingThreads) {
if (osxThread.thread.isAlive()) {
osxThread.output.println(jsonObject.toString());
didSend = true;
} else {
removeList.add(osxThread);
}
}
for (ClientThread clientThread : removeList) {
this.setOfOSXOutgoingThreads.remove(clientThread);
}
return didSend;
}
public void addMobileOutgoingThread(ClientThread clientThread) {
this.setOfMobileOutgoingThreads.add(clientThread);
}
public void addMobileIncomingThread(ClientThread clientThread) {
this.setOfMobileIncomingThreads.add(clientThread);
}
public void removeMobileThread(ClientThread clientThread) {
this.setOfMobileOutgoingThreads.remove(clientThread);
}
public void notifyMobileOutgoingThreads(JSONObject jsonObject) {
ArrayList<ClientThread> removeList = new ArrayList<>();
for (ClientThread mobileThread : setOfMobileOutgoingThreads) {
if (mobileThread.thread.isAlive()) {
System.out.println("Mobile outgoing thread is alive, sending msg to client");
mobileThread.output.println(jsonObject.toString());
} else {
System.out.println("Mobile outgoing thread is dead");
removeList.add(mobileThread);
}
}
for (ClientThread clientThread : removeList) {
this.setOfMobileOutgoingThreads.remove(clientThread);
}
System.out.println("Finished return notifying of outgoing message to Mobile");
}
public void notifyMobileIncomingThreads(JSONObject jsonObject) {
ArrayList<ClientThread> removeList = new ArrayList<ClientThread>();
for (ClientThread mobileThread : setOfMobileIncomingThreads) {
if (mobileThread.thread.isAlive()) {
System.out.println("Mobile incoming thread is alive, sending msg to client");
mobileThread.output.println(jsonObject.toString());
} else {
System.out.println("Mobile incoming thread is dead");
removeList.add(mobileThread);
}
}
for (ClientThread clientThread : removeList) {
this.setOfMobileOutgoingThreads.remove(clientThread);
}
System.out.println("Finished notifying of incoming messages");
}
}