Search in sources :

Example 1 with OClientConnection

use of com.orientechnologies.orient.server.OClientConnection in project orientdb by orientechnologies.

the class OLiveCommandResultListener method onLiveResult.

public void onLiveResult(int iToken, ORecordOperation iOp) throws OException {
    boolean sendFail = true;
    do {
        List<OClientConnection> connections = session.getConnections();
        if (connections.size() == 0) {
            try {
                ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                OLogManager.instance().warn(this, "Unsubscribing live query for connection " + connection);
                OLiveQueryHook.unsubscribe(iToken, db);
            } catch (Exception e) {
                OLogManager.instance().warn(this, "Unsubscribing live query for connection " + connection, e);
            }
            break;
        }
        OClientConnection curConnection = connections.get(0);
        ONetworkProtocolBinary protocol = (ONetworkProtocolBinary) curConnection.getProtocol();
        OChannelBinary channel = protocol.getChannel();
        try {
            channel.acquireWriteLock();
            try {
                ByteArrayOutputStream content = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(content);
                out.writeByte('r');
                out.writeByte(iOp.type);
                out.writeInt(iToken);
                out.writeByte(ORecordInternal.getRecordType(iOp.getRecord()));
                writeVersion(out, iOp.getRecord().getVersion());
                writeRID(out, (ORecordId) iOp.getRecord().getIdentity());
                writeBytes(out, protocol.getRecordBytes(connection, iOp.getRecord()));
                channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                channel.writeInt(Integer.MIN_VALUE);
                channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY);
                channel.writeBytes(content.toByteArray());
                channel.flush();
            } finally {
                channel.releaseWriteLock();
            }
            sendFail = false;
        } catch (IOException e) {
            session.removeConnection(curConnection);
            connections = session.getConnections();
            if (connections.isEmpty()) {
                ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                OLiveQueryHook.unsubscribe(iToken, db);
                break;
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, protocol.getRemoteAddress());
            protocol.getServer().getClientConnectionManager().disconnect(connection);
            OLiveQueryHook.unsubscribe(iToken, connection.getDatabase());
            break;
        }
    } while (sendFail);
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) DataOutputStream(java.io.DataOutputStream) OClientConnection(com.orientechnologies.orient.server.OClientConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException)

Example 2 with OClientConnection

use of com.orientechnologies.orient.server.OClientConnection in project orientdb by orientechnologies.

the class OServerCommandPostKillDbConnection method doPost.

private void doPost(OHttpRequest iRequest, OHttpResponse iResponse, String db, String command) throws IOException {
    final List<OClientConnection> connections = server.getClientConnectionManager().getConnections();
    for (OClientConnection connection : connections) {
        if (connection.getProtocol() instanceof ONetworkProtocolHttpAbstract) {
            final ONetworkProtocolHttpAbstract http = (ONetworkProtocolHttpAbstract) connection.getProtocol();
            final OHttpRequest req = http.getRequest();
            if (req != null && req != iRequest && req.sessionId.equals(iRequest.sessionId)) {
                server.getClientConnectionManager().interrupt(connection.getId());
            }
        }
    }
    iResponse.send(OHttpUtils.STATUS_OK_NOCONTENT_CODE, OHttpUtils.STATUS_OK_NOCONTENT_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
}
Also used : OHttpRequest(com.orientechnologies.orient.server.network.protocol.http.OHttpRequest) ONetworkProtocolHttpAbstract(com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpAbstract) OClientConnection(com.orientechnologies.orient.server.OClientConnection)

Example 3 with OClientConnection

use of com.orientechnologies.orient.server.OClientConnection in project orientdb by orientechnologies.

the class OLiveCommandResultListener method onUnsubscribe.

@Override
public void onUnsubscribe(int iLiveToken) {
    boolean sendFail = true;
    do {
        List<OClientConnection> connections = session.getConnections();
        if (connections.size() == 0) {
            break;
        }
        ONetworkProtocolBinary protocol = (ONetworkProtocolBinary) connections.get(0).getProtocol();
        OChannelBinary channel = protocol.getChannel();
        try {
            channel.acquireWriteLock();
            try {
                ByteArrayOutputStream content = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(content);
                out.writeByte('u');
                out.writeInt(iLiveToken);
                channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                channel.writeInt(Integer.MIN_VALUE);
                channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY);
                channel.writeBytes(content.toByteArray());
                channel.flush();
            } finally {
                channel.releaseWriteLock();
            }
            sendFail = false;
        } catch (IOException e) {
            connections = session.getConnections();
            if (connections.isEmpty()) {
                break;
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, protocol.getRemoteAddress());
            protocol.getServer().getClientConnectionManager().disconnect(connection);
            break;
        }
    } while (sendFail);
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) DataOutputStream(java.io.DataOutputStream) OClientConnection(com.orientechnologies.orient.server.OClientConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException)

Example 4 with OClientConnection

use of com.orientechnologies.orient.server.OClientConnection in project orientdb by orientechnologies.

the class ONetworkProtocolBinary method execute.

@Override
protected void execute() throws Exception {
    requestType = -1;
    // do not remove this or we will get deadlock upon shutdown.
    if (isShutdownFlag())
        return;
    clientTxId = 0;
    okSent = false;
    try {
        requestType = channel.readByte();
        OChannelBinaryProtocol.checkRequestTypeRange(channel, requestType);
        clientTxId = channel.readInt();
        // GET THE CONNECTION IF EXIST
        OClientConnection connection = server.getClientConnectionManager().getConnection(clientTxId, this);
        if (isHandshaking(requestType)) {
            handshakeRequest(connection, requestType, clientTxId);
        } else if (isDistributed(requestType)) {
            distributedRequest(connection, requestType, clientTxId);
        } else
            sessionRequest(connection, requestType, clientTxId);
    } catch (Exception e) {
        // if an exception arrive to this point we need to kill the current socket.
        sendShutdown();
        throw e;
    }
}
Also used : OClientConnection(com.orientechnologies.orient.server.OClientConnection) OLockException(com.orientechnologies.common.concur.lock.OLockException) OException(com.orientechnologies.common.exception.OException) SocketException(java.net.SocketException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException) IOException(java.io.IOException)

Aggregations

OClientConnection (com.orientechnologies.orient.server.OClientConnection)4 OException (com.orientechnologies.common.exception.OException)3 IOException (java.io.IOException)3 OChannelBinary (com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OLockException (com.orientechnologies.common.concur.lock.OLockException)1 OIOException (com.orientechnologies.common.io.OIOException)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)1 OHttpRequest (com.orientechnologies.orient.server.network.protocol.http.OHttpRequest)1 ONetworkProtocolHttpAbstract (com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpAbstract)1 SocketException (java.net.SocketException)1