Sunday, October 11, 2020

Java Basic Util - NIO.2 - Socket Command

package basic.util.socket;


@FunctionalInterface

public interface IAction <V, S, T> {

void run(V v, S s, T t);

}

2
package basic.util.socket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import basic.util.FileUtil;
import basic.util.exception.BusinessException;
import basic.util.log.Log;

public enum Command implements ICommand {
ready("ready"), 
getServerData("getServerData"){
        @Override
        public void serverAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ServerSocketHandler handler) {
    try {
    BaseSocket.readFromFile(channel, buffer, handler.getDataFilePath());
    BaseSocket.writeChannel(channel, Command.Bye.getValue());
    } catch (InterruptedException exception) {
    Log.severe(exception);
    }
        }
        
        @Override
public void clientAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ClientSocketHandler handler) {
try {
BaseSocket.readToFile(channel, buffer, handler.getData());
} catch (InterruptedException | ExecutionException exception) {
handler.retainThrowable(AccessChannelFailed, exception, RetrieveFromServer,
handler.getServerHost(), Integer.toString(handler.getPort()));
} catch (TimeoutException exception) {
channel.write(ByteBuffer.wrap(Command.Bye.getValue().getBytes()));
handler.retainThrowable(AccessChannelFailed, exception, RetrieveFromServer,
handler.getServerHost(), Integer.toString(handler.getPort()));
} catch (IOException exception) {
handler.retainThrowable(BusinessException.Failed, exception, "Write",
"Data File", handler.getData());
}
}
    }, 
sendServerResult("sendServerResult"){
        @Override
        public void serverAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ServerSocketHandler handler) {
        try {
        String content = BaseSocket.readFromSocket(channel, buffer);       
    FileUtil.appendJsonFile(content, handler.getDoneResultFilePath());
} catch (InterruptedException | ExecutionException exception) {
Log.severe(exception);
} catch (TimeoutException exception) {
channel.write(ByteBuffer.wrap(Command.Bye.getValue().getBytes()));
Log.severe(exception);
}
        }
        
        @Override
public void clientAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ClientSocketHandler handler) {
try {
BaseSocket.writeChannel(channel, handler.getData());
BaseSocket.writeChannel(channel, Command.Bye.getValue());
} catch (InterruptedException exception) {
handler.retainThrowable(AccessChannelFailed, exception, RetrieveFromServer,
handler.getServerHost(), Integer.toString(handler.getPort()));
}
}
    }, Bye("{\0}");
/*IAction<AsynchronousSocketChannel, ByteBuffer, String> action = (channel, buffer, string) -> {
try {
BaseSocket.readFromFile(channel, buffer, string);
} catch (InterruptedException exception) {
Log.severe(exception);
}
};*/
private String value;
    private Command(String value){
    this.value = value;
    }
            
    public String getValue(){
        return value;
    }
            
    public static Optional<ICommand> match(String value) {
    Command matched = null;
    Command[] commands = Command.values();
    for (Command command : commands) {
    if (command.getValue().equals(value)) {
    matched = command;
    break;
    }
    }
    return Optional.ofNullable(matched);
    }

@Override
public void serverAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ServerSocketHandler handler) {
// TODO Auto-generated method stub
}

@Override
public void clientAction(AsynchronousSocketChannel channel, ByteBuffer buffer, ClientSocketHandler handler) {
// TODO Auto-generated method stub
}
protected static final String AccessChannelFailed = "{0} to {1}:{2} failed";
protected static final String ExceptionWrong = "Exception.Wrong";
private static final String RetrieveFromServer = "Button.RetrieveFromServer";
}

No comments:

Post a Comment