Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.avro.ipc;
 
 
A socket-based server implementation. This uses a simple, non-standard wire protocol and is not intended for production services.
 
 public class SocketServer extends Thread implements Server {
   private static final Logger LOG = LoggerFactory.getLogger(SocketServer.class);
 
   private Responder responder;
   private ThreadGroup group;
 
   public SocketServer(Responder responderSocketAddress addr)
     throws IOException {
     String name = "SocketServer on "+addr;
 
     this. = responder;
     this. = new ThreadGroup(name);
     this. = ServerSocketChannel.open();
 
     .socket().bind(addr);
 
     setName(name);
     setDaemon(true);
     start();
   }
 
   public int getPort() { return .socket().getLocalPort(); }
 
   public void run() {
     .info("starting "+.socket().getInetAddress());
     while (true) {
       try {
         new Connection(.accept());
       } catch (ClosedChannelException e) {
         return;
       } catch (IOException e) {
         .warn("unexpected error"e);
         throw new RuntimeException(e);
       } finally {
         .info("stopping "+.socket().getInetAddress());
       }
     }
   }
 
   public void close() {
     .interrupt();
   }
 
   private class Connection extends SocketTransceiver implements Runnable {
 
     public Connection(SocketChannel channelthrows IOException {
       super(channel);
 
       Thread thread = new Thread(this);
       thread.setName("Connection to "+channel.socket().getRemoteSocketAddress());
       thread.setDaemon(true);
       thread.start();
     }
 
     public void run() {
       try {
         try {
           while (true) {
             writeBuffers(.respond(readBuffers()));
           }
         } catch (ClosedChannelException e) {
           return;
         } finally {
           close();
         }
       } catch (IOException e) {
        .warn("unexpected error"e);
      }
    }
  }
  
  public static void main(String[] argthrows Exception {
    SocketServer server = new SocketServer(nullnew InetSocketAddress(0));
    ..println("started");
    server.join();
  }
New to GrepCode? Check out our FAQ X