KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > Session


1 /**
2  * $RCSfile: Session.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/05/23 17:51:50 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger;
13
14 import org.jivesoftware.messenger.auth.AuthToken;
15 import org.xmpp.packet.JID;
16
17 import java.util.Date JavaDoc;
18
19 /**
20  * The session represents a connection between the server and a client (c2s) or
21  * another server (s2s) as well as a connection with a component. Authentication and
22  * user accounts are associated with c2s connections while s2s has an optional authentication
23  * association but no single user user.<p>
24  *
25  * Obtain object managers from the session in order to access server resources.
26  *
27  * @author Gaston Dombiak
28  */

29 public abstract class Session implements RoutableChannelHandler {
30
31     /**
32      * The utf-8 charset for decoding and encoding Jabber packet streams.
33      */

34     protected static String JavaDoc CHARSET = "UTF-8";
35
36     public static final int STATUS_CLOSED = -1;
37     public static final int STATUS_CONNECTED = 1;
38     public static final int STATUS_STREAMING = 2;
39     public static final int STATUS_AUTHENTICATED = 3;
40
41     /**
42      * The Address this session is authenticated as.
43      */

44     private JID address;
45
46     /**
47      * The stream id for this session (random and unique).
48      */

49     private StreamID streamID;
50
51     /**
52      * The current session status.
53      */

54     protected int status = STATUS_CONNECTED;
55
56     /**
57      * The connection that this session represents.
58      */

59     protected Connection conn;
60
61     /**
62      * The authentication token for this session.
63      */

64     protected AuthToken authToken;
65
66     protected SessionManager sessionManager;
67
68     private String JavaDoc serverName;
69
70     private Date JavaDoc startDate = new Date JavaDoc();
71
72     private long lastActiveDate;
73     private long clientPacketCount = 0;
74     private long serverPacketCount = 0;
75
76     /**
77      * Creates a session with an underlying connection and permission protection.
78      *
79      * @param connection The connection we are proxying
80      */

81     public Session(String JavaDoc serverName, Connection connection, StreamID streamID) {
82         conn = connection;
83         this.streamID = streamID;
84         this.serverName = serverName;
85         String JavaDoc id = streamID.getID();
86         this.address = new JID(null, serverName, id);
87         this.sessionManager = SessionManager.getInstance();
88     }
89
90     /**
91       * Obtain the address of the user. The address is used by services like the core
92       * server packet router to determine if a packet should be sent to the handler.
93       * Handlers that are working on behalf of the server should use the generic server
94       * hostname address (e.g. server.com).
95       *
96       * @return the address of the packet handler.
97       */

98     public JID getAddress() {
99         return address;
100     }
101
102     /**
103      * Sets the new address of this session. The address is used by services like the core
104      * server packet router to determine if a packet should be sent to the handler.
105      * Handlers that are working on behalf of the server should use the generic server
106      * hostname address (e.g. server.com).
107      */

108     public void setAddress(JID address){
109         this.address = address;
110     }
111
112     /**
113      * Returns the connection associated with this Session.
114      *
115      * @return The connection for this session
116      */

117     public Connection getConnection() {
118         return conn;
119     }
120
121     /**
122      * Obtain the current status of this session.
123      *
124      * @return The status code for this session
125      */

126     public int getStatus() {
127         return status;
128     }
129
130     /**
131      * Set the new status of this session. Setting a status may trigger
132      * certain events to occur (setting a closed status will close this
133      * session).
134      *
135      * @param status The new status code for this session
136      */

137     public void setStatus(int status) {
138         this.status = status;
139     }
140
141     /**
142      * Obtain the stream ID associated with this sesison. Stream ID's are generated by the server
143      * and should be unique and random.
144      *
145      * @return This session's assigned stream ID
146      */

147     public StreamID getStreamID() {
148         return streamID;
149     }
150
151     /**
152      * Obtain the name of the server this session belongs to.
153      *
154      * @return the server name.
155      */

156     public String JavaDoc getServerName() {
157         return serverName;
158     }
159
160     /**
161      * Obtain the date the session was created.
162      *
163      * @return the session's creation date.
164      */

165     public Date JavaDoc getCreationDate() {
166         return startDate;
167     }
168
169     /**
170      * Obtain the time the session last had activity.
171      *
172      * @return The last time the session received activity.
173      */

174     public Date JavaDoc getLastActiveDate() {
175         return new Date JavaDoc(lastActiveDate);
176     }
177
178     /**
179      * Obtain the number of packets sent from the client to the server.
180      */

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

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

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

208     public long getNumServerPackets() {
209         return serverPacketCount;
210     }
211
212     public String JavaDoc toString() {
213         return super.toString() + " status: " + status + " address: " + address + " id: " + streamID;
214     }
215 }
Popular Tags