1 /* 2 * File : PRUDPPacketReply.java 3 * Created : 20-Jan-2004 4 * By : parg 5 * 6 * Azureus - a Java Bittorrent client 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details ( see the LICENSE file ). 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 package com.aelitis.net.udp.uc; 24 25 /** 26 * @author parg 27 * 28 */ 29 30 import java.io.*; 31 import java.net.InetSocketAddress; 32 import java.util.*; 33 34 import org.gudy.azureus2.core3.util.AEMonitor; 35 import org.gudy.azureus2.core3.util.Debug; 36 37 public abstract class 38 PRUDPPacketReply 39 extends PRUDPPacket 40 { 41 public static final int PR_HEADER_SIZE = 8; 42 43 private static AEMonitor class_mon = new AEMonitor( "PRUDPPacketReply:class" ); 44 45 private static Map packet_decoders = new HashMap(); 46 47 public static void registerDecoders( Map _decoders )48 registerDecoders( 49 Map _decoders ) 50 { 51 try{ 52 class_mon.enter(); 53 54 Map new_decoders = new HashMap( packet_decoders ); 55 56 Iterator it = _decoders.keySet().iterator(); 57 58 while( it.hasNext()){ 59 60 Integer action = (Integer)it.next(); 61 62 if ( packet_decoders.containsKey( action )){ 63 64 Debug.out( "Duplicate codec! " + action ); 65 } 66 } 67 68 new_decoders.putAll( _decoders ); 69 70 packet_decoders = new_decoders; 71 72 }finally{ 73 74 class_mon.exit(); 75 } 76 } 77 78 public PRUDPPacketReply( int _action, int _tran_id )79 PRUDPPacketReply( 80 int _action, 81 int _tran_id ) 82 { 83 super( _action, _tran_id ); 84 } 85 86 87 public void serialise( DataOutputStream os )88 serialise( 89 DataOutputStream os ) 90 91 throws IOException 92 { 93 // add to this and you need to adjust HEADER_SIZE above 94 95 os.writeInt( getAction()); 96 97 os.writeInt( getTransactionId() ); 98 } 99 100 public static PRUDPPacketReply deserialiseReply( PRUDPPacketHandler handler, InetSocketAddress originator, DataInputStream is )101 deserialiseReply( 102 PRUDPPacketHandler handler, 103 InetSocketAddress originator, 104 DataInputStream is ) 105 106 throws IOException 107 { 108 int action = is.readInt(); 109 110 PRUDPPacketReplyDecoder decoder = (PRUDPPacketReplyDecoder)packet_decoders.get( new Integer( action )); 111 112 if ( decoder == null ){ 113 114 throw( new IOException( "No decoder registered for action '" + action + "'" )); 115 } 116 117 int transaction_id = is.readInt(); 118 119 return( decoder.decode( handler, originator, is, action, transaction_id )); 120 } 121 122 public String getString()123 getString() 124 { 125 return( super.getString() + ":reply[trans=" + getTransactionId() + "]" ); 126 } 127 }