Example usage for org.apache.http.nio.reactor IOReactor execute

List of usage examples for org.apache.http.nio.reactor IOReactor execute

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor IOReactor execute.

Prototype

void execute(IOEventDispatch eventDispatch) throws IOException;

Source Link

Document

Starts the reactor and initiates the dispatch of I/O event notifications to the given IOEventDispatch .

Usage

From source file:org.opcfoundation.ua.transport.https.HttpsServer.java

@Override
public EndpointHandle bind(SocketAddress socketAddress, EndpointBinding endpointBinding)
        throws ServiceResultException {
    if (endpointBinding == null || socketAddress == null || endpointBinding.endpointServer != this)
        throw new IllegalArgumentException();
    String url = endpointBinding.endpointAddress.getEndpointUrl();

    // Start endpoint handler
    {//from w  w  w. j  av a  2s.co m
        String endpointId = url;
        endpointId = UriUtil.getEndpointName(url);
        if (endpointId == null)
            endpointId = "";
        //         else endpointId = "*"+endpointId;
        HttpAsyncRequestHandler<?> oldEndpointHandler = registry.lookup(endpointId);
        if (oldEndpointHandler == null) {
            HttpsServerEndpointHandler endpointHandler = new HttpsServerEndpointHandler(endpointBinding);
            registry.register(endpointId, endpointHandler);
            registry.register("", discoveryHandler);
        } else {
            HttpsServerEndpointHandler oldEndpointHander2 = (HttpsServerEndpointHandler) oldEndpointHandler;
            if (oldEndpointHander2.endpointServer != endpointBinding.endpointServer) {
                throw new ServiceResultException(StatusCodes.Bad_UnexpectedError,
                        "Cannot bind endpoint " + url + " and "
                                + oldEndpointHander2.endpointBinding.endpointAddress.getEndpointUrl()
                                + " with two different sets of service.");
            }
        }
    }

    // Make socket handle and endpoint handle
    String scheme = UriUtil.getTransportProtocol(endpointBinding.endpointAddress.getEndpointUrl());
    SocketHandle socketHandle = getOrCreateSocketHandle(socketAddress, scheme);

    HttpsEndpointHandle endpointHandle = socketHandle.getOrCreate(endpointBinding);

    try {
        // Shutdown reactor
        shutdownReactor();
        // Create reactor
        initReactor();

        // Bind to listen the given ports
        for (SocketHandle sh : socketHandleSnapshot()) {
            if (sh.listenerEndpoint == null) {
                sh.listenerEndpoint = ioReactor.listen(sh.getSocketAddress());
            }
        }

        // Start reactor threads
        if (UriUtil.SCHEME_HTTPS.equals(scheme)) {
            if (sslReactorThread == null || !sslReactorThread.isAlive()) {
                final IOReactor r = ioReactor;
                final Semaphore s = sslThreadSemaphore = new Semaphore(0);
                sslReactorThread = new Thread() {
                    public void run() {
                        try {
                            setState(CloseableObjectState.Open);
                            r.execute(sslIoEventDispatch);
                        } catch (IOException e) {
                            HttpsServer.this.setError(new ServiceResultException(e));
                        } finally {
                            s.release(9999);
                        }
                    };
                };
                if (!getState().isOpen())
                    setState(CloseableObjectState.Opening);
                sslReactorThread.start();
            }
        }

        if (UriUtil.SCHEME_HTTP.equals(scheme)) {
            if (plainReactorThread == null || !plainReactorThread.isAlive()) {
                final IOReactor r = ioReactor;
                final Semaphore s = plainThreadSemaphore = new Semaphore(0);
                plainReactorThread = new Thread() {
                    public void run() {
                        try {
                            setState(CloseableObjectState.Open);
                            r.execute(plainIoEventDispatch);
                        } catch (IOException e) {
                            HttpsServer.this.setError(new ServiceResultException(e));
                        } finally {
                            s.release(9999);
                        }
                    };
                };
                if (!getState().isOpen())
                    setState(CloseableObjectState.Opening);
                plainReactorThread.start();
            }
        }

    } catch (ServiceResultException e) {
        endpointHandle.close();
        throw e;
    }
    log.info("Endpoint bound to {}", url);
    return endpointHandle;
}