1
package basic.util.socket;
public class BaseSocketHandler {
private int port;
private boolean isReady;
public BaseSocketHandler(int port) {
this.port = port;
}
protected int getPort(){
return port;
}
protected void setPort(int port){
this.port = port;
}
public boolean isReady() {
return isReady;
}
public void setReady(boolean isReady) {
this.isReady = isReady;
}
public static void main(String[] args) {
System.out.println(Command.ready);
}
}
2
package basic.util.socket;
import java.io.IOException;
import basic.util.log.Log;
public class ServerSocketHandler extends BaseSocketHandler {
private ServerSocket serverSocket;
public ServerSocketHandler(int port) {
super(port);
}
public void run() {
//ServerSocket serverSocket = new ServerSocket(getPort());
serverSocket = new ServerSocket(getPort());
serverSocket.run(this);
}
public void reRun() {
close();
run();
}
public void reRun(int port) {
this.setPort(port);
reRun();
}
public boolean isListening() {
if (serverSocket != null && serverSocket.getListener() != null && serverSocket.getListener().isOpen()) return true;
else return false;
}
public void close() {
if (isListening()) {
try {
serverSocket.getListener().close();
} catch (IOException exception) {
Log.severe(exception);
}
}
}
public String getDataFilePath() {
//return SettingController.getSetting().getProperty(SettingController.DataFilePath);
return "C:/data.txt";
}
public String getDoneResultFilePath() {
//return SettingController.getSetting().getProperty(SettingController.DoneFilePath);
return "C:/result.txt";
}
}
3
package basic.util.socket;
import basic.util.exception.BaseException;
import basic.util.log.Log;
public class BaseClientSocketHandler extends BaseSocketHandler {
final private String serverHost;
private BaseException throwable;
public BaseClientSocketHandler(String serverHost, int port) {
super(port);
this.serverHost = serverHost;
}
public String getServerHost() {
return serverHost;
}
public Throwable getThrowable() {
return throwable;
}
public void retainThrowable(String message, Throwable throwable, String... parameters) {
this.throwable = new BaseException(message, throwable, parameters);
Log.severe(throwable);
}
protected void throwWhenException() {
if (getThrowable() != null) {
throw throwable;
}
}
protected void waitAsyncDone() {
try {
while (!isReady()) {
Thread.sleep(10);
}
} catch (InterruptedException exception) {
BaseException.throwException("Current thread is interrupted", exception);
}
}
}
4
package basic.util.socket;
public class ClientSocketHandler extends BaseClientSocketHandler {
public ClientSocketHandler(String serverHost, int port) {
super(serverHost, port);
}
private ClientSocket getClientSocket() {
return new ClientSocket(getServerHost(), getPort());
}
public void transferFile(Command cmd, String path) {
setData(path);
ClientSocket clientSocket = getClientSocket();
clientSocket.run(this, cmd);
waitAsyncDone();
throwWhenException();
}
public void sendData(Command cmd, String data) {
setData(data);
ClientSocket clientSocket = getClientSocket();
clientSocket.run(this, cmd);
waitAsyncDone();
throwWhenException();
}
private String data;
public String getData() {
return data;
}
private void setData(String data) {
this.data = data;
}
}
No comments:
Post a Comment