Package tigase.xmpp

Examples of tigase.xmpp.XMPPSession


      }
      if (ClusterMethods.SESSION_TRANSFER.toString().equals(clel.getMethodName())) {
        Set<String> cancel_nodes = new LinkedHashSet<String>();
        String node_found = null;
        String userId = clel.getMethodParam(USER_ID);
        XMPPSession session = getSession(userId);
        String connectionId = clel.getMethodParam(CONNECTION_ID);
        XMPPResourceConnection conn = null;
        if (session != null) {
          conn = session.getResourceForConnectionId(connectionId);
        }
              //ClusterElement result = null;
        if (getComponentId().equals(clel.getFirstNode())) {
          log.finest("Session transfer request came back to me....");
          // Ok, the request has came back to me, let's look what to do now....
          // First let's check whether any of the nodes has decided to accept
          // the transfer:
          // This is a set of nodes we will send a transfer cancell later on
          long time = 0;
          long hash = 0;
          for (String node: clel.getVisitedNodes()) {
            if (clel.getMethodResultVal(node + "-CREATED") != null) {
              long tmp_time = clel.getMethodResultVal(node + "-" + AUTH_TIME, 0);
              long tmp_hash = clel.getMethodResultVal(
                node + "-HASH-" + XMPP_SESSION_ID, 0);
              log.finest("Node: " + node + " responded with: "
                + clel.getMethodResultVal(node + "-CREATED")
                + ", tmp_time: " + tmp_time
                + ", tmp_hash: " + tmp_hash);
              boolean replace_node = false;
              if (tmp_time == time) {
                if (tmp_hash > hash) {
                  replace_node = true;
                }
              } else {
                if (tmp_time > time) {
                  replace_node = true;
                }
              }
              if (replace_node) {
                if (node_found != null) {
                  log.finest("Addeding node to cancel_nodes: " + node_found);
                  cancel_nodes.add(node_found);
                }
                node_found = node;
                hash = tmp_hash;
                time = tmp_time;
              } else {
                log.finest("Addeding node to cancel_nodes: " + node);
                cancel_nodes.add(node);
              }
            }
          }
          if (node_found != null) {
            // This is where we want to do the user session transfer then
            if (session != null) {
              if (conn != null) {
                Map<String, String> res_vals = new LinkedHashMap<String, String>();
                res_vals.put(TRANSFER, "accept");
                ClusterElement result = clel.createMethodResponse(getComponentId(),
                  node_found, "result", res_vals);
                fastAddOutPacket(new Packet(result.getClusterElement()));
                conn.putSessionData("redirect-to", node_found);
                sendAllOnHold(conn);

                String xmpp_sessionId = clel.getMethodParam(XMPP_SESSION_ID);
                Packet redirect = Command.REDIRECT.getPacket(node_found,
                  connectionId, StanzaType.set, "1", "submit");
                Command.addFieldValue(redirect, "session-id", xmpp_sessionId);
                fastAddOutPacket(redirect);
              } else {
                // Ups, the user has disconnected, send session transfer to all
                log.finest("Addeding node to cancel_nodes: " + node_found);
                cancel_nodes.add(node_found);
                log.fine("The user connection doesn't exist: " + userId
                  + ", connectionId: " + connectionId);
              }
            } else {
              // Ups, the user has disconnected, send session transfer to all
              log.finest("Addeding node to cancel_nodes: " + node_found);
              cancel_nodes.add(node_found);
              log.fine("The user session doesn't exist: " + userId);
            }
          } else {
            // Set status to NORMALL, user is not logged in on any other node.
            if (conn != null) {
              log.finest("Set status to NORMAL and send all ON_HOLD");
              conn.setConnectionStatus(ConnectionStatus.NORMAL);
              sendAllOnHold(conn);
            } else {
              log.fine("The user connection doesn't exist: " + userId
                + ", connectionId: " + connectionId);
            }
          }
          if (cancel_nodes.size() > 0) {
            // Send session transfer to all.
            Map<String, String> res_vals = new LinkedHashMap<String, String>();
            res_vals.put(TRANSFER, "cancel");
            for (String node: cancel_nodes) {
              ClusterElement result = clel.createMethodResponse(getComponentId(),
                node, "result", res_vals);
              log.finest("Sending sesstion transfer CANCEL to node: " + node);
              fastAddOutPacket(new Packet(result.getClusterElement()));
            }
          }
        } else {
          // A request from some other node, maybe the user session should be
          // transfered here...
          ClusterElement result = ClusterElement.createForNextNode(clel,
            cluster_nodes,  getComponentId());
          if (session != null) {
            conn = session.getOldestConnection();
            boolean transfer_in = false;
            switch (conn.getConnectionStatus()) {
            case ON_HOLD:
              long local_auth_time = conn.getAuthTime();
              long remote_auth_time = clel.getMethodParam(AUTH_TIME, 0L);
              if (local_auth_time == remote_auth_time) {
                transfer_in = (conn.getSessionId().hashCode() >
                  clel.getMethodParam(XMPP_SESSION_ID).hashCode());
              } else {
                transfer_in = (local_auth_time > remote_auth_time);
              }
              break;
            case REDIRECT:
              transfer_in = false;
              break;
            case NORMAL:
              transfer_in = true;
              break;
            default:
              break;
            }
            if (transfer_in) {
              addTempSession(clel);
              result.addMethodResult(getComponentId() + "-" + AUTH_TIME,
                "" + conn.getAuthTime());
              result.addMethodResult(getComponentId() + "-HASH-" + XMPP_SESSION_ID,
                "" + conn.getSessionId().hashCode());
              result.addMethodResult(getComponentId() + "-STATUS",
                "" + conn.getConnectionStatus());
              result.addMethodResult(getComponentId() + "-CREATED",
                "" + true);
            }
          } else {
            // Do nothing really, just forward the request to a next node
          }
          fastAddOutPacket(new Packet(result.getClusterElement()));
        }
      }
      break;
    case result:
      if (ClusterMethods.SESSION_TRANSFER.toString().equals(clel.getMethodName())) {
        String transfer = clel.getMethodResultVal(TRANSFER);
        if (transfer == null) {
          log.warning("Incorrect response for the session transfer: "
            + packet.toString());
          return;
        }
        if (transfer.equals("accept")) {
          String userId = clel.getMethodParam(USER_ID);
          XMPPSession session = getSession(userId);
          if (session == null) {
            // Ups, something wrong happened, let's record this in the log
            // file for now...
            log.warning("User session does not exist for the request to complete the user transfer: " + packet.toString());
            return;
          }
          String connectionId = clel.getMethodParam(CONNECTION_ID);
          XMPPResourceConnection conn =
            session.getResourceForConnectionId(connectionId);
          if (conn == null) {
            // Ups, something wrong happened, let's record this in the log
            // file for now...
            log.warning("User connection does not exist for the request to complete the user transfer: " + packet.toString());
            return;
View Full Code Here


  protected XMPPSession getSession(String jid) {
    return sessionsByNodeId.get(JIDUtils.getNodeID(jid));
  }

  private XMPPResourceConnection getResourceConnection(String jid) {
    XMPPSession session = getSession(jid);
    if (session != null) {
      log.finest("Session not null, getting resource for jid: " + jid);
      return session.getResourceConnection(jid);
    } // end of if (session != null)
    return null;
  }
View Full Code Here

    try {
      if (conn.isAuthorized()
        || (conn.getConnectionStatus() == ConnectionStatus.TEMP)) {
        String userId = conn.getUserId();
        log.info("Closing connection for: " + userId);
        XMPPSession session = conn.getParentSession();
        if (session != null) {
          log.info("Found parent session for: " + userId);
          if (session.getActiveResourcesSize() <= 1) {
            session = sessionsByNodeId.remove(userId);
            if (session == null) {
              log.info("UPS can't remove session, not found in map: " + userId);
            } else {
              log.finer("Number of user sessions: " + sessionsByNodeId.size());
            } // end of else
            auth_repository.logout(userId);
          } else {
            StringBuilder sb = new StringBuilder();
            for (XMPPResourceConnection res_con: session.getActiveResources()) {
              sb.append(", res=" + res_con.getResource() + " ("
                + res_con.getConnectionStatus() + ")");
            }
            log.finer("Number of connections is "
              + session.getActiveResourcesSize() + " for the user: " + userId
              + sb.toString());
          } // end of else
        } // end of if (session.getActiveResourcesSize() == 0)
      }
    } catch (NotAuthorizedException e) {
View Full Code Here

  public boolean handlesLocalDomains() {
    return true;
  }

  protected void registerNewSession(String userId, XMPPResourceConnection conn) {
    XMPPSession session = sessionsByNodeId.get(userId);
    if (session == null) {
      session = new XMPPSession(JIDUtils.getNodeNick(userId));
      sessionsByNodeId.put(userId, session);
      log.finest("Created new XMPPSession for: " + userId);
    } // end of if (session == null)
    session.addResourceConnection(conn);
  }
View Full Code Here

  public void handleLogout(String userName, XMPPResourceConnection conn) {
    String domain = conn.getDomain();
    addOutPacket(Command.CLOSE.getPacket(getComponentId(),
        conn.getConnectionId(), StanzaType.set, conn.nextStanzaId()));
    String userId = JIDUtils.getNodeID(userName, domain);
    XMPPSession session = sessionsByNodeId.get(userId);
    if (session != null && session.getActiveResourcesSize() <= 1) {
      sessionsByNodeId.remove(userId);
    } // end of if (session.getActiveResourcesSize() == 0)
  }
View Full Code Here

TOP

Related Classes of tigase.xmpp.XMPPSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.