summary refs log blame commit diff stats
path: root/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
blob: 1813511f79fb2a3af0be3abc3f032519df8e0537 (plain) (tree)















































                                                                                               
package ganarchy.friendcode.sam;

import com.google.common.collect.ImmutableMap;

import java.io.IOException;
import java.net.*;

public class I2PSamStreamForwarder extends I2PSamStateMachine {
    private final String id;
    private final String port;
    private final SocketAddress socketAddress;

    public I2PSamStreamForwarder(SocketAddress socketAddress, String id, String port) {
        this.id = id;
        this.port = port;
        this.socketAddress = socketAddress;
    }

    @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", "FORWARD",
                ImmutableMap.of(
                    "ID", this.id,
                    "PORT", this.port,
                    "SILENT", "true"
                )
            ));
            return "OK".equals(this.getCommand("STREAM", "STATUS").parameters().get("RESULT"));
        } catch (IOException e) {
            return false;
        }
    }
}