-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestGen.java
More file actions
152 lines (124 loc) · 4.17 KB
/
RequestGen.java
File metadata and controls
152 lines (124 loc) · 4.17 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
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
/**
*
* @author ningluo
*/
public class RequestGen implements Runnable {
private final String[] FileList;
private final String ServerName;
int ServerPort;
String ServerIP;
Socket socket;
private BufferedReader inFromServer;
private DataOutputStream outToServer;
private final long threshold;
private String currentFileName;
public RequestGen(String[] files, String sn, int p, String sip, long T) throws IOException {
this.ServerName = sn;
this.FileList = files;
this.ServerPort = p;
this.ServerIP = sip;
this.threshold = T;
}
@Override
public void run(){
long start = System.currentTimeMillis();
while(System.currentTimeMillis() - start < threshold){
PackageExchange();
}
// System.out.println(count);
}
//
// timer.schedule(new TimerTask(){
//16 public void run() {
// PackageExchange();
//23 }
//24 }, 0, 1000);
//25
//26 timer.schedule(new TimerTask(){
//27 public void run() {
//28 timer.cancel();
//29 }
//30 }, new Date(end));
//
public void PackageExchange() {
try {
for (int i = 0; i < FileList.length; i++) {
socket = new Socket(ServerIP, ServerPort);
inFromServer
= new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToServer
= new DataOutputStream(socket.getOutputStream());
SendRequest(FileList[i], ServerName);
currentFileName = FileList[i];
// System.out.println(FileList[i]);
Debug.DEBUG(FileList[i]);
// Debug.DEBUG(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
ReceiveResponse();
Debug.DEBUG(currentFileName + " finished");
}
} catch (Exception ex) {
Logger.getLogger(TestingRequestGen.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void SendRequest(String filename, String serverName) throws Exception {
// if (filename.equals("test.cgi")) {
// long start = System.currentTimeMillis();
//
// while(System.currentTimeMillis() - start < 4000) {
// }
//
// }
outToServer.writeBytes("GET " + filename + " HTTP/1.0 \r\n");
outToServer.writeBytes("Host: " + serverName + "\r\n");
outToServer.writeBytes("If-Modified-Since: Sun, 28 Oct 2009 07:28:00 GMT"+"\r\n");
outToServer.writeBytes("\r\n");
} // end send request
private void ReceiveResponse() throws Exception {
// while (!inFromServer.ready()) {
// }
try {
// String requestMessageLine = inFromServer.readLine();
//Debug.DEBUG(requestMessageLine);
BufferedReader br = new BufferedReader(inFromServer);
String line = br.readLine();
Debug.DEBUG(line);
while ((line = br.readLine()) != null) {
// System.out.println(line);
}// Debug.DEBUG(requestMessageLine);
// while (!inFromServer.ready()) {
// }
// try {
// if (requestMessageLine.startsWith("Content-Length: ")) {
// int contentLength = Integer.parseInt(requestMessageLine.substring(16));
// char [] buffer = new char[contentLength];
// inFromServer.read(buffer, 0, contentLength);
//
// }
// requestMessageLine = inFromServer.readLine();
} catch (SocketException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// Debug.DEBUG(currentFileName);
// Debug.DEBUG(requestMessageLine);
// throw e;
}
inFromServer.close();
socket.close();
return;
}
}