-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentServer.java
152 lines (132 loc) · 4.68 KB
/
ContentServer.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
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.math.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import xml.XMLCreator;
import xml.Packet;
class ContentServer {
public static String id = null;
public static String inputfile = null;
public static int lamport_timestamp = 0;
public static int retry_count = 0;
public static String input = null;
public static String servername = null;
public static int port = 0;
public static void main(String[] args) {
//ContentServer input handling
Scanner input = new Scanner(System.in);
id = input.nextLine();
inputfile = input.nextLine();
String str = input.nextLine();
String cutName = str.replace("https://","");
String[] split = cutName.split(":");
servername = split[0];
port = Integer.parseInt(split[1]);
run(servername, port, input);
}
public static void run(String servername, int port, Scanner input) {
System.out.println("Initial timestamp: " + lamport_timestamp);
try (Socket socket = new Socket(servername, port)) {
//write to AtomServer
PrintWriter out_w = new PrintWriter(socket.getOutputStream(), true);
//Read from AtomServer
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
//Get the length of the xml file to be sent
int length = getLength(inputfile, id);
//PUT request header through socket
System.out.println("PUT /atom.xml HTTP/1.1\r\nUser-Agent: " + id + "\r\nContent Type: application/atom+xml\r\nContent Length: " + length);
out_w.println("PUT /atom.xml HTTP/1.1\r\nUser-Agent: " + id + "\r\nContent Type: application/atom+xml\r\nContent Length: " + length);
//PUT request body through socket
lamport_timestamp++;
buildThenSend(inputfile, id, socket);
ObjectInputStream inObj = new ObjectInputStream(socket.getInputStream());
Packet response_packet = (Packet) inObj.readObject();
//Process the response
String response = response_packet.xml;
int response_stamp = response_packet.timestamp;
lamport_timestamp = Math.max(response_stamp, lamport_timestamp) + 1;
System.out.println("Server response: " + response);
System.out.println("Current Timestamp: " + lamport_timestamp);
//If response is successful proceed to send heartbeat
if(response.equals("200 - Success") || response.equals("201 - HTTP Created")) {
String new_input = null;
while (true) {
if (input.hasNextLine())
new_input = input.nextLine();
if (new_input != null) {
if(new_input.equals("exit")) {
break;
} else {
out_w.println("1");
Thread.sleep(3000);
}
}
}
} else if(response.equals("204 - No Content")) {
System.out.println("Server did not store request");
}
}
catch (ConnectException e) {
System.out.println("Error: Server is offline. Retry in 12 seconds");
try {
Thread.sleep(12000);
} catch (InterruptedException ir) {
ir.printStackTrace();
}
System.out.println("Retrying ...");
retry_count++;
if(retry_count < 3)
run(servername, port, input);
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static int getLength(String input, String id) {
int length = 0;
try {
TransformerFactory tsf = TransformerFactory.newInstance();
Transformer ts = tsf.newTransformer();
XMLCreator creator = new XMLCreator();
String toSend = creator.build(input,id);
if(toSend != null)
length = toSend.length();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
return length;
}
}
private static void buildThenSend(String input, String id, Socket socket_channel) {
try {
TransformerFactory tsf = TransformerFactory.newInstance();
Transformer ts = tsf.newTransformer();
XMLCreator creator = new XMLCreator();
String toSend = creator.build(input,id);
ObjectOutputStream obj = new ObjectOutputStream(socket_channel.getOutputStream());
Packet packet = new Packet(toSend, lamport_timestamp);
obj.writeObject(packet);
}
catch (DOMException e) {
System.out.println("Error: XML cannot build. Input source is empty or not formatted.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}