package org.hudsonci.maven.eventspy.common;
import hudson.remoting.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
implements Closeable
{
private static final Logger log = LoggerFactory.getLogger(RemotingClient.class);
private final ExecutorService executor = Executors.newCachedThreadPool();
private final int port;
private Channel channel;
this.port = port;
}
ensureOpened();
return channel;
}
return channel != null;
}
if (!isOpen()) {
throw new IllegalStateException();
}
}
public void open()
throws IOException, InterruptedException {
if (isOpen()) {
throw new IllegalStateException();
}
log.debug("Opening w/port: {}", port);
final Socket socket = new Socket((String)null, port);
InputStream input = new BufferedInputStream(new FilterInputStream(socket.getInputStream())
{
public void close()
throws IOException {
socket.shutdownInput();
}
});
OutputStream output = new BufferedOutputStream(new FilterOutputStream(socket.getOutputStream())
{
public void close()
throws IOException {
socket.shutdownOutput();
}
});
channel = new Channel(getClass().getName(), executor, Channel.Mode.BINARY, input, output);
log.debug("Opened");
}
public void join()
throws InterruptedException {
log.debug("Joining");
getChannel().join();
log.debug("Joined");
}
public void close()
throws IOException {
if (channel != null) {
log.debug("Closing");
channel.close();
channel = null;
log.debug("Closed");
}
}
@Override
return "RemotingClient{" +
"port=" + port +
", channel=" + channel +
'}';
}
extends java.io.FilterOutputStream
{
super(out);
}
public void write(
final byte[] b)
throws IOException {
out.write(b);
}
public void write(
final byte[] b,
final int off,
final int len)
throws IOException {
out.write(b, off, len);
}
public void close()
throws IOException {
out.close();
}
}
public static void main(
final String[] args)
throws Exception {
assert args != null;
assert args.length == 1;
int port = Integer.parseInt(args[0]);
RemotingClient client = new RemotingClient(port);
client.open();
client.join();
client.close();
System.exit(0);
}
}