KickJava   Java API By Example, From Geeks To Geeks.

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


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: JComVcSocketEvents.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.net.*;
42 import java.util.*;
43 import java.text.*;
44 import org.planetamessenger.net.*;
45 import org.planetamessenger.io.*;
46 import org.planetamessenger.util.*;
47
48
49
50 class JComVcSocketEvents implements JSocketEventListener {
51
52   JComVcEventListener eventListener;
53   
54   
55   
56   JComVcSocketEvents() {
57   /**
58      Constructor. Initializes all class
59      data.
60   */

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

74     
75     eventListener = listener;
76   }
77   
78   public void onAccept( java.net.Socket JavaDoc sok ) {
79   /**
80      Implements the onAccept socket
81      response.
82      Parameters:
83       * sok - The connected socket;
84   */

85   
86     System.err.println( "JComVcSocketEvents.onAccept(" + sok.getInetAddress().getHostAddress() + ") - Triggered" );
87   
88     HashMap<String JavaDoc, String JavaDoc> hMsgData = new HashMap<String JavaDoc, String JavaDoc>();
89     JIOStream ioStream = new JIOStream();
90     String JavaDoc strFmt = JComVcConstants.formatSuccessString( JComVcConstants.COMVC_MESSAGE_FLAG );
91         
92     
93     try {
94       
95       /*
96       String strData = ioStream.recv( sok.getInputStream() );
97       System.err.println( strData );
98       */

99
100       // Send the success message to server
101
if( !ioStream.send( sok.getOutputStream(), strFmt ) ) {
102         System.err.println( "JComVcSocketEvents.onAccept() - Error to send message OK to server" );
103         // Not will return (only for information)
104
}
105       else {
106         System.err.println( "JComVcSocketEvents.onAccept() - Success Message notification sent successfull - " + strFmt );
107       }
108
109       if( !ioStream.recv( sok.getInputStream(), hMsgData, false ) ) {
110         System.err.println( "JComVcSocketEvents.onAccept() - Error to receive data" );
111         sok.close();
112         return;
113       }
114       
115       System.err.println( "JComVcSocketEvents.onAccept() - Data received : " + hMsgData );
116       
117       if( !hMsgData.containsKey( JComVcConstants.COMVC_MSG_KEY ) ) {
118         System.err.println( "JComVcSocketEvents.onAccept() - Error in received message" );
119         sok.close();
120         return;
121       }
122
123       // Dispatch onMessage event
124
dispatchMessage( hMsgData, eventListener );
125       
126       //sok.close();
127
} catch( java.io.IOException JavaDoc e ) {
128       System.err.println( e );
129     }
130   }
131   
132   public void onReceive( java.net.Socket JavaDoc sok ) {
133   /**
134      Implements the onReceive socket
135      response.
136      Parameters:
137       * sok - The connected socket;
138   */

139    
140   }
141   
142   public void onSend( java.net.Socket JavaDoc sok ) {
143   /**
144      Implements the onSend socket
145      response.
146      Parameters:
147       * sok - The connected socket;
148   */

149     
150   }
151   
152   public void onClose( java.net.Socket JavaDoc sok ) {
153   /**
154      Implements the onClose socket
155      response.
156      Parameters:
157       * sok - The connected socket;
158   */

159     
160   }
161   
162   // Static methods
163
public static void dispatchMessage( HashMap hMsgData, JComVcEventListener listener ) {
164   /**
165    * Format and dispatches a message
166    * received to destination user.
167    * Parameters:
168    * o hMsgData - The Message received from
169    * server;
170    * o listener - The listener that will
171    * dispatch the message data;
172    */

173     
174     SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );
175     ParsePosition pos = new ParsePosition( 0 );
176     GregorianCalendar msgDate = new GregorianCalendar();
177     long nFromUser = Long.parseLong( ( !hMsgData.containsKey( JComVcConstants.COMVC_MSGOFF_KEY ) ? JKeyParser.removeCRLF( hMsgData.get( JComVcConstants.COMVC_USERID_KEY ).toString() ) : hMsgData.get( JComVcConstants.COMVC_USERID_KEY ).toString() ) );
178     char aEOL[] = { 0x0e, 0x14 };
179     String JavaDoc strTmp = hMsgData.get( JComVcConstants.COMVC_MESSAGE_FLAG ).toString();
180     String JavaDoc strMsg = org.planetamessenger.util.JKeyParser.replace( strTmp, new String JavaDoc( aEOL ), "\r\n" );
181     String JavaDoc strDateTime = ( !hMsgData.containsKey( JComVcConstants.COMVC_MSGOFF_KEY ) ? JKeyParser.removeCRLF( hMsgData.get( JComVcConstants.COMVC_DATE_KEY ).toString() ) : hMsgData.get( JComVcConstants.COMVC_DATE_KEY ).toString() ) +
182                                       ( !hMsgData.containsKey( JComVcConstants.COMVC_MSGOFF_KEY ) ? JKeyParser.removeCRLF( hMsgData.get( JComVcConstants.COMVC_TIME_KEY ).toString() ) : hMsgData.get( JComVcConstants.COMVC_TIME_KEY ).toString() );
183       
184     format.setLenient( false );
185     msgDate.setTime( format.parse( strDateTime, pos ) );
186         
187     if( listener != null ) {
188       listener.onReceiveMessage( nFromUser, msgDate, strMsg );
189     }
190   }
191 }
192
193 // JComVcSocketEvents class
194
Popular Tags