1 29 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 61 62 eventListener = null; 63 } 64 65 void addComVcEventListener( JComVcEventListener listener ) { 66 74 75 eventListener = listener; 76 } 77 78 public void onAccept( java.net.Socket sok ) { 79 85 86 System.err.println( "JComVcSocketEvents.onAccept(" + sok.getInetAddress().getHostAddress() + ") - Triggered" ); 87 88 HashMap<String , String > hMsgData = new HashMap<String , String >(); 89 JIOStream ioStream = new JIOStream(); 90 String strFmt = JComVcConstants.formatSuccessString( JComVcConstants.COMVC_MESSAGE_FLAG ); 91 92 93 try { 94 95 99 100 if( !ioStream.send( sok.getOutputStream(), strFmt ) ) { 102 System.err.println( "JComVcSocketEvents.onAccept() - Error to send message OK to server" ); 103 } 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 dispatchMessage( hMsgData, eventListener ); 125 126 } catch( java.io.IOException e ) { 128 System.err.println( e ); 129 } 130 } 131 132 public void onReceive( java.net.Socket sok ) { 133 139 140 } 141 142 public void onSend( java.net.Socket sok ) { 143 149 150 } 151 152 public void onClose( java.net.Socket sok ) { 153 159 160 } 161 162 public static void dispatchMessage( HashMap hMsgData, JComVcEventListener listener ) { 164 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 strTmp = hMsgData.get( JComVcConstants.COMVC_MESSAGE_FLAG ).toString(); 180 String strMsg = org.planetamessenger.util.JKeyParser.replace( strTmp, new String ( aEOL ), "\r\n" ); 181 String 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 | Popular Tags |