Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed 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.fusesource.fabric.dosgi.tcp;
 
 import java.net.URI;
 import java.util.Map;
 
A TCP based transport server

Author(s):
Hiram Chirino
 
 
 public class TcpTransportServer implements TransportServer {
 
     private final String bindScheme;
     private final InetSocketAddress bindAddress;
 
     private int backlog = 100;
     private Map<StringObjecttransportOptions;
 
     private ServerSocketChannel channel;
     private TransportAcceptListener listener;
     private DispatchQueue dispatchQueue;
     private DispatchSource acceptSource;
 
     public TcpTransportServer(URI locationthrows UnknownHostException {
          = location.getScheme();
         String host = location.getHost();
         host = (host == null || host.length() == 0) ? "::" : host;
          = new InetSocketAddress(InetAddress.getByName(host), location.getPort());
     }
 
     public void setAcceptListener(TransportAcceptListener listener) {
         this. = listener;
     }
 
     public InetSocketAddress getSocketAddress() {
         return (InetSocketAddress.socket().getLocalSocketAddress();
     }
 
     public DispatchQueue getDispatchQueue() {
         return ;
     }
 
     public void setDispatchQueue(DispatchQueue dispatchQueue) {
         this. = dispatchQueue;
     }
 
     public void suspend() {
         .suspend();
     }
 
     public void resume() {
         .resume();
     }
 
     public void start() throws Exception {
         start(null);
     }
     public void start(Runnable onCompletedthrows Exception {
 
         try {
              = ServerSocketChannel.open();
             .configureBlocking(false);
             .socket().bind();
         } catch (IOException e) {
             throw IOExceptionSupport.create("Failed to bind to server socket: " +  + " due to: " + ee);
        }
        .setEventHandler(new Runnable() {
            public void run() {
                try {
                    SocketChannel client = .accept();
                    whileclient!=null ) {
                        handleSocket(client);
                        client = .accept();
                    }
                } catch (Exception e) {
                    .onAcceptError(TcpTransportServer.thise);
                }
            }
        });
        .setCancelHandler(new Runnable() {
            public void run() {
                try {
                    .close();
                } catch (IOException e) {
                }
            }
        });
        .resume();
        ifonCompleted!=null ) {
            .execute(onCompleted);
        }
    }
    public String getBoundAddress() {
        try {
            return new URI(null.getAddress().getHostAddress(), .socket().getLocalPort(), nullnullnull).toString();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    public String getConnectAddress() {
        try {
            return new URI(nullresolveHostName(), .socket().getLocalPort(), nullnullnull).toString();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    protected String resolveHostName() {
        String result;
        if (.getAddress().isAnyLocalAddress()) {
            // make it more human readable and useful, an alternative to 0.0.0.0
            try {
                result = InetAddress.getLocalHost().getCanonicalHostName();
            } catch (UnknownHostException e) {
                result = "localhost";
            }
        } else {
            result = .getAddress().getCanonicalHostName();
        }
        return result;
    }
    public void stop() {
        stop(null);
    }
    public void stop(final Runnable onCompleted) {
        if.isCanceled() ) {
            onCompleted.run();
        } else {
            .setCancelHandler(new Runnable() {
                public void run() {
                    try {
                        .close();
                    } catch (IOException e) {
                    }
                    ifonCompleted!=null ) {
                        onCompleted.run();
                    }
                }
            });
            .cancel();
        }
    }
    public int getBacklog() {
        return ;
    }
    public void setBacklog(int backlog) {
        this. = backlog;
    }
    protected final void handleSocket(SocketChannel socketthrows Exception {
        HashMap<StringObjectoptions = new HashMap<StringObject>();
//      options.put("maxInactivityDuration", Long.valueOf(maxInactivityDuration));
//      options.put("maxInactivityDurationInitalDelay", Long.valueOf(maxInactivityDurationInitalDelay));
//      options.put("trace", Boolean.valueOf(trace));
//      options.put("soTimeout", Integer.valueOf(soTimeout));
//      options.put("socketBufferSize", Integer.valueOf(socketBufferSize));
//      options.put("connectionTimeout", Integer.valueOf(connectionTimeout));
//      options.put("dynamicManagement", Boolean.valueOf(dynamicManagement));
//      options.put("startLogging", Boolean.valueOf(startLogging));
        TcpTransport transport = createTransport(socketoptions);
        .onAccept(thistransport);
    }
    protected TcpTransport createTransport(SocketChannel socketChannelHashMap<StringObjectoptionsthrows Exception {
        TcpTransport transport = createTransport();
        transport.connected(socketChannel);
        ifoptions!=null ) {
            IntrospectionSupport.setProperties(transportoptions);
        }
        if ( != null) {
            IntrospectionSupport.setProperties(transport);
        }
        return transport;
    }
    protected TcpTransport createTransport() {
        return new TcpTransport();
    }
    public void setTransportOption(Map<StringObjecttransportOptions) {
        this. = transportOptions;
    }

    

Returns:
pretty print of this
    public String toString() {
        return getBoundAddress();
    }
New to GrepCode? Check out our FAQ X