package open.dolphin.impl.claim;
import java.io.IOException;
import java.net.Socket;
/**
* @author Kazushi Minagawa Digital Globe, Inc.
*
*/
public class OrcaSocket {
private final String host;
private final int port;
private final long period;
private final int tryCnt;
public OrcaSocket(String host, int port, long period, int tryCnt) {
this.host = host;
this.port = port;
this.period = period;
this.tryCnt = tryCnt;
}
public Socket getSocket() {
Socket socket = null;
int cnt = 0;
while (true) {
try {
socket = new Socket(host, port);
break;
} catch (IOException e) {
cnt++;
if ( (cnt % tryCnt) != 0) {
sleep(period);
} else {
break;
}
}
}
return socket;
}
private void sleep(long msec) {
try {
Thread.sleep(msec);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}