Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.SocketAddress;
- import java.net.SocketException;
- import java.nio.channels.SocketChannel;
- import java.util.ArrayList;
- import java.util.List;
- public class MultiSocket extends Socket {
- public static class Destination {
- private final String host;
- private final int port;
- public Destination(final String host, final int port) {
- this.host = host;
- this.port = port;
- }
- public String getHost() {
- return host;
- }
- public int getPort() {
- return port;
- }
- }
- private final List<Socket> alls;
- private List<InputStream> allis;
- private List<OutputStream> allos;
- private int lastwrite;
- private final byte[] buf;
- private int buflen;
- private class MultiInputStream extends InputStream {
- @Override
- public int read() throws IOException {
- if (lastwrite < 0)
- throw new IllegalStateException("MultiSocket is only for request-response");
- while (alls.size() > 0) {
- try {
- final int b = allis.get(lastwrite).read();
- buflen = 0;
- return b;
- } catch (final Exception e) {
- remove(lastwrite);
- allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
- lastwrite = 0;
- }
- }
- throw new IOException("No working sockets");
- }
- }
- private class MultiOutputStream extends OutputStream {
- @Override
- public void write(final int b) throws IOException {
- while (alls.size() > 0) {
- try {
- buf[buflen] = (byte) b;
- buflen++;
- allos.get(0).write(b);
- lastwrite = 0;
- return;
- } catch (final Exception e) {
- remove(0);
- allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
- lastwrite = 0;
- }
- }
- throw new IOException("No working sockets");
- }
- }
- private void remove(final int ix) {
- final OutputStream os = allos.remove(ix);
- try {
- os.close();
- } catch (final Exception ex) {
- // nothing to do
- }
- final InputStream is = allis.remove(ix);
- try {
- is.close();
- } catch (final Exception ex) {
- // nothing to do
- }
- final Socket s = alls.remove(ix);
- try {
- s.close();
- } catch (final Exception ex) {
- // nothing to do
- }
- }
- public MultiSocket(final List<Destination> dest) throws IOException {
- alls = new ArrayList<Socket>();
- for (final Destination d : dest) {
- alls.add(new Socket(d.getHost(), d.getPort()));
- }
- lastwrite = -1;
- buf = new byte[102400];
- buflen = 0;
- }
- @Override
- public InputStream getInputStream() throws IOException {
- allis = new ArrayList<InputStream>();
- for (final Socket s : alls) {
- allis.add(s.getInputStream());
- }
- return new MultiInputStream();
- }
- @Override
- public OutputStream getOutputStream() throws IOException {
- allos = new ArrayList<OutputStream>();
- for (final Socket s : alls) {
- allos.add(s.getOutputStream());
- }
- return new MultiOutputStream();
- }
- @Override
- public synchronized void close() throws IOException {
- while (alls.size() > 0) {
- remove(0);
- }
- }
- // move exception throwing stubs up as they get an implementation that makes sense
- @Override
- public void connect(final SocketAddress endpoint) throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void bind(final SocketAddress bindpoint) throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public InetAddress getInetAddress() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public InetAddress getLocalAddress() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public int getPort() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public int getLocalPort() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public SocketAddress getRemoteSocketAddress() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public SocketAddress getLocalSocketAddress() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public SocketChannel getChannel() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setTcpNoDelay(final boolean on) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean getTcpNoDelay() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setSoLinger(final boolean on, final int linger) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public int getSoLinger() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void sendUrgentData(final int data) throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setOOBInline(final boolean on) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean getOOBInline() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized void setSoTimeout(final int timeout) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized int getSoTimeout() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized void setSendBufferSize(final int size) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized int getSendBufferSize() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized void setReceiveBufferSize(final int size) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public synchronized int getReceiveBufferSize() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setKeepAlive(final boolean on) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean getKeepAlive() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setTrafficClass(final int tc) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public int getTrafficClass() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setReuseAddress(final boolean on) throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean getReuseAddress() throws SocketException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void shutdownInput() throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void shutdownOutput() throws IOException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public String toString() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean isConnected() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean isBound() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean isClosed() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean isInputShutdown() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean isOutputShutdown() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public void setPerformancePreferences(final int connectionTime, final int latency,
- final int bandwidth) {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public int hashCode() {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- public boolean equals(final Object obj) {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- protected Object clone() throws CloneNotSupportedException {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- @Override
- protected void finalize() throws Throwable {
- throw new RuntimeException("Not supported by MultiSocket");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement