blob: 95b6a61e4c12fd6fbbcd499d51dc1c2f7a23fa8e (
plain) (
tree)
|
|
package ganarchy.friendcode.sam;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.net.*;
public class I2PSamStreamConnector extends I2PSamStateMachine {
private final String id;
private final SocketAddress socketAddress;
private final String friendCode;
private boolean connected;
private I2PSamCommand status;
public I2PSamStreamConnector(SocketAddress socketAddress, String id, String friendCode) {
this.id = id;
this.socketAddress = socketAddress;
this.friendCode = friendCode;
}
@Override
public boolean connect() {
try {
Socket samSocket = new Socket();
samSocket.connect(this.socketAddress, 3000);
return this.connect(samSocket);
} catch (IOException e) {
return false;
}
}
public boolean start() {
if (!super.start()) {
return false;
}
try {
this.sendCommand(new I2PSamCommand(
"STREAM", "CONNECT",
ImmutableMap.of(
"ID", this.id,
"DESTINATION", this.friendCode
)
));
return this.connected = "OK".equals((this.status = this.getCommand("STREAM", "STATUS")).parameters().get("RESULT"));
} catch (IOException e) {
return false;
}
}
@Override
protected void sendCommand(I2PSamCommand command) throws IOException {
if (this.connected) {
throw new IllegalStateException("call unwrap() instead");
}
super.sendCommand(command);
}
@Override
public void step() throws IOException {
if (this.connected) {
throw new IllegalStateException("call unwrap() instead");
}
super.step();
}
@Override
public Socket unwrap() {
return super.unwrap();
}
public I2PSamCommand getStatus() {
return this.status;
}
}
|