Session.java 5.8 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14
package org.jivesoftware.messenger;

import org.jivesoftware.messenger.auth.AuthToken;
15 16
import org.xmpp.packet.JID;

Matt Tucker's avatar
Matt Tucker committed
17 18 19 20
import java.util.Date;

/**
 * The session represents a connection between the server and a client (c2s) or
21 22 23
 * another server (s2s) as well as a connection with a component. Authentication and
 * user accounts are associated with c2s connections while s2s has an optional authentication
 * association but no single user user.<p>
Matt Tucker's avatar
Matt Tucker committed
24 25 26
 *
 * Obtain object managers from the session in order to access server resources.
 *
27
 * @author Gaston Dombiak
Matt Tucker's avatar
Matt Tucker committed
28
 */
29 30 31 32 33 34
public abstract class Session implements RoutableChannelHandler {

    /**
     * The utf-8 charset for decoding and encoding Jabber packet streams.
     */
    protected static String CHARSET = "UTF-8";
Matt Tucker's avatar
Matt Tucker committed
35 36 37 38 39 40

    public static final int STATUS_CLOSED = -1;
    public static final int STATUS_CONNECTED = 1;
    public static final int STATUS_STREAMING = 2;
    public static final int STATUS_AUTHENTICATED = 3;

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    /**
     * The Address this session is authenticated as.
     */
    private JID address;

    /**
     * The stream id for this session (random and unique).
     */
    private StreamID streamID;

    /**
     * The current session status.
     */
    protected int status = STATUS_CONNECTED;

    /**
     * The connection that this session represents.
     */
    protected Connection conn;

    /**
     * The authentication token for this session.
     */
    protected AuthToken authToken;

    protected SessionManager sessionManager;

    private String serverName;

    private Date startDate = new Date();

    private long lastActiveDate;
    private long clientPacketCount = 0;
    private long serverPacketCount = 0;

    /**
     * Creates a session with an underlying connection and permission protection.
     *
     * @param connection The connection we are proxying
     */
    public Session(String serverName, Connection connection, StreamID streamID) {
        conn = connection;
        this.streamID = streamID;
        this.serverName = serverName;
        String id = streamID.getID();
        this.address = new JID(null, serverName, id);
        this.sessionManager = SessionManager.getInstance();
    }

Matt Tucker's avatar
Matt Tucker committed
90 91 92 93 94 95 96 97
    /**
      * Obtain the address of the user. The address is used by services like the core
      * server packet router to determine if a packet should be sent to the handler.
      * Handlers that are working on behalf of the server should use the generic server
      * hostname address (e.g. server.com).
      *
      * @return the address of the packet handler.
      */
98 99 100
    public JID getAddress() {
        return address;
    }
Matt Tucker's avatar
Matt Tucker committed
101

Derek DeMoro's avatar
Derek DeMoro committed
102 103 104 105 106 107
    /**
     * Sets the new address of this session. The address is used by services like the core
     * server packet router to determine if a packet should be sent to the handler.
     * Handlers that are working on behalf of the server should use the generic server
     * hostname address (e.g. server.com).
     */
108 109 110
    public void setAddress(JID address){
        this.address = address;
    }
Derek DeMoro's avatar
Derek DeMoro committed
111

Matt Tucker's avatar
Matt Tucker committed
112 113 114 115 116
    /**
     * Returns the connection associated with this Session.
     *
     * @return The connection for this session
     */
117 118 119
    public Connection getConnection() {
        return conn;
    }
Matt Tucker's avatar
Matt Tucker committed
120 121 122 123 124 125

    /**
     * Obtain the current status of this session.
     *
     * @return The status code for this session
     */
126 127 128
    public int getStatus() {
        return status;
    }
Matt Tucker's avatar
Matt Tucker committed
129 130 131 132 133 134 135 136

    /**
     * Set the new status of this session. Setting a status may trigger
     * certain events to occur (setting a closed status will close this
     * session).
     *
     * @param status The new status code for this session
     */
137 138 139
    public void setStatus(int status) {
        this.status = status;
    }
Matt Tucker's avatar
Matt Tucker committed
140 141 142 143 144 145 146

    /**
     * Obtain the stream ID associated with this sesison. Stream ID's are generated by the server
     * and should be unique and random.
     *
     * @return This session's assigned stream ID
     */
147 148 149
    public StreamID getStreamID() {
        return streamID;
    }
Matt Tucker's avatar
Matt Tucker committed
150 151 152 153 154 155

    /**
     * Obtain the name of the server this session belongs to.
     *
     * @return the server name.
     */
156 157 158
    public String getServerName() {
        return serverName;
    }
Matt Tucker's avatar
Matt Tucker committed
159 160 161 162 163 164

    /**
     * Obtain the date the session was created.
     *
     * @return the session's creation date.
     */
165 166 167
    public Date getCreationDate() {
        return startDate;
    }
Matt Tucker's avatar
Matt Tucker committed
168 169 170 171 172 173

    /**
     * Obtain the time the session last had activity.
     *
     * @return The last time the session received activity.
     */
174 175 176
    public Date getLastActiveDate() {
        return new Date(lastActiveDate);
    }
Matt Tucker's avatar
Matt Tucker committed
177 178 179 180

    /**
     * Obtain the number of packets sent from the client to the server.
     */
181 182 183 184
    public void incrementClientPacketCount() {
        clientPacketCount++;
        lastActiveDate = System.currentTimeMillis();
    }
Matt Tucker's avatar
Matt Tucker committed
185 186 187 188

    /**
     * Obtain the number of packets sent from the server to the client.
     */
189 190 191 192
    public void incrementServerPacketCount() {
        serverPacketCount++;
        lastActiveDate = System.currentTimeMillis();
    }
Matt Tucker's avatar
Matt Tucker committed
193 194 195 196 197 198

    /**
     * Obtain the number of packets sent from the client to the server.
     *
     * @return The number of packets sent from the client to the server.
     */
199 200 201
    public long getNumClientPackets() {
        return clientPacketCount;
    }
Matt Tucker's avatar
Matt Tucker committed
202 203 204 205 206 207

    /**
     * Obtain the number of packets sent from the server to the client.
     *
     * @return The number of packets sent from the server to the client.
     */
208 209 210
    public long getNumServerPackets() {
        return serverPacketCount;
    }
211

Gaston Dombiak's avatar
Gaston Dombiak committed
212 213 214
    public String toString() {
        return super.toString() + " status: " + status + " address: " + address + " id: " + streamID;
    }
Matt Tucker's avatar
Matt Tucker committed
215
}