-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCvsLbsKg.java
138 lines (112 loc) · 4.44 KB
/
CvsLbsKg.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
import java.net.Socket;
import java.net.ServerSocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CvsLbsKg {
public static int port;
public static void process (Socket clientSocket) throws IOException {
// open up IO streams
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
/* Write a welcome message to the client */
out.println("Welcome to the pounds(lbs) to kilogram(kg) conversion server!");
/* read and print the client's request */
// readLine() blocks until the server receives a new line from client
String userInput;
if ((userInput = in.readLine()) == null) {
System.out.println("Error reading message");
out.close();
in.close();
clientSocket.close();
return;
}
System.out.println("Received message: " + userInput);
//split userInput to individual items
String[] tokens = userInput.split(" ");
if(tokens[0].equals("check")){
out.println("ack");
out.close();
in.close();
clientSocket.close();
return;
}
//convert function
Double msg;
msg = ConvResult(tokens[0], tokens[2]);
out.println(msg.toString());
// close IO streams, then socket
out.close();
in.close();
clientSocket.close();
}
public static double ConvResult (String s, String amt) {
double result;
result = 0;
if (s.equals("lbs")){
result = Double.parseDouble(amt) / 2.2;
}else if (s.equals("kg")){
result = Double.parseDouble(amt) * 2.2;
}
return result;
}
public static void main(String[] args) throws Exception {
//check if argument length is invalid
if(args.length != 1) {
System.err.println("Usage: java ConvServer port");
}
// create socket
port = Integer.parseInt(args[0]);
ServerSocket serverSocket = new ServerSocket(port);
//this init() is to tell discoveryServer I launched
init(port);
System.err.println("Started server on port " + port);
// wait for connections, and process
try {
while(true) {
// a "blocking" call which waits until a connection is requested
Socket clientSocket = serverSocket.accept();
System.err.println("\nAccepted connection from client");
process(clientSocket);
}
}catch (IOException e) {
System.err.println("Connection Error");
}
System.exit(0);
}
public static void init(int port) throws Exception{
Socket socketDiscovery = new Socket("127.0.0.1", 11111);
BufferedReader in = new BufferedReader(new InputStreamReader(socketDiscovery.getInputStream()));
PrintWriter out = new PrintWriter(socketDiscovery.getOutputStream(), true);
out.println("add" + " " + "lbs" + " " + "kg" + " " + "127.0.0.1" + " " + String.valueOf(port));
String temp = in.readLine();
temp = in.readLine();
in.close();
out.close();
socketDiscovery.close();
attachShutDownHook();
}
public static void attachShutDownHook() throws Exception{
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try{
Socket socketDiscovery = new Socket("127.0.0.1", 11111);
BufferedReader in = new BufferedReader(new InputStreamReader(socketDiscovery.getInputStream()));
PrintWriter out = new PrintWriter(socketDiscovery.getOutputStream(), true);
out.println("remove" + " " + "127.0.0.1" + " " + String.valueOf(port));
String temp = in.readLine();
temp = in.readLine();
in.close();
out.close();
socketDiscovery.close();
}
catch(Exception e){
System.out.println("connection to discovery server failed!");
}
}
});
}
}