-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentServer.java
More file actions
119 lines (103 loc) · 4.46 KB
/
ContentServer.java
File metadata and controls
119 lines (103 loc) · 4.46 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
110
111
112
113
114
115
116
117
118
119
import java.net.Socket;
import java.net.ConnectException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.util.Scanner;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
class ContentServer {
public static int lamportClock = 0;
private static int port = 0;
public static int count = 0;
public static String hostName = "";
public static String ID = "";
public static String inputFile = "";
public static Socket socket = null;
public static void operateServer(Scanner input, String hostName, int port) throws IOException, ClassNotFoundException, InterruptedException {
System.out.println("Starting Content Server, timestamp: " + lamportClock);
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Socket socket = new Socket(hostName, port);
try {
oos = new ObjectOutputStream(socket.getOutputStream());
PrintWriter outputWriter = new PrintWriter(socket.getOutputStream(), true);
ois = new ObjectInputStream(socket.getInputStream());
int length = 0;
XMLFactory xmlFactory = new XMLFactoryImplementation();
String xmlOut = xmlFactory.buildXML(inputFile, ID);
if (xmlOut != null) {
length = xmlOut.length();
}
System.out.println("PUT /atom.xml HTTP/1.1");
System.out.println("User-Agent: " + ID );
System.out.println("Content Type: application/atom+xml");
System.out.println("Content Length: " + length);
outputWriter.println("PUT /atom.xml HTTP/1.1");
outputWriter.println("User-Agent: " + ID);
outputWriter.println("Content Type: application/atom+xml");
outputWriter.println("Content Length: " + length);
lamportClock++;
Packet packet = new Packet(xmlOut, lamportClock);
oos.writeObject(packet);
Packet responsePacket = (Packet) ois.readObject();
int responseTimeStamp = responsePacket.timeStamp;
String response = responsePacket.xmlString;
lamportClock = Math.max(lamportClock, responseTimeStamp);
lamportClock++;
System.out.println("Response: " + response +
"\r\nTimestamp" + lamportClock);
if (response.equalsIgnoreCase("204 - No Content")) {
System.out.println("Request not stored.");
} else if ((response.equalsIgnoreCase("201 - HTTP Created")) ||
(response.equalsIgnoreCase("200 - Success"))) {
String temp = null;
while (true) {
if (input.hasNextLine()) {
temp = input.nextLine();
}
if (temp != null) {
if(!temp.equalsIgnoreCase("exit")) {
outputWriter.println("1");
Thread.sleep(2000);
} else {
break;
}
}
}
}
ois.close();
oos.close();
}
catch (ConnectException e) {
System.out.println("Connection error, retrying...");
Thread.sleep(10000);
count++;
if (count < 3) {
operateServer(input, hostName, port);
}
}
catch( Exception e ) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
finally {
socket.close();
}
}
public static void main (String args[]) throws IOException, ClassNotFoundException, InterruptedException {
Scanner input = new Scanner(System.in);
System.out.println("Enter ID and Input File: ");
ID = input.nextLine();
inputFile = input.nextLine();
System.out.println("Enter the Client Name and Port Number: ");
hostName = input.nextLine();
hostName = hostName.replace("https://", "");
String[] nameComponents = hostName.split(":");
hostName = nameComponents[0];
port = Integer.parseInt(nameComponents[1]);
operateServer(input, hostName, port);
}
}