public void connectionRemoved(Connection connection) { if (connection.isClient() && connection instanceof TcpIpConnection && nodeEngine.isActive()) { final ClientEndpoint endpoint = endpoints.get(connection); if (endpoint != null && node.getLocalMember().getUuid().equals(endpoint.getPrincipal().getOwnerUuid())) { removeEndpoint(connection, true); if (!endpoint.isFirstConnection()) { return; } NodeEngine nodeEngine = node.nodeEngine; final Collection<MemberImpl> memberList = nodeEngine.getClusterService().getMemberList(); for (MemberImpl member : memberList) { final ClientDisconnectionOperation op = new ClientDisconnectionOperation(endpoint.getUuid()); op.setNodeEngine(nodeEngine) .setServiceName(SERVICE_NAME) .setService(this) .setResponseHandler(ResponseHandlerFactory.createEmptyResponseHandler()); if (member.localMember()) { nodeEngine.getOperationService().runOperation(op); } else { nodeEngine.getOperationService().send(op, member.getAddress()); } } } } }
private void sendClientEvent(ClientEndpoint endpoint) { if (endpoint.isFirstConnection()) { final EventService eventService = nodeEngine.getEventService(); final Collection<EventRegistration> regs = eventService.getRegistrations(SERVICE_NAME, SERVICE_NAME); eventService.publishEvent(SERVICE_NAME, regs, endpoint, endpoint.getUuid().hashCode()); } }
Set<ClientEndpoint> getEndpoints(String uuid) { Set<ClientEndpoint> endpointSet = new HashSet<ClientEndpoint>(); for (ClientEndpoint endpoint : endpoints.values()) { if (uuid.equals(endpoint.getUuid())) { endpointSet.add(endpoint); } } return endpointSet; }
public void sendResponse(ClientEndpoint endpoint, Object response) { if (response instanceof Throwable) { response = ClientExceptionConverters.get(endpoint.getClientType()).convert((Throwable) response); } final Data resultData = response != null ? serializationService.toData(response) : NULL; Connection conn = endpoint.getConnection(); conn.write(new DataAdapter(resultData, serializationService.getSerializationContext())); }
void removeEndpoints(String memberUuid) { Iterator<ClientEndpoint> iterator = endpoints.values().iterator(); while (iterator.hasNext()) { ClientEndpoint endpoint = iterator.next(); String ownerUuid = endpoint.getPrincipal().getOwnerUuid(); if (memberUuid.equals(ownerUuid)) { iterator.remove(); removeEndpoint(endpoint, true); } } }
public void run() { final Connection conn = packet.getConn(); final ClientEndpoint endpoint = getEndpoint(conn); ClientRequest request = null; try { final Data data = packet.getData(); request = (ClientRequest) serializationService.toObject(data); if (endpoint.isAuthenticated() || request instanceof AuthenticationRequest) { request.setEndpoint(endpoint); final String serviceName = request.getServiceName(); if (serviceName != null) { final Object service = nodeEngine.getService(serviceName); if (service == null) { if (nodeEngine.isActive()) { throw new IllegalArgumentException( "No service registered with name: " + serviceName); } throw new HazelcastInstanceNotActiveException(); } request.setService(service); } request.setClientEngine(ClientEngineImpl.this); final SecurityContext securityContext = getSecurityContext(); if (securityContext != null && request instanceof SecureRequest) { final Permission permission = ((SecureRequest) request).getRequiredPermission(); if (permission != null) { securityContext.checkPermission(endpoint.getSubject(), permission); } } request.process(); } else { Exception exception; if (nodeEngine.isActive()) { String message = "Client " + conn + " must authenticate before any operation."; logger.severe(message); exception = new AuthenticationException(message); } else { exception = new HazelcastInstanceNotActiveException(); } sendResponse(endpoint, exception); removeEndpoint(conn); } } catch (Throwable e) { final Level level = nodeEngine.isActive() ? Level.SEVERE : Level.FINEST; String message = request != null ? "While executing request: " + request + " -> " + e.getMessage() : e.getMessage(); logger.log(level, message, e); sendResponse(endpoint, e); } }
public void dispatchEvent(ClientEndpoint event, ClientListener listener) { if (event.isAuthenticated()) { listener.clientConnected(event); } else { listener.clientDisconnected(event); } }
public void shutdown() { for (ClientEndpoint endpoint : endpoints.values()) { try { endpoint.destroy(); } catch (LoginException e) { logger.finest(e.getMessage()); } try { final Connection conn = endpoint.getConnection(); if (conn.live()) { conn.close(); } } catch (Exception e) { logger.finest(e); } } endpoints.clear(); }
void bind(final ClientEndpoint endpoint) { final Connection conn = endpoint.getConnection(); if (conn instanceof TcpIpConnection) { Address address = new Address(conn.getRemoteSocketAddress()); TcpIpConnectionManager connectionManager = (TcpIpConnectionManager) node.getConnectionManager(); connectionManager.bind((TcpIpConnection) conn, address, null, false); } sendClientEvent(endpoint); }
private void destroyEndpoint(ClientEndpoint endpoint, boolean closeImmediately) { if (endpoint != null) { logger.info("Destroying " + endpoint); try { endpoint.destroy(); } catch (LoginException e) { logger.warning(e); } final Connection connection = endpoint.getConnection(); if (closeImmediately) { try { connection.close(); } catch (Throwable e) { logger.warning("While closing client connection: " + connection, e); } } else { nodeEngine .getExecutionService() .schedule( new Runnable() { public void run() { if (connection.live()) { try { connection.close(); } catch (Throwable e) { logger.warning("While closing client connection: " + e.toString()); } } } }, 1111, TimeUnit.MILLISECONDS); } sendClientEvent(endpoint); } }
void removeEndpoint(final ClientEndpoint endpoint, boolean closeImmediately) { endpoints.remove(endpoint.getConnection()); LOGGER.info("Destroying " + endpoint); try { endpoint.destroy(); } catch (LoginException e) { LOGGER.warning(e); } final Connection connection = endpoint.getConnection(); if (closeImmediately) { try { connection.close(); } catch (Throwable e) { LOGGER.warning("While closing client connection: " + connection, e); } } else { nodeEngine .getExecutionService() .schedule( new Runnable() { public void run() { if (connection.live()) { try { connection.close(); } catch (Throwable e) { LOGGER.warning("While closing client connection: " + e.toString()); } } } }, DESTROY_ENDPOINT_DELAY_MS, TimeUnit.MILLISECONDS); } clientEngine.sendClientEvent(endpoint); }