KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2     =========================================================================
3     Package comvc - Implements Network classes.
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: JComVcUDPEvents.java,v 1.6 2007/02/11 13:52:36 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.6 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.protocols.comvc;
40
41 import java.util.*;
42 import java.io.*;
43 import org.planetamessenger.net.*;
44 import org.planetamessenger.io.*;
45
46
47
48 class JComVcUDPEvents implements JDatagramEventListener {
49   
50   JComVcEventListener eventListener;
51   HashMap hContactList;
52   
53   
54   
55   JComVcUDPEvents( HashMap hContactList ) {
56   /**
57      Constructor. Initializes all class
58      data.
59      Parameters:
60       * hContactList - The contactlist
61         with all users info;
62   */

63     
64     eventListener = null;
65     this.hContactList = hContactList;
66   }
67   
68   void addComVcEventListener( JComVcEventListener listener ) {
69   /**
70     Attaches a SocketListener to this
71     class.
72     Parameters:
73      * listener - The PluginListener
74        that will be attached to this
75        class;
76   */

77     
78     eventListener = listener;
79   }
80   
81   public void onClose() {
82   /**
83      Implements the onClose UDP
84      response.
85   */

86     
87   }
88   
89   public void onReceive( java.net.DatagramPacket JavaDoc pak ) {
90   /**
91      Implements the onReceive UDP
92      response.
93      Parameters:
94       * pak - The Packet received;
95   */

96     
97     System.err.println( "JComVcUDPEvents.onReceive() [" + pak.getAddress().getHostAddress() + "] (" + pak.getLength() + " bytes)" );
98     
99     ByteArrayInputStream inStream = new ByteArrayInputStream( pak.getData() );
100     JIOStream ioStream = new JIOStream();
101     HashMap<String JavaDoc, String JavaDoc> hServerParms = new HashMap<String JavaDoc, String JavaDoc>();
102     JComVcContactItem contactItem;
103     
104
105     // Receive all server data (INP/OUT)
106
if( !ioStream.recv( inStream, hServerParms ) ) {
107       System.err.println( "JComVcUDPEvents.onReceive() - Server data error " + hServerParms.toString() );
108       return;
109     }
110     
111     System.err.println( "JComVcUDPEvents.onReceive(" + pak.getAddress().getHostAddress() + ") - Server sent " + hServerParms.toString() );
112
113     // Checks if the message is a INP ou OUT message
114
if( !hServerParms.containsKey( JComVcConstants.COMVC_INPUT_FLAG ) && !hServerParms.containsKey( JComVcConstants.COMVC_OUTPUT_FLAG ) ) {
115       System.err.println( "JComVcUDPEvents.onReceive() - INP/OUT parameter error" );
116       return;
117     }
118     
119     // Update the ContactList item
120
Long JavaDoc lUserId = new Long JavaDoc( Long.parseLong( hServerParms.get( JComVcConstants.COMVC_USERID_KEY ).toString() ) );
121     
122     if( hContactList.containsKey( lUserId ) ) {
123       contactItem = ( JComVcContactItem ) hContactList.get( lUserId );
124     
125       /**
126          The code bellow don't works, because UOL clients send the incorrect
127          address (if the client have 2 or more network adapters).
128          In our case the original ComVC is sending the IP address of internal
129          network adapter.
130          The workaround is bellow.
131       */

132       //contactItem.strIPAddress = hServerParms.get( JComVcConstants.COMVC_IP_KEY ).toString();
133
contactItem.strIPAddress = pak.getAddress().getHostAddress();
134       contactItem.nTicket = Long.parseLong( hServerParms.get( JComVcConstants.COMVC_TICKET_KEY ).toString() );
135       // Only for INP parameters
136
if( hServerParms.containsKey( JComVcConstants.COMVC_INPUT_FLAG ) ) {
137         
138         int nUserStatus = Integer.parseInt( hServerParms.get( JComVcConstants.COMVC_STATUS_KEY ).toString() );
139         
140         contactItem.nTCPPort = Integer.parseInt( hServerParms.get( JComVcConstants.COMVC_PORT_KEY ).toString() );
141         contactItem.nUDPPort = Integer.parseInt( hServerParms.get( JComVcConstants.COMVC_PORTUDP_KEY ).toString() );
142         contactItem.nClientVersion = Long.parseLong( hServerParms.get( JComVcConstants.COMVC_VERSION_KEY ).toString() );
143         contactItem.nUserStatus = ( ( nUserStatus == JComVcConstants.STATUS_BUDDY_INVISIBLE ) ? JComVcConstants.STATUS_INVISIBLE : nUserStatus );
144       }
145       else
146         contactItem.nUserStatus = JComVcConstants.STATUS_OFFLINE;
147
148       // Dispatches the onStatusChanged event
149
if( eventListener != null )
150         eventListener.onBuddyStatusChanged( contactItem.nUserId, contactItem.nUserStatus );
151     }
152     else {
153       System.err.println( "JComVcUDPEvents.onReceive() - UserId [" + lUserId.longValue() + "] not found in ContactList" );
154     }
155   }
156
157   public void onSend( java.net.DatagramPacket JavaDoc pak ) {
158   /**
159      Implements the onSend UDP
160      response.
161      Parameters:
162       * pak - The Packet sent;
163   */

164     
165   }
166 }
167
168 // JComVcUDPEvents class
Popular Tags