-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServer.java
218 lines (190 loc) · 6.71 KB
/
ChatServer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PushbackInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;
public class ChatServer {
public static final int portNum = Config.getAsInt("ServerPortNum");
// activeSenders is the list of clients that are currently active.
private Set activeSenders = Collections.synchronizedSet(new HashSet());
public ChatServer() {
// This constructor never returns, unless there is an error.
try{
ServerSocket ss;
ss = new ServerSocket(portNum);
for(;;){
// wait for a new client to connect, then hook it up properly
Socket sock = ss.accept();
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
System.err.println("Got connection");
SenderThread sender = new SenderThread(out);
new ReceiverThread(in, sender);
}
}catch(IOException x){
x.printStackTrace();
}
}
public static void main(String[] argv){
new ChatServer();
}
class SenderThread extends Thread {
// forwards messages to a client
// messages are queued when somebody calls queueForSending
// we take them from the queue and send them along
private OutputStream out;
private Queue queue;
SenderThread(OutputStream outStream) {
out = outStream;
queue = new Queue();
activeSenders.add(this);
start();
}
public void queueForSending(byte[] message){
// Queue a message, to be sent as soon as possible.
// We queue messages, rather than sending them immediately, because
// sending immediately would cause us to block, if the client
// had fallen behind in processing his incoming messages. If we
// blocked, the processing of incoming messages would be frozen,
// which would be Very Bad. By queueing messages, we can ensure that
// the processing of incoming messages never stalls, no matter how
// badly clients behave.
queue.put(message);
}
public void run() {
// suck messages out of the queue and send them out
try{
for(;;){
Object o = queue.get();
byte[] barr = (byte[])o;
out.write(barr);
out.flush();
}
}catch(IOException x){
// unexpected exception -- stop relaying messages
x.printStackTrace();
try{
out.close();
}catch(IOException x2){}
}
activeSenders.remove(this);
}
}
class ReceiverThread extends Thread {
// receives messages from a client, and forwards them to everybody else
public byte[] trim(byte[] bytes)
{
int i = bytes.length - 1;
while (i >= 0 && bytes[i] == 0)
{
--i;
}
return Arrays.copyOf(bytes, i + 1);
}
private InputStream in;
private SenderThread me;
private String userName;
private byte[] userNameBytes = null;
ReceiverThread(InputStream inStream, SenderThread mySenderThread) {
in = inStream;
me = mySenderThread;
start();
}
public void run() {
// get first line, which is the client's username
ByteArrayOutputStream baos = getOneLine(false);
byte[] baosbuf = baos.toByteArray();
byte[] key = InsecureSharedValue.getValue();
BlockCipher bc = new BlockCipher(key);
byte[] outArr = new byte[256];
for(int j = 0; j < Math.ceil((baosbuf.length - 1) / 8.0); j++)
bc.decrypt(baosbuf, j * 8, outArr, j * 8);
byte[] trimed = trim(outArr);
String name = new String(trimed);
//name = name.substring(0, name.length()-1); // trim trailing carriage return
userName = name;
name = "[" + name + "] ";
userNameBytes = name.getBytes();
//System.err.println(new String(userNameBytes));
// get subsequent lines, and sent them to the other clients
for(;;){
baos = getOneLine(true);
sendToOthers(baos);
}
}
private ByteArrayOutputStream getOneLine(boolean prependUserName) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// read in a message, terminated by carriage-return
// buffer the message in baos, until we see EOF or carriage-return
// return the message we got
// if prependUserName is true, then prepend the username of the
// sender to the message before we return it
try{
byte[] ct = new byte[256];
//if(prependUserName) baos.write(userNameBytes);
int c;
int i = 0;
do{
c = in.read();
if(c == -1){
// got EOF -- return what we have, then quit
return(baos);
}
if (!prependUserName) baos.write(c);
if (c != '\n'){
ct[i++] = (byte)c;
//System.out.print(i + ":" + (byte)c +",");
}
}while(c != '\n');
//System.out.println();
// return what we have -- note: this includes a final CR
if (prependUserName) {
byte[] key = InsecureSharedValue.getValue();
BlockCipher bc = new BlockCipher(key);
byte[] outArr = new byte[256];
for(int j = 0; j < Math.ceil(i / 8.0); j++)
bc.decrypt(ct, j * 8, outArr, j * 8);
byte[] trimed = trim(outArr);
int len8 = (int) (Math.ceil((userNameBytes.length + trimed.length)/8.0) * 8);
byte[] msg = new byte[len8];
System.arraycopy(userNameBytes, 0, msg, 0, userNameBytes.length);
System.arraycopy(trimed, 0, msg, userNameBytes.length, trimed.length);
//System.out.println(new String(msg));
byte[] buf = new byte[512];
for(int j = 0; j < len8 / 8; j++)
bc.encrypt(msg, j * 8, buf, j * 8);
byte[] tmp = trim(buf);
baos.write(tmp);
baos.write('\n');
}
return(baos);
}catch(IOException x){
// return what we have, then quit
return(baos);
}
}
// stArr is a dummy variable, used to make toArray happy below
private final SenderThread[] stArr = new SenderThread[1];
private void sendToOthers(ByteArrayOutputStream baos) {
// extract the contents of baos, and queue them for sending to all
// other clients (but not to ourself);
// also, reset baos so it is empty and can be reused
byte[] message = baos.toByteArray();
baos.reset();
SenderThread[] guys = (SenderThread[])(activeSenders.toArray(stArr));
for(int i=0; i<guys.length; ++i){
SenderThread st = guys[i];
if(st != me) st.queueForSending(message);
}
}
}
}