Java Reference
In-Depth Information
simply has the protocol instance keep track of the amount of the remaining time, and use
setSoTimeout() to ensure that no read() call blocks for longer than that time. Unfortunately,
there is no way to bound the duration of a write() call, so we cannot really guarantee that the
time limit will hold. TimelimitEchoProtocolFactory.java implements this approach.
TimelimitEchoProtocolFactory.java
0 import java.net.*; // for Socket
1 import java.io.*; // for IOException and Input/OutputStream
2 import java.util.*; // for ArrayList
3
4 public class TimelimitEchoProtocolFactory implements ProtocolFactory {
5
6
public Runnable createProtocol(Socket clntSock, Logger logger) {
7
return new TimelimitEchoProtocol(clntSock, logger);
8
}
9 }
10
11 class TimelimitEchoProtocol implements Runnable {
12
private static final int BUFSIZE = 32; // Size (in bytes) of receive buffer
13
private static final String TIMELIMIT = "10000"; // Default time limit (ms)
14
private static final String TIMELIMITPROP = "Timelimit"; // Thread property
15
16
private int timelimit;
17
private Socket clntSock;
18
private Logger logger;
19
20
public TimelimitEchoProtocol(Socket clntSock, Logger logger) {
21
this.clntSock = clntSock;
22
this.logger = logger;
23
// Get the time limit from the System properties or take the default
24
timelimit = Integer.parseInt(System.getProperty(TIMELIMITPROP, TIMELIMIT));
25
}
26
27
public void run() {
28
ArrayList entry = new ArrayList();
29
entry.add("Client address and port="+
30
clntSock.getInetAddress().getHostAddress() + ":" +
31
clntSock.getPort());
32
entry.add("Thread="+Thread.currentThread().getName());
33
34
try {
35
// Get the input and output I/O streams from socket
36
InputStream in = clntSock.getInputStream();
37
OutputStream out = clntSock.getOutputStream();
38
 
Search WWH ::




Custom Search