Ejemplo n.º 1
0
  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());
          }
        }
      }
    }
  }
Ejemplo n.º 2
0
 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());
   }
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
 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()));
 }
Ejemplo n.º 5
0
    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);
      }
    }
Ejemplo n.º 6
0
 public void dispatchEvent(ClientEndpoint event, ClientListener listener) {
   if (event.isAuthenticated()) {
     listener.clientConnected(event);
   } else {
     listener.clientDisconnected(event);
   }
 }
Ejemplo n.º 7
0
 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();
 }
Ejemplo n.º 8
0
 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);
 }
Ejemplo n.º 9
0
  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);
    }
  }