KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > protocols > comvc > JComVcSession


1 /*
2     =========================================================================
3     Package comvc - Implements the ComVc Protocol.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JComVcSession.java,v 1.7 2007/01/28 17:39:21 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.7 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.comvc;
40 import java.lang.*;
41
42
43
44 public class JComVcSession {
45
46   protected JClientEngine clientEngine;
47   
48   
49
50   private void selfTest( int nTestType, int nUserId, java.lang.String JavaDoc strUserName, java.lang.String JavaDoc strPasswd, long nUserIds[] ) {
51   /**
52      Performs a class self test.
53      Parameters:
54       * nTestType - Type of Test that will be
55         done;
56       * nUserId - The User id on server;
57       * strUserName - UserName to do the Test;
58       * strPasswd - Password of user;
59   */

60     
61     JUserDetails userDetails = new JUserDetails();
62     
63     
64     userDetails.setName( strUserName );
65     userDetails.setNick( strUserName );
66     userDetails.setPassword( strPasswd );
67     userDetails.setUserId( nUserId );
68     
69     setUser( userDetails );
70    
71     switch( nTestType ) {
72       
73       case JComVcConstants.COMVC_CREATE_USER : {
74                                                   if( !createNewUser() )
75                                                     System.out.println( "Create user error" );
76                                                   else
77                                                     System.out.println( "User " + strUserName + " was created. The User Id is " + clientEngine.userDetails.getUserId() );
78                                                } break;
79
80       case JComVcConstants.COMVC_BUDDY_LIST : {
81                                                   for( int nCount = 0; nCount < java.lang.reflect.Array.getLength( nUserIds ); nCount++ )
82                                                     addToContactList( nUserIds[nCount], true );
83                                                }
84                                               
85       case JComVcConstants.COMVC_USER_LOGIN : {
86                                                   if( !login( JComVcConstants.STATUS_ONLINE ) )
87                                                     System.out.println( "User login error" );
88                                                   else
89                                                     System.out.println( "User " + strUserName + " logged in" );
90                                                } break;
91     }
92
93     
94     if( ( ( nTestType == JComVcConstants.COMVC_BUDDY_LIST ) || ( nTestType == JComVcConstants.COMVC_USER_LOGIN ) ) && ( getStatus() != JComVcConstants.STATUS_OFFLINE ) ) {
95       int nCommand = 0;
96       
97       while( nCommand != 'e' ) {
98         System.out.print( "Command : " );
99         
100         try {
101           nCommand = System.in.read();
102         } catch( java.io.IOException JavaDoc e ) {
103           System.err.println( e );
104         }
105         
106         switch( nCommand ) {
107          
108           case 'm' : {
109                         //byte[] aMessage = new byte[255];
110
//byte[] aToUser = new byte[10];
111

112                         //try {
113
// System.out.print( "To UserID : " );
114
// System.in.read( aToUser );
115
// System.out.print( "Message : " );
116
// System.in.read( aMessage );
117
//} catch( java.io.IOException ex ) {
118
// System.err.println( ex );
119
//}
120

121                         //String strUser = new String( aToUser );
122
//String strMsg = new String( aMessage );
123

124                         //sendMessage( Long.parseLong( strUser ), strMsg );
125
sendMessage( 1322237, "Eae manow recebeu ???" );
126                         
127                         //System.out.println( "Message sent to UserID " + strUser );
128
}
129         }
130       }
131       
132     }
133         
134     clientEngine.logout();
135   }
136   
137   public JComVcSession() {
138   /**
139      Constructor. Initializes all JComVcSession
140      class data.
141   */

142       
143     clientEngine = new JClientEngine();
144   }
145   
146   void addComVcEventListener( JComVcEventListener listener ) {
147   /**
148      Add a event listener to this
149      engine class. All events will
150      be dispatch to this listener.
151      Parameter:
152       * listener - Listener to attach
153         in this class;
154   */

155     
156     clientEngine.addComVcEventListener( listener );
157   }
158   
159   public boolean createNewUser() {
160   /**
161      Create a new user. If the new user is created,
162      the function returns true and the field nUserId
163      of JUserDetails is filled, else the function
164      returns false;
165   */

166
167     return clientEngine.createNewUser();
168   }
169   
170   public boolean changePassword( java.lang.String JavaDoc strOldPasswd, java.lang.String JavaDoc strNewPasswd ) {
171   /**
172    * Change the user password.
173    * Parameters:
174    * o strOldPasswd - The user old password;
175    * o strNewPasswd - The user new password;
176    */

177     
178     return clientEngine.changePassword( strOldPasswd, strNewPasswd );
179   }
180   
181   public JBuddyDetails getBuddyDetails( long nUserId ) {
182   /**
183    * Returns the specific user details.
184    * Parameters:
185    * o nUserId - The user id to retrieve
186    * details;
187    */

188    
189     return clientEngine.getBuddyDetails( nUserId );
190   }
191   
192   public boolean login( int nInitialStatus ) {
193   /**
194      Login to server using the user details
195      stored in userDetails member object.
196      Parameters:
197       * nInitialStatus - Sets the initial
198         status of the user;
199   */

200     
201     return clientEngine.login( nInitialStatus );
202   }
203   
204   public boolean logout() {
205   /**
206      Performs the logout in ComVC Server.
207   */

208     
209     return clientEngine.logout();
210   }
211   
212   public void setUser( JUserDetails userDetails ) {
213   /**
214    * Sets the user info for this
215    * connection.
216    * Parameters:
217    * o userDetails - User details class
218    * for this connection.
219    */

220     
221     clientEngine.setUser( userDetails );
222   }
223
224   public JUserDetails getUser() {
225   /**
226    * Retrieve the user details
227    * object of this connection.
228    */

229     
230     return clientEngine.getUser();
231   }
232
233   public boolean setStatus( int nStatus ) {
234   /**
235      Sets the connection engine status.
236      Parameters:
237       * nStatus - The new client Connection
238         status.
239   */

240
241     return clientEngine.setStatus( nStatus );
242   }
243   
244   public int getStatus() {
245   /**
246      Returns the Status of this
247      connection.
248   */

249    
250     return clientEngine.getStatus();
251   }
252   
253   public boolean addToContactList( long nUserId, boolean bRequestAuth ) {
254   /**
255    * Add a user into protocol contact
256    * list.
257    * Parameters:
258    * o nUserId - The UserId that will
259    * be inserted into ContactList;
260    * o bRequestAuth - Request authorization
261    * flag;
262    */

263   
264     return clientEngine.addToContactList( nUserId, bRequestAuth );
265   }
266   
267   boolean removeFromContactList( long nUserId ) {
268   /**
269    * remove a user from protocol contact
270    * list.
271    * Parameters:
272    * o nUserId - The UserId that will
273    * be removed from ContactList;
274    */

275     
276     return clientEngine.removeFromContactList( nUserId );
277   }
278   
279   public void clearContactList() {
280   /**
281    * Clear all contact list.
282    */

283     
284     clientEngine.clearContactList();
285   }
286   
287   public boolean authorizeUser( long nUserId ) {
288   /**
289    * Authorize another user to put in contactlist
290    * in your ContactList.
291    * Parameters:
292    * o nUserId - The UserId that authorization
293    * will be sent;
294   */

295     
296     return clientEngine.authorizeUser( nUserId );
297   }
298
299   boolean sendMessage( long nUserId, java.lang.String JavaDoc strMsg ) {
300   /**
301      Send a message to a Buddy from
302      ContactList.
303      Parameters:
304       * nUserId - The UserId that
305         the message will be sent;
306       * strMsg - Message to be sent;
307   */

308    
309     return clientEngine.sendMessage( nUserId, strMsg );
310   }
311
312   public static void main( String JavaDoc[] args ) {
313   /**
314      Performs a way to do a self test
315      in ComVcSession class.
316      Parameters:
317       * args - Parameters to do a Test;
318         first parameter is Type of test,
319         second is UserName, third Password.
320   */

321     
322     JComVcSession comVcSession = new JComVcSession();
323     long aUserIds[] = null;
324     
325         
326     if( args.length < 4 ) {
327       System.out.println( "Number of parameters invalid" );
328       System.out.println( "Usage: JComVcSession TypeOfTest UserId UserName Password ...list of buddies...." );
329       return;
330     }
331         
332     if( args.length > 4 ) {
333       aUserIds = new long[args.length - 4];
334       
335       for( int nCount = 0; nCount < ( args.length - 4 ); nCount++ )
336         aUserIds[nCount] = Long.parseLong( args[nCount+4] );
337     }
338
339     comVcSession.selfTest( Integer.parseInt( args[0] ), Integer.parseInt( args[1] ), args[2], args[3], aUserIds );
340   }
341 }
342
343 // JComVcSession class
344
Popular Tags