Advertisement
Guest User

Untitled

a guest
May 19th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import lombok.AutoGenMethodStub;
  9. import lombok.Cleanup;
  10. import lombok.Data;
  11.  
  12. @AutoGenMethodStub (throwException = true)
  13. public class MultiSocketLomboked extends Socket {
  14.     @Data
  15.     public static class Destination {
  16.         private final String    host;
  17.         private final int           port;
  18.     }
  19.  
  20.     private final List<Socket>  alls;
  21.     private List<InputStream>       allis;
  22.     private List<OutputStream>  allos;
  23.     private int                                 lastwrite;
  24.     private final byte[]                buf;
  25.     private int                                 buflen;
  26.  
  27.     private class MultiInputStream extends InputStream {
  28.         @Override
  29.         public int read() throws IOException {
  30.             if (lastwrite < 0)
  31.                 throw new IllegalStateException("MultiSocket is only for request-response");
  32.             while (alls.size() > 0) {
  33.                 try {
  34.                     final int b = allis.get(lastwrite).read();
  35.                     buflen = 0;
  36.                     return b;
  37.                 } catch (final Exception e) {
  38.                     remove(lastwrite);
  39.                     allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
  40.                     lastwrite = 0;
  41.                 }
  42.             }
  43.             throw new IOException("No working sockets");
  44.         }
  45.     }
  46.  
  47.     private class MultiOutputStream extends OutputStream {
  48.         @Override
  49.         public void write(final int b) throws IOException {
  50.             while (alls.size() > 0) {
  51.                 try {
  52.                     buf[buflen] = (byte) b;
  53.                     buflen++;
  54.                     allos.get(0).write(b);
  55.                     lastwrite = 0;
  56.                     return;
  57.                 } catch (final Exception e) {
  58.                     remove(0);
  59.                     allos.get(0).write(buf, 0, buflen); // additional code is necessary final to handle if this call throws an exception
  60.                     lastwrite = 0;
  61.                 }
  62.             }
  63.             throw new IOException("No working sockets");
  64.         }
  65.     }
  66.  
  67.     private void remove(final int ix) {
  68.         try {
  69.             @Cleanup final OutputStream os = allos.remove(ix);
  70.             @Cleanup final InputStream is = allis.remove(ix);
  71.             @Cleanup final Socket s = alls.remove(ix);
  72.         } catch (final IOException ignored) {}
  73.     }
  74.  
  75.     public MultiSocketLomboked(final List<Destination> dest) throws IOException {
  76.         alls = new ArrayList<Socket>();
  77.         for (final Destination d : dest) {
  78.             alls.add(new Socket(d.getHost(), d.getPort()));
  79.         }
  80.         lastwrite = -1;
  81.         buf = new byte[102400];
  82.         buflen = 0;
  83.     }
  84.  
  85.     @Override
  86.     public InputStream getInputStream() throws IOException {
  87.         allis = new ArrayList<InputStream>();
  88.         for (final Socket s : alls) {
  89.             allis.add(s.getInputStream());
  90.         }
  91.         return new MultiInputStream();
  92.     }
  93.  
  94.     @Override
  95.     public OutputStream getOutputStream() throws IOException {
  96.         allos = new ArrayList<OutputStream>();
  97.         for (final Socket s : alls) {
  98.             allos.add(s.getOutputStream());
  99.         }
  100.         return new MultiOutputStream();
  101.     }
  102.  
  103.     @Override
  104.     public synchronized void close() throws IOException {
  105.         while (alls.size() > 0) {
  106.             remove(0);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement