-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiClient.java
240 lines (149 loc) · 6.04 KB
/
MultiClient.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package client;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import tools.*;
public class MultiClient{
private final String SERVER_HOST = "localhost";
private final int SERVER_PORT = 40302;
private ServerSocket ss = null;
private int port;
private TimeInfo timeinfo;
private String clientId = null;
public static void main(String []args){
new MultiClient().run();
}
private void unsubscribe() throws Exception{
if ( clientId == null ){
System.err.println("You haven't subscribe time yet!");
return;
}
Socket csocket = new Socket( SERVER_HOST, SERVER_PORT );
PrintWriter out = new PrintWriter( csocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader( csocket.getInputStream() ) );
out.println("unsubscribe " + clientId);
String line;
if( ( line = in.readLine() ) != null &&
line.equals("OK") ){
out.close();
in.close();
csocket.close();
System.out.println("Time unsubscribed");
}else{
System.err.println("Failed to unsubscribe");
}
}
private void subscribe() throws Exception{
Socket csocket = new Socket( SERVER_HOST, SERVER_PORT );
this.port = csocket.getLocalPort(); //get local port, it will be used in the future
PrintWriter out = new PrintWriter( csocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader( csocket.getInputStream() ) );
out.println("subscribe");
String line;
if( ( line = in.readLine() ) != null ){
clientId = line; //get client id
out.close();
in.close();
csocket.close();
this.ss = new ServerSocket( this.port );
System.out.println("Time subscribed, client id is " + clientId);
}else{
throw new Exception("Failed to subscribe");
}
}
class SocketProcess implements Runnable{
private void getTime() throws Exception{
Socket s = ss.accept();
ObjectInputStream in = new ObjectInputStream( s.getInputStream() );
timeinfo = (TimeInfo)in.readObject();
System.out.println( timeinfo.time );
in.close();
s.close();
}
public void cast(){
if( timeinfo.point + 1 < timeinfo.addresses.size() )
timeinfo.point++;
else
timeinfo.point = -1; // no more client
if( timeinfo.addresses.size() > 0 && timeinfo.point != -1 ){
clientInfo ci;
InetAddress ip;
int port;
for(int i=timeinfo.point; i<timeinfo.addresses.size(); i++ ){
try{
ci = timeinfo.addresses.get(i); // get first client
ip = ci.getAddress();
port = ci.getPort();
Socket s = new Socket( ip, port );
ObjectOutputStream out = new ObjectOutputStream( s.getOutputStream() );
out.writeObject( timeinfo );
out.close();
s.close();
System.out.println("Time is sent to client " + i + " " + ip.getHostAddress()
+ ":" + port + "\n");
return;
}
catch(Exception e)
{
System.err.println("Fail to send time to client " + i );
if( timeinfo.point + 1 < timeinfo.addresses.size() )
timeinfo.point++;
continue;
}
}
System.err.println("Failed to send time to clients, will resend after");
}
else{ // no subscription
if( timeinfo.point == -1 )
System.out.println("I am last client");
}
System.out.println("");
}
public void run(){
try{
System.out.println("I am start...");
while(true){
this.getTime();
this.cast();
}
}catch(Exception e){
System.err.println("Client error: " + e.getMessage() );
System.exit(-1);
}
}
}
public void run(){
try{
/*
InputStream in = System.in;
FileInputStream fi = (FileInputStream) in;
FileChannel fc = fi.getChannel();
*/
this.subscribe();
Thread t = new Thread( new SocketProcess() );
t.start();
while(true){
Console cons;
String cmd;
if( ( cons = System.console() ) != null &&
( cmd = cons.readLine() ) != null ){
if ( cmd.equals("ussb") ){
this.unsubscribe();
System.out.println("Client is exiting...");
System.exit(0);
}
else{
System.out.println("Unknown cmd");
}
}
//this.getTime();
//this.cast();
}
}catch(Exception e){
System.err.println("Client error: " + e.getMessage() );
System.exit(-1);
}
}
}