Search in sources :

Example 1 with ONetworkProtocolBinary

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

the class OClientConnectionManager method pushDistribCfg2Clients.

/**
 * Pushes the distributed configuration to all the connected clients.
 */
public void pushDistribCfg2Clients(final ODocument iConfig) {
    if (iConfig == null)
        return;
    final Set<String> pushed = new HashSet<String>();
    for (OClientConnection c : connections.values()) {
        if (!c.getData().supportsPushMessages)
            continue;
        try {
            final String remoteAddress = c.getRemoteAddress();
            if (pushed.contains(remoteAddress))
                // ALREADY SENT: JUMP IT
                continue;
        } catch (Exception e) {
            // SOCKET EXCEPTION SKIP IT
            continue;
        }
        if (!(c.getProtocol() instanceof ONetworkProtocolBinary) || c.getData().serializationImpl == null)
            // INVOLVE ONLY BINARY PROTOCOLS
            continue;
        final ONetworkProtocolBinary p = (ONetworkProtocolBinary) c.getProtocol();
        final OChannelBinary channel = p.getChannel();
        final ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(c.getData().serializationImpl);
        if (ser == null)
            return;
        final byte[] content = ser.toStream(iConfig, false);
        try {
            // TRY ACQUIRING THE LOCK FOR MAXIMUM 3 SECS TO AVOID TO FREEZE CURRENT THREAD
            if (channel.tryAcquireWriteLock(TIMEOUT_PUSH)) {
                try {
                    channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                    channel.writeInt(Integer.MIN_VALUE);
                    channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG);
                    channel.writeBytes(content);
                    channel.flush();
                    pushed.add(c.getRemoteAddress());
                    OLogManager.instance().debug(this, "Sent updated cluster configuration to the remote client %s", c.getRemoteAddress());
                } finally {
                    channel.releaseWriteLock();
                }
            } else {
                OLogManager.instance().info(this, "Timeout on sending updated cluster configuration to the remote client %s", c.getRemoteAddress());
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, c.getRemoteAddress());
        }
    }
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException)

Example 2 with ONetworkProtocolBinary

use of com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary 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 ONetworkProtocolBinary

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

the class OLiveCommandResultListenerTest method before.

@Before
public void before() throws IOException {
    MockitoAnnotations.initMocks(this);
    db = new ODatabaseDocumentTx("memory:" + OLiveCommandResultListenerTest.class.getSimpleName());
    db.create();
    OClientConnectionManager manager = new OClientConnectionManager(server);
    protocol = new ONetworkProtocolBinary();
    protocol.initVariables(server, channelBinary);
    connection = manager.connect(protocol);
    OTokenHandlerImpl tokenHandler = new OTokenHandlerImpl(server);
    byte[] token = tokenHandler.getSignedBinaryToken(db, db.getUser(), connection.getData());
    connection = manager.connect(protocol, connection, token, tokenHandler);
    connection.setDatabase(db);
    connection.getData().serializationImpl = ORecordSerializerNetwork.NAME;
    Mockito.when(server.getClientConnectionManager()).thenReturn(manager);
}
Also used : ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OTokenHandlerImpl(com.orientechnologies.orient.server.token.OTokenHandlerImpl) Before(org.junit.Before)

Example 4 with ONetworkProtocolBinary

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

the class OClientConnectionTest method testAlreadyAuthenticatedButNotOnSpecificConnection.

@Test(expected = OTokenSecurityException.class)
public void testAlreadyAuthenticatedButNotOnSpecificConnection() throws IOException {
    OClientConnection conn = new OClientConnection(1, null);
    OTokenHandler handler = new OTokenHandlerImpl(null);
    byte[] tokenBytes = handler.getSignedBinaryToken(db, db.getUser(), conn.getData());
    conn.validateSession(tokenBytes, handler, protocol);
    assertTrue(conn.getTokenBased());
    assertEquals(tokenBytes, conn.getTokenBytes());
    assertNotNull(conn.getToken());
    // second validation don't need token
    ONetworkProtocolBinary otherConn = Mockito.mock(ONetworkProtocolBinary.class);
    conn.validateSession(null, handler, otherConn);
}
Also used : ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) OTokenHandlerImpl(com.orientechnologies.orient.server.token.OTokenHandlerImpl) Test(org.junit.Test)

Aggregations

ONetworkProtocolBinary (com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary)4 OException (com.orientechnologies.common.exception.OException)2 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)2 OTokenHandlerImpl (com.orientechnologies.orient.server.token.OTokenHandlerImpl)2 IOException (java.io.IOException)2 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 ORecordSerializer (com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)1 OChannelBinary (com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary)1 ONetworkProtocol (com.orientechnologies.orient.server.network.protocol.ONetworkProtocol)1 Socket (java.net.Socket)1 Entry (java.util.Map.Entry)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SSLSocket (javax.net.ssl.SSLSocket)1 Before (org.junit.Before)1 Test (org.junit.Test)1