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());
}
}
}
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();
}
}
}
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);
}
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);
}
Aggregations