Advertisement
Guest User

Untitled

a guest
May 19th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.99 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6. import java.net.SocketAddress;
  7. import java.net.SocketException;
  8. import java.nio.channels.SocketChannel;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class MultiSocket extends Socket {
  13.     public static class Destination {
  14.         private final String    host;
  15.         private final int           port;
  16.  
  17.         public Destination(final String host, final int port) {
  18.             this.host = host;
  19.             this.port = port;
  20.         }
  21.  
  22.         public String getHost() {
  23.             return host;
  24.         }
  25.  
  26.         public int getPort() {
  27.             return port;
  28.         }
  29.     }
  30.  
  31.     private final List<Socket>  alls;
  32.     private List<InputStream>       allis;
  33.     private List<OutputStream>  allos;
  34.     private int                                 lastwrite;
  35.     private final byte[]                buf;
  36.     private int                                 buflen;
  37.  
  38.     private class MultiInputStream extends InputStream {
  39.         @Override
  40.         public int read() throws IOException {
  41.             if (lastwrite < 0)
  42.                 throw new IllegalStateException("MultiSocket is only for request-response");
  43.             while (alls.size() > 0) {
  44.                 try {
  45.                     final int b = allis.get(lastwrite).read();
  46.                     buflen = 0;
  47.                     return b;
  48.                 } catch (final Exception e) {
  49.                     remove(lastwrite);
  50.                     allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
  51.                     lastwrite = 0;
  52.                 }
  53.             }
  54.             throw new IOException("No working sockets");
  55.         }
  56.     }
  57.  
  58.     private class MultiOutputStream extends OutputStream {
  59.         @Override
  60.         public void write(final int b) throws IOException {
  61.             while (alls.size() > 0) {
  62.                 try {
  63.                     buf[buflen] = (byte) b;
  64.                     buflen++;
  65.                     allos.get(0).write(b);
  66.                     lastwrite = 0;
  67.                     return;
  68.                 } catch (final Exception e) {
  69.                     remove(0);
  70.                     allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
  71.                     lastwrite = 0;
  72.                 }
  73.             }
  74.             throw new IOException("No working sockets");
  75.         }
  76.     }
  77.  
  78.     private void remove(final int ix) {
  79.         final OutputStream os = allos.remove(ix);
  80.         try {
  81.             os.close();
  82.         } catch (final Exception ex) {
  83.             // nothing to do
  84.         }
  85.         final InputStream is = allis.remove(ix);
  86.         try {
  87.             is.close();
  88.         } catch (final Exception ex) {
  89.             // nothing to do
  90.         }
  91.         final Socket s = alls.remove(ix);
  92.         try {
  93.             s.close();
  94.         } catch (final Exception ex) {
  95.             // nothing to do
  96.         }
  97.     }
  98.  
  99.     public MultiSocket(final List<Destination> dest) throws IOException {
  100.         alls = new ArrayList<Socket>();
  101.         for (final Destination d : dest) {
  102.             alls.add(new Socket(d.getHost(), d.getPort()));
  103.         }
  104.         lastwrite = -1;
  105.         buf = new byte[102400];
  106.         buflen = 0;
  107.     }
  108.  
  109.     @Override
  110.     public InputStream getInputStream() throws IOException {
  111.         allis = new ArrayList<InputStream>();
  112.         for (final Socket s : alls) {
  113.             allis.add(s.getInputStream());
  114.         }
  115.         return new MultiInputStream();
  116.     }
  117.  
  118.     @Override
  119.     public OutputStream getOutputStream() throws IOException {
  120.         allos = new ArrayList<OutputStream>();
  121.         for (final Socket s : alls) {
  122.             allos.add(s.getOutputStream());
  123.         }
  124.         return new MultiOutputStream();
  125.     }
  126.  
  127.     @Override
  128.     public synchronized void close() throws IOException {
  129.         while (alls.size() > 0) {
  130.             remove(0);
  131.         }
  132.     }
  133.  
  134.     // move exception throwing stubs up as they get an implementation that makes sense
  135.     @Override
  136.     public void connect(final SocketAddress endpoint) throws IOException {
  137.         throw new RuntimeException("Not supported by MultiSocket");
  138.     }
  139.  
  140.     @Override
  141.     public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
  142.         throw new RuntimeException("Not supported by MultiSocket");
  143.     }
  144.  
  145.     @Override
  146.     public void bind(final SocketAddress bindpoint) throws IOException {
  147.         throw new RuntimeException("Not supported by MultiSocket");
  148.     }
  149.  
  150.     @Override
  151.     public InetAddress getInetAddress() {
  152.         throw new RuntimeException("Not supported by MultiSocket");
  153.     }
  154.  
  155.     @Override
  156.     public InetAddress getLocalAddress() {
  157.         throw new RuntimeException("Not supported by MultiSocket");
  158.     }
  159.  
  160.     @Override
  161.     public int getPort() {
  162.         throw new RuntimeException("Not supported by MultiSocket");
  163.     }
  164.  
  165.     @Override
  166.     public int getLocalPort() {
  167.         throw new RuntimeException("Not supported by MultiSocket");
  168.     }
  169.  
  170.     @Override
  171.     public SocketAddress getRemoteSocketAddress() {
  172.         throw new RuntimeException("Not supported by MultiSocket");
  173.     }
  174.  
  175.     @Override
  176.     public SocketAddress getLocalSocketAddress() {
  177.         throw new RuntimeException("Not supported by MultiSocket");
  178.     }
  179.  
  180.     @Override
  181.     public SocketChannel getChannel() {
  182.         throw new RuntimeException("Not supported by MultiSocket");
  183.     }
  184.  
  185.     @Override
  186.     public void setTcpNoDelay(final boolean on) throws SocketException {
  187.         throw new RuntimeException("Not supported by MultiSocket");
  188.     }
  189.  
  190.     @Override
  191.     public boolean getTcpNoDelay() throws SocketException {
  192.         throw new RuntimeException("Not supported by MultiSocket");
  193.     }
  194.  
  195.     @Override
  196.     public void setSoLinger(final boolean on, final int linger) throws SocketException {
  197.         throw new RuntimeException("Not supported by MultiSocket");
  198.     }
  199.  
  200.     @Override
  201.     public int getSoLinger() throws SocketException {
  202.         throw new RuntimeException("Not supported by MultiSocket");
  203.     }
  204.  
  205.     @Override
  206.     public void sendUrgentData(final int data) throws IOException {
  207.         throw new RuntimeException("Not supported by MultiSocket");
  208.     }
  209.  
  210.     @Override
  211.     public void setOOBInline(final boolean on) throws SocketException {
  212.         throw new RuntimeException("Not supported by MultiSocket");
  213.     }
  214.  
  215.     @Override
  216.     public boolean getOOBInline() throws SocketException {
  217.         throw new RuntimeException("Not supported by MultiSocket");
  218.     }
  219.  
  220.     @Override
  221.     public synchronized void setSoTimeout(final int timeout) throws SocketException {
  222.         throw new RuntimeException("Not supported by MultiSocket");
  223.     }
  224.  
  225.     @Override
  226.     public synchronized int getSoTimeout() throws SocketException {
  227.         throw new RuntimeException("Not supported by MultiSocket");
  228.     }
  229.  
  230.     @Override
  231.     public synchronized void setSendBufferSize(final int size) throws SocketException {
  232.         throw new RuntimeException("Not supported by MultiSocket");
  233.     }
  234.  
  235.     @Override
  236.     public synchronized int getSendBufferSize() throws SocketException {
  237.         throw new RuntimeException("Not supported by MultiSocket");
  238.     }
  239.  
  240.     @Override
  241.     public synchronized void setReceiveBufferSize(final int size) throws SocketException {
  242.         throw new RuntimeException("Not supported by MultiSocket");
  243.     }
  244.  
  245.     @Override
  246.     public synchronized int getReceiveBufferSize() throws SocketException {
  247.         throw new RuntimeException("Not supported by MultiSocket");
  248.     }
  249.  
  250.     @Override
  251.     public void setKeepAlive(final boolean on) throws SocketException {
  252.         throw new RuntimeException("Not supported by MultiSocket");
  253.     }
  254.  
  255.     @Override
  256.     public boolean getKeepAlive() throws SocketException {
  257.         throw new RuntimeException("Not supported by MultiSocket");
  258.     }
  259.  
  260.     @Override
  261.     public void setTrafficClass(final int tc) throws SocketException {
  262.         throw new RuntimeException("Not supported by MultiSocket");
  263.     }
  264.  
  265.     @Override
  266.     public int getTrafficClass() throws SocketException {
  267.         throw new RuntimeException("Not supported by MultiSocket");
  268.     }
  269.  
  270.     @Override
  271.     public void setReuseAddress(final boolean on) throws SocketException {
  272.         throw new RuntimeException("Not supported by MultiSocket");
  273.     }
  274.  
  275.     @Override
  276.     public boolean getReuseAddress() throws SocketException {
  277.         throw new RuntimeException("Not supported by MultiSocket");
  278.     }
  279.  
  280.     @Override
  281.     public void shutdownInput() throws IOException {
  282.         throw new RuntimeException("Not supported by MultiSocket");
  283.     }
  284.  
  285.     @Override
  286.     public void shutdownOutput() throws IOException {
  287.         throw new RuntimeException("Not supported by MultiSocket");
  288.     }
  289.  
  290.     @Override
  291.     public String toString() {
  292.         throw new RuntimeException("Not supported by MultiSocket");
  293.     }
  294.  
  295.     @Override
  296.     public boolean isConnected() {
  297.         throw new RuntimeException("Not supported by MultiSocket");
  298.     }
  299.  
  300.     @Override
  301.     public boolean isBound() {
  302.         throw new RuntimeException("Not supported by MultiSocket");
  303.     }
  304.  
  305.     @Override
  306.     public boolean isClosed() {
  307.         throw new RuntimeException("Not supported by MultiSocket");
  308.     }
  309.  
  310.     @Override
  311.     public boolean isInputShutdown() {
  312.         throw new RuntimeException("Not supported by MultiSocket");
  313.     }
  314.  
  315.     @Override
  316.     public boolean isOutputShutdown() {
  317.         throw new RuntimeException("Not supported by MultiSocket");
  318.     }
  319.  
  320.     @Override
  321.     public void setPerformancePreferences(final int connectionTime, final int latency,
  322.             final int bandwidth) {
  323.         throw new RuntimeException("Not supported by MultiSocket");
  324.     }
  325.  
  326.     @Override
  327.     public int hashCode() {
  328.         throw new RuntimeException("Not supported by MultiSocket");
  329.     }
  330.  
  331.     @Override
  332.     public boolean equals(final Object obj) {
  333.         throw new RuntimeException("Not supported by MultiSocket");
  334.     }
  335.  
  336.     @Override
  337.     protected Object clone() throws CloneNotSupportedException {
  338.         throw new RuntimeException("Not supported by MultiSocket");
  339.     }
  340.  
  341.     @Override
  342.     protected void finalize() throws Throwable {
  343.         throw new RuntimeException("Not supported by MultiSocket");
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement