KickJava   Java API By Example, From Geeks To Geeks.

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


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: JComVcContactItem.java,v 1.7 2007/02/12 00:43:55 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.7 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.comvc;
40
41
42
43 class JComVcContactItem {
44   
45   protected long nUserId;
46   protected String JavaDoc strIPAddress;
47   protected int nTCPPort;
48   protected int nUDPPort;
49   protected long nClientVersion;
50   protected long nTicket;
51   protected int nUserStatus;
52   
53   
54   JComVcContactItem() {
55   /**
56      Constructor. Initialize all class
57      data.
58   */

59   
60     nUserId = 0;
61     strIPAddress = "";
62     nTCPPort = 0;
63     nUDPPort = 0;
64     nClientVersion = 0;
65     nTicket = 0;
66     nUserStatus = JComVcConstants.STATUS_OFFLINE;
67   }
68   
69   public boolean equals( Object JavaDoc obj ) {
70   /**
71      Replaces the default equals to
72      perform a specific compare
73      between two JComVcContactItem
74      objects.
75      Parameter:
76       * obj - The object that will
77         compare to this object;
78   */

79    
80     JComVcContactItem item = ( JComVcContactItem ) obj;
81   
82     return item.nUserId == nUserId;
83   }
84
85   static JComVcContactItem parseItem( java.lang.String JavaDoc strContactList ) {
86   /**
87     * Parses a contactlist string into a item.
88     * Returns a JComVcContactItem object
89     * from ContactList String.
90     * This contactitem is erased from
91     * strContactList passed as parameter.
92     * Parameter:
93     * o strContactList - The full (or partial)
94     * contactlist sent by server;
95   */

96     
97     String JavaDoc strTmp;
98     int nBeginIndex = strContactList.indexOf( "\r\n" );
99     
100     
101     if( nBeginIndex < 0 )
102       return null;
103     
104     // Parse the ContacItem
105
strTmp = strContactList.substring( 0, nBeginIndex );
106     System.err.println( strTmp );
107     
108     // Gets the UserId
109
JComVcContactItem contactItem = new JComVcContactItem();
110     int nEndIndex = strTmp.indexOf( '|' );
111     int nUserStatus;
112           
113     // Parse UserId
114
if( nEndIndex < 0 )
115       return null;
116     contactItem.nUserId = Long.parseLong( strTmp.substring( 0, nEndIndex ) );
117     
118     // Parse the Ticket
119
nBeginIndex = nEndIndex + 1;
120     if( ( nEndIndex = strTmp.indexOf( '|', nBeginIndex ) ) < 0 )
121       return null;
122     contactItem.nTicket = Long.parseLong( strTmp.substring( nBeginIndex, nEndIndex ) );
123     
124     // Parse IP address
125
nBeginIndex = nEndIndex + 1;
126     if( ( nEndIndex = strTmp.indexOf( '|', nBeginIndex ) ) < 0 )
127       return null;
128     contactItem.strIPAddress = strTmp.substring( nBeginIndex, nEndIndex );
129     
130     // Parse TCP Port
131
nBeginIndex = nEndIndex + 1;
132     if( ( nEndIndex = strTmp.indexOf( '|', nBeginIndex ) ) < 0 )
133       return null;
134     contactItem.nTCPPort = Integer.parseInt( strTmp.substring( nBeginIndex, nEndIndex ) );
135     
136     // Parse UDP port
137
nBeginIndex = nEndIndex + 1;
138     if( ( nEndIndex = strTmp.indexOf( '|', nBeginIndex ) ) < 0 )
139       return null;
140     contactItem.nUDPPort = Integer.parseInt( strTmp.substring( nBeginIndex, nEndIndex ) );
141     
142     // Parse User status
143
nBeginIndex = nEndIndex + 1;
144     if( ( nEndIndex = strTmp.indexOf( '|', nBeginIndex ) ) < 0 )
145       return null;
146     
147     nUserStatus = Integer.parseInt( strTmp.substring( nBeginIndex, nEndIndex ) );
148     contactItem.nUserStatus = ( ( nUserStatus == JComVcConstants.STATUS_BUDDY_INVISIBLE ) ? JComVcConstants.STATUS_INVISIBLE : nUserStatus );
149     
150     // Parse Client Version
151
nBeginIndex = nEndIndex + 1;
152     contactItem.nClientVersion = Long.parseLong( strTmp.substring( nBeginIndex ) );
153        
154     System.err.println( "JComVcContactItem.parseContactList() - UserId :" + contactItem.nUserId );
155     System.err.println( "JComVcContactItem.parseContactList() - IP Address :" + contactItem.strIPAddress );
156     System.err.println( "JComVcContactItem.parseContactList() - TCPPort :" + contactItem.nTCPPort );
157     System.err.println( "JComVcContactItem.parseContactList() - UDPPort :" + contactItem.nUDPPort );
158     System.err.println( "JComVcContactItem.parseContactList() - User Status :" + contactItem.nUserStatus );
159     System.err.println( "JComVcContactItem.parseContactList() - Ticket :" + contactItem.nTicket );
160     System.err.println( "JComVcContactItem.parseContactList() - Client Version :" + contactItem.nClientVersion );
161     
162     return contactItem;
163   }
164
165   static java.util.HashMap JavaDoc<Long JavaDoc, JComVcContactItem> parseList( java.lang.String JavaDoc strContactList ) {
166   /**
167     * Parses a contactlist string into a HashMap.
168     * Returns a HashMap object containing all
169     * ContactList.
170     * This ContactItem is erased from
171     * strContactList passed as parameter.
172     * Parameter:
173     * o strContactList - The full (or partial)
174     * contactlist sent by server;
175   */

176
177     java.util.HashMap JavaDoc<Long JavaDoc, JComVcContactItem> hContactList = new java.util.HashMap JavaDoc<Long JavaDoc, JComVcContactItem>();
178     JComVcContactItem contactItem;
179     
180     
181     
182     while( ( contactItem = JComVcContactItem.parseItem( strContactList ) ) != null ) {
183
184       Long JavaDoc userId = new Long JavaDoc( contactItem.nUserId );
185
186       // Adds server sent ContactList to our ContactList
187
hContactList.put( userId, contactItem );
188
189       // Cut this ContactItem from ContactList string
190
strContactList = strContactList.substring( strContactList.indexOf( "\r\n" ) + 2 ); // + 2 from \r\n
191
System.err.println( "Contact List " + strContactList );
192     }
193     
194     return hContactList;
195   }
196 }
197
198 // JComVcContactItem class
199
Popular Tags