Search in sources :

Example 1 with ONetworkProtocol

use of com.orientechnologies.orient.server.network.protocol.ONetworkProtocol in project orientdb by orientechnologies.

the class OClientConnectionManager method kill.

/**
 * Disconnects and kill the associated network manager.
 *
 * @param connection
 *          connection to kill
 */
public void kill(final OClientConnection connection) {
    if (connection != null) {
        final ONetworkProtocol protocol = connection.getProtocol();
        try {
            // INTERRUPT THE NEWTORK MANAGER TOO
            protocol.interrupt();
        } catch (Exception e) {
            OLogManager.instance().error(this, "Error during interruption of binary protocol", e);
        }
        disconnect(connection);
        // KILL THE NETWORK MANAGER TOO
        protocol.sendShutdown();
    }
}
Also used : ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException)

Example 2 with ONetworkProtocol

use of com.orientechnologies.orient.server.network.protocol.ONetworkProtocol in project orientdb by orientechnologies.

the class OClientConnectionManager method shutdown.

public void shutdown() {
    timerTask.cancel();
    List<ONetworkProtocol> toWait = new ArrayList<ONetworkProtocol>();
    final Iterator<Entry<Integer, OClientConnection>> iterator = connections.entrySet().iterator();
    while (iterator.hasNext()) {
        final Entry<Integer, OClientConnection> entry = iterator.next();
        final ONetworkProtocol protocol = entry.getValue().getProtocol();
        if (protocol != null)
            protocol.sendShutdown();
        OLogManager.instance().debug(this, "Sending shutdown to thread %s", protocol);
        OCommandRequestText command = entry.getValue().getData().command;
        if (command != null && command.isIdempotent()) {
            protocol.interrupt();
        } else {
            if (protocol instanceof ONetworkProtocolBinary && ((ONetworkProtocolBinary) protocol).getRequestType() == OChannelBinaryProtocol.REQUEST_SHUTDOWN) {
                continue;
            }
            final Socket socket;
            if (protocol == null || protocol.getChannel() == null)
                socket = null;
            else
                socket = protocol.getChannel().socket;
            if (socket != null && !socket.isClosed() && !socket.isInputShutdown()) {
                try {
                    OLogManager.instance().debug(this, "Closing input socket of thread %s", protocol);
                    if (// An SSLSocket will throw an UnsupportedOperationException.
                    !(socket instanceof SSLSocket))
                        socket.shutdownInput();
                } catch (IOException e) {
                    OLogManager.instance().debug(this, "Error on closing connection of %s client during shutdown", e, entry.getValue().getRemoteAddress());
                }
            }
            if (protocol.isAlive()) {
                if (protocol instanceof ONetworkProtocolBinary && ((ONetworkProtocolBinary) protocol).getRequestType() == -1) {
                    try {
                        OLogManager.instance().debug(this, "Closing socket of thread %s", protocol);
                        protocol.getChannel().close();
                    } catch (Exception e) {
                        OLogManager.instance().debug(this, "Error during chanel close at shutdown", e);
                    }
                    OLogManager.instance().debug(this, "Sending interrupt signal to thread %s", protocol);
                    protocol.interrupt();
                }
                toWait.add(protocol);
            }
        }
    }
    for (ONetworkProtocol protocol : toWait) {
        try {
            protocol.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Entry(java.util.Map.Entry) ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 3 with ONetworkProtocol

use of com.orientechnologies.orient.server.network.protocol.ONetworkProtocol in project orientdb by orientechnologies.

the class OServer method activate.

@SuppressWarnings("unchecked")
public OServer activate() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    try {
        serverSecurity = new ODefaultServerSecurity(this, serverCfg);
        Orient.instance().setSecurity(serverSecurity);
        // Checks to see if the OrientDB System Database exists and creates it if not.
        // Make sure this happens after setSecurityFactory() is called.
        initSystemDatabase();
        for (OServerLifecycleListener l : lifecycleListeners) l.onBeforeActivate();
        final OServerConfiguration configuration = serverCfg.getConfiguration();
        tokenHandler = new OTokenHandlerImpl(this);
        if (configuration.network != null) {
            // REGISTER/CREATE SOCKET FACTORIES
            if (configuration.network.sockets != null) {
                for (OServerSocketFactoryConfiguration f : configuration.network.sockets) {
                    Class<? extends OServerSocketFactory> fClass = (Class<? extends OServerSocketFactory>) loadClass(f.implementation);
                    OServerSocketFactory factory = fClass.newInstance();
                    try {
                        factory.config(f.name, f.parameters);
                        networkSocketFactories.put(f.name, factory);
                    } catch (OConfigurationException e) {
                        OLogManager.instance().error(this, "Error creating socket factory", e);
                    }
                }
            }
            // REGISTER PROTOCOLS
            for (OServerNetworkProtocolConfiguration p : configuration.network.protocols) networkProtocols.put(p.name, (Class<? extends ONetworkProtocol>) loadClass(p.implementation));
            // STARTUP LISTENERS
            for (OServerNetworkListenerConfiguration l : configuration.network.listeners) networkListeners.add(new OServerNetworkListener(this, networkSocketFactories.get(l.socket), l.ipAddress, l.portRange, l.protocol, networkProtocols.get(l.protocol), l.parameters, l.commands));
        } else
            OLogManager.instance().warn(this, "Network configuration was not found");
        try {
            loadStorages();
            loadUsers();
            loadDatabases();
        } catch (IOException e) {
            final String message = "Error on reading server configuration";
            OLogManager.instance().error(this, message, e);
            throw OException.wrapException(new OConfigurationException(message), e);
        }
        registerPlugins();
        for (OServerLifecycleListener l : lifecycleListeners) l.onAfterActivate();
        running = true;
        String httpAddress = "localhost:2480";
        for (OServerNetworkListener listener : getNetworkListeners()) {
            if (listener.getProtocolType().getName().equals(ONetworkProtocolHttpDb.class.getName()))
                httpAddress = listener.getListeningAddress(true);
        }
        OLogManager.instance().info(this, "OrientDB Studio available at $ANSI{blue http://%s/studio/index.html}", httpAddress);
        OLogManager.instance().info(this, "$ANSI{green:italic OrientDB Server is active} v" + OConstants.getVersion() + ".");
    } catch (ClassNotFoundException e) {
        running = false;
        throw e;
    } catch (InstantiationException e) {
        running = false;
        throw e;
    } catch (IllegalAccessException e) {
        running = false;
        throw e;
    } catch (RuntimeException e) {
        running = false;
        throw e;
    } finally {
        startupLatch.countDown();
    }
    return this;
}
Also used : OServerSocketFactory(com.orientechnologies.orient.server.network.OServerSocketFactory) IOException(java.io.IOException) OTokenHandlerImpl(com.orientechnologies.orient.server.token.OTokenHandlerImpl) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ODefaultServerSecurity(com.orientechnologies.orient.server.security.ODefaultServerSecurity) ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) OServerNetworkListener(com.orientechnologies.orient.server.network.OServerNetworkListener) ONetworkProtocolHttpDb(com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpDb)

Example 4 with ONetworkProtocol

use of com.orientechnologies.orient.server.network.protocol.ONetworkProtocol in project orientdb by orientechnologies.

the class OServerNetworkListener method run.

@Override
public void run() {
    try {
        while (active) {
            try {
                // listen for and accept a client connection to serverSocket
                final Socket socket = serverSocket.accept();
                final int max = OGlobalConfiguration.NETWORK_MAX_CONCURRENT_SESSIONS.getValueAsInteger();
                int conns = server.getClientConnectionManager().getTotal();
                if (conns >= max) {
                    server.getClientConnectionManager().cleanExpiredConnections();
                    conns = server.getClientConnectionManager().getTotal();
                    if (conns >= max) {
                        // MAXIMUM OF CONNECTIONS EXCEEDED
                        OLogManager.instance().warn(this, "Reached maximum number of concurrent connections (max=%d, current=%d), reject incoming connection from %s", max, conns, socket.getRemoteSocketAddress());
                        socket.close();
                        // PAUSE CURRENT THREAD TO SLOW DOWN ANY POSSIBLE ATTACK
                        Thread.sleep(100);
                        continue;
                    }
                }
                socket.setPerformancePreferences(0, 2, 1);
                if (socketBufferSize > 0) {
                    socket.setSendBufferSize(socketBufferSize);
                    socket.setReceiveBufferSize(socketBufferSize);
                }
                // CREATE A NEW PROTOCOL INSTANCE
                final ONetworkProtocol protocol = protocolType.newInstance();
                // CONFIGURE THE PROTOCOL FOR THE INCOMING CONNECTION
                protocol.config(this, server, socket, configuration);
            } catch (Throwable e) {
                if (active)
                    OLogManager.instance().error(this, "Error on client connection", e);
            } finally {
            }
        }
    } finally {
        try {
            if (serverSocket != null && !serverSocket.isClosed())
                serverSocket.close();
        } catch (IOException ioe) {
        }
    }
}
Also used : ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) IOException(java.io.IOException)

Example 5 with ONetworkProtocol

use of com.orientechnologies.orient.server.network.protocol.ONetworkProtocol in project orientdb by orientechnologies.

the class OClientConnectionManager method killAllChannels.

public void killAllChannels() {
    for (Map.Entry<Integer, OClientConnection> entry : connections.entrySet()) {
        try {
            ONetworkProtocol protocol = entry.getValue().getProtocol();
            protocol.getChannel().close();
            final Socket socket;
            if (protocol == null || protocol.getChannel() == null)
                socket = null;
            else
                socket = protocol.getChannel().socket;
            if (socket != null && !socket.isClosed() && !socket.isInputShutdown()) {
                if (// An SSLSocket will throw an UnsupportedOperationException.
                !(socket instanceof SSLSocket))
                    socket.shutdownInput();
            }
        } catch (Exception e) {
            OLogManager.instance().debug(this, "Error on killing connection to %s client", e, entry.getValue().getRemoteAddress());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SSLSocket(javax.net.ssl.SSLSocket) ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException)

Aggregations

ONetworkProtocol (com.orientechnologies.orient.server.network.protocol.ONetworkProtocol)5 IOException (java.io.IOException)5 OException (com.orientechnologies.common.exception.OException)3 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)3 Socket (java.net.Socket)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SSLSocket (javax.net.ssl.SSLSocket)2 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)1 OServerNetworkListener (com.orientechnologies.orient.server.network.OServerNetworkListener)1 OServerSocketFactory (com.orientechnologies.orient.server.network.OServerSocketFactory)1 ONetworkProtocolBinary (com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary)1 ONetworkProtocolHttpDb (com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpDb)1 ODefaultServerSecurity (com.orientechnologies.orient.server.security.ODefaultServerSecurity)1 OTokenHandlerImpl (com.orientechnologies.orient.server.token.OTokenHandlerImpl)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1