-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
109 lines (93 loc) · 3.79 KB
/
Server.java
File metadata and controls
109 lines (93 loc) · 3.79 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* A simple filer server implementation using Sockets. For the assignment,
* implement the functions in `FileRequestHandler.java`. Test your
* implementation with telnet and with your web browser.
*/
public class Server {
private final Path documentRoot;
private final int port;
public Server(int port) throws FileNotFoundException {
this.documentRoot = Paths.get("www-root");
this.port = port;
if (!(Files.exists(documentRoot) &&
Files.isDirectory(documentRoot))) {
throw new FileNotFoundException(
"Document root does not exist or is not a directory."
);
}
}
/**
* Start a passive socket and wait for a connection.
*
* This is described in detail in Networks, Protocols, Services, WT:II-61
* Implementation based on Networks, Protocols, Services, WT:II-63
*/
public void startListening() throws IOException {
// Start passive socket on the server (step 1a)
ServerSocket passiveSocket = new ServerSocket(port);
System.out.println("Server started on port " + port + ".");
// Create a handler that processes any request sent by the client.
FileRequestHandler handler = new FileRequestHandler(documentRoot);
// The passive socket is now listening for an incoming request (step 1b)
boolean listen = true;
while (listen) {
// When a client connects, a new connection socket regarding the
// client is initialized by the server (step 3b)
Socket connectionSocket = passiveSocket.accept();
handleConnection(handler, connectionSocket);
}
passiveSocket.close();
}
/**
* Handle a new connection by retrieving a request from the client which
* will then be processed by a request handler.
*
* Implementation based on Networks, Protocols, Services, WT:II-64
*/
public void handleConnection(FileRequestHandler handler,
Socket connectionSocket) throws IOException {
OutputStream responseStream = connectionSocket.getOutputStream();
// From the connection socket, get the input stream and create a
// buffered reader. BufferedReader implements `readLine()` which
// allows entering the request on the client side.
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())
);
// Wait until the next line reaches the stream (i.e. the user hit
// return and completed their request).
String requestString = bufferedReader.readLine();
// Use the socket IP and try to determine a host name
// E.g. on my system that would be `RAUMSTATION`.
String clientHost = connectionSocket.getInetAddress().getHostName();
System.out.println(clientHost + ": " + requestString);
// The request was received and can now be processed.
handler.handle(requestString, responseStream);
bufferedReader.close();
responseStream.close();
}
/**
* Start the file server with a provided document root and port.
*/
public static void main(String[] args) throws IOException {
int port = 8080;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
Server server = new Server(port);
try {
server.startListening();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}