-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootStrapServer.java
More file actions
190 lines (151 loc) · 4.52 KB
/
BootStrapServer.java
File metadata and controls
190 lines (151 loc) · 4.52 KB
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
class WorkerThread implements Runnable {
private Socket client = null;
InputStreamReader ipReader = null;
BufferedReader input = null;
PrintStream output = null;
String entryhost;
public static HashMap<String, String> users = new HashMap<String, String>();;
public WorkerThread(Socket client, String entryPoint) {
this.client = client;
this.entryhost = entryPoint;
//users = new HashMap<String, String>();
}
/**
* Get Input Output Streams from client socket
*
* @throws IOException
*/
void openIOStreams() throws IOException {
ipReader = new InputStreamReader(client.getInputStream());
input = new BufferedReader(ipReader);
output = new PrintStream(client.getOutputStream(), true);
}
/**
* Closes input and output streams.
*
* @throws IOException
*/
void closeIOStreams() throws IOException {
input.close();
output.close();
}
public void run() {
try {
openIOStreams();
System.out.println("before request");
String request = input.readLine();
System.out.println("after request "+ request);
if (request.equals("join")) {
int port = Integer.parseInt(input.readLine());
if (entryhost == null) {
String[] host = client.getRemoteSocketAddress().toString()
.split(":");
String ip = host[0].substring(1);
BootStrapServer.entryPoint = ip + ":"
+ String.valueOf(port);
output.println("owner");
} else {
output.println(entryhost);
System.out.println("Sent out " + entryhost + " for entry");
}
}
else {
String host[] = request.split(":");
String newEntry = host[0] + ":" + host[1];
String c = client.getRemoteSocketAddress().toString()
.split(":")[0];
if (entryhost.split(":")[0].equalsIgnoreCase(c.substring(1))) {
System.out.println("New entry is " + newEntry);
BootStrapServer.setEntry(newEntry);
}
}
closeIOStreams();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
public class BootStrapServer implements Runnable {
private int port = 8085;
private ServerSocket serverSocket = null;
private Thread currentThread = null;
public static volatile String entryPoint = null;
public BootStrapServer(int port) throws IOException {
this.port = port;
serverSocket = new ServerSocket(this.port);
}
public static void setEntry(String entry) {
entryPoint = entry;
}
@Override
public void run() {
while (true) {
Socket client = null;
try {
// Accept peer connection and assign to socket
client = serverSocket.accept();
InputStreamReader ipReader = new InputStreamReader(
client.getInputStream());
BufferedReader input = new BufferedReader(ipReader);
PrintStream output = new PrintStream(client.getOutputStream());
String command = input.readLine();
System.out.println("Command is :" + command);
if (command.equalsIgnoreCase("authenticate")) {
while (!checkUser(input, output));
}
System.out.println("Entry point iiiisss: " + entryPoint);
// Make new Worker for current client and start a thread for it.
new Thread(new WorkerThread(client, entryPoint)).start();
} catch (IOException e) {
System.out.println("Error accepting connection.");
}
}
}
private boolean checkUser(BufferedReader input, PrintStream output)
throws IOException {
String command = input.readLine();
String username = input.readLine();
String password = input.readLine();
if (command.equalsIgnoreCase("authenticate")) {
if (WorkerThread.users.containsKey(username)) {
if (WorkerThread.users.get(username).equalsIgnoreCase(password)) {
output.println("loginsucc");
return true;
} else {
output.println("wrongpass");
return false;
}
} else {
output.println("wronguser");
return false;
}
} else {
WorkerThread.users.put(username, password);
output.println("signupsucc");
return true;
}
}
public static void main(String[] args) throws Exception {
// Get port from command line.
int port = Integer.parseInt(args[0]);
//int port = 3252;
// int port = 9998;
// Initialize server to listen to port.
BootStrapServer server = new BootStrapServer(port);
server.run();
}
}