Commit e58fd43b authored by amir.yosef's avatar amir.yosef

cleaning

parent 0a1f7bde
package handshake;
import model.Command;
import parser.CommandParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class PingCommand extends Sender {
public PingCommand(CommandParser commandParser) {
super(commandParser);
}
@Override
public void send(BufferedReader bufferedReader, OutputStream outputStream) throws IOException {
byte[] bytes = commandParser.getResponseFromCommandArray(List.of(Command.PING.getValue().toLowerCase())).getBytes();
outputStream.write(bytes);
outputStream.flush();
}
}
package handshake;
import model.Command;
import parser.CommandParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class ReplConfCommand extends Sender {
private final String port;
public ReplConfCommand(String port, CommandParser commandParser) {
super(commandParser);
this.port = port;
}
@Override
public void send(BufferedReader bufferedReader, OutputStream outputStream) throws IOException {
String replconfResp = "";
replconfResp = getReplConfResp(bufferedReader, outputStream);
if (!replconfResp.isEmpty()) {
replconfResp = sendReplConfCapa(bufferedReader, outputStream);
}
if (!replconfResp.isEmpty()) {
outputStream.write(commandParser.getResponseFromCommandArray(List.of(Command.PSYNC.getValue(), "?", "-1")).getBytes());
}
}
private String sendReplConfCapa(BufferedReader bufferedReader, OutputStream outputStream) throws IOException {
outputStream.write(commandParser.getResponseFromCommandArray(List.of(
Command.REPLCONF.getValue(),
"capa",
"npsync2")
).getBytes()
);
outputStream.flush();
return bufferedReader.readLine();
}
private String getReplConfResp(BufferedReader bufferedReader, OutputStream outputStream) throws IOException {
outputStream.write(commandParser.getResponseFromCommandArray(List.of(
Command.REPLCONF.getValue(),
"listening-port",
String.valueOf(port))
).getBytes()
);
outputStream.flush();
return bufferedReader.readLine();
}
}
package handshake;
import parser.CommandParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
public abstract class Sender {
protected final CommandParser commandParser;
protected Sender(CommandParser commandParser) {
this.commandParser = commandParser;
}
public abstract void send(BufferedReader bufferedReader, OutputStream outputStream) throws IOException;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment