-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
123 lines (106 loc) · 2.91 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class FileSender implements Layer{
private FileSenderThread sender;
private StreamLayer sublayer;
class FileSenderThread extends Thread{
private String filename;
private StreamLayer sublayer;
public FileSenderThread(String filename, StreamLayer sublayer) {
this.filename = filename;
this.sublayer = sublayer;
}
public void run(){
try{
sublayer.send(("SEND "+filename).getBytes(), false);
Scanner sc = new Scanner(new File(filename));
while (sc.hasNextLine()){
sublayer.send(sc.nextLine().getBytes(), false);
}
sublayer.send("**CLOSE**".getBytes(), true);
sc.close();
} catch(FileNotFoundException e){
System.err.println(e);
sublayer.send("FILE NOT FOUND".getBytes(), true);
return;
}
}
}
public void receive(byte[] payload, String host){
if (sender == null){
String message = new String(payload);
if (message.startsWith("GET")){
String filename = message.substring(4);
this.sender = new FileSenderThread(filename, this.sublayer);
this.sender.start();
}
}
}
public FileSender(StreamLayer sublayer){
this.sender = null;
this.sublayer = sublayer;
}
public void send(byte[] payload){
throw new UnsupportedOperationException("don't use to send");
}
public void deliverTo(Layer above){
throw new UnsupportedOperationException("don't support any Layer above");
}
public void close(){
//NOTHING
}
}
class NewStreamsHandler extends Thread{
ConnectedLayer cl;
public NewStreamsHandler(ConnectedLayer cl){
this.cl = cl;
}
public void run(){
while (true){
try{
Integer pendingstream = cl.accept();
if (pendingstream != null){
StreamLayer sl = new StreamLayer(cl,pendingstream);
FileSender fs = new FileSender(sl);
sl.deliverTo(fs);
cl.register(sl,pendingstream);
}
} catch (InterruptedException e){
break;
}
}
}
}
public class Server {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("syntax : java Server myPort");
return;
}
if (GroundLayer.start(Integer.parseInt(args[0]))){
DispatchLayer.start();
List<NewStreamsHandler> handlers = new ArrayList<NewStreamsHandler>();
while (true){
try{
PendingConnection pc = DispatchLayer.accept();
if (pc != null){
String[] hostaddress = pc.host.split(":"); //Separate IP address from port num
String host = hostaddress[0].substring(1);
int port = Integer.parseInt(hostaddress[1]);
ConnectedLayer cl = new ConnectedLayer(host, port, pc.id);
NewStreamsHandler nsh = new NewStreamsHandler(cl);
nsh.start();
handlers.add(nsh);
cl.receive(pc.payload, pc.host);
DispatchLayer.register(cl, pc.id);
}
} catch (InterruptedException e){
break;
}
}
}
}
}