1 /*
2  * File    : PRUDPPacket.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 
33 import org.gudy.azureus2.core3.util.AEMonitor;
34 import org.gudy.azureus2.core3.util.RandomUtils;
35 
36 public abstract class
37 PRUDPPacket
38 {
39 	public static final int	MAX_PACKET_SIZE			= 8192;
40 	public static final int DEFAULT_UDP_TIMEOUT		= 30000;
41 
42 	private static int				next_id 	= RandomUtils.nextInt();
43 	private static AEMonitor		class_mon	= new AEMonitor( "PRUDPPacket" );
44 
45 	private	InetSocketAddress	address;
46 
47 	private int		type;
48 	private int		transaction_id;
49 
50 	private PRUDPPacket	previous_packet;
51 
52 	private int			serialised_size;
53 
54 	protected
PRUDPPacket( int _type, int _transaction_id )55 	PRUDPPacket(
56 		int		_type,
57 		int		_transaction_id )
58 	{
59 		type			= _type;
60 		transaction_id	= _transaction_id;
61 	}
62 
63 	protected
PRUDPPacket( int _type )64 	PRUDPPacket(
65 		int		_type )
66 	{
67 		type			= _type;
68 
69 		try{
70 			class_mon.enter();
71 
72 			transaction_id	= next_id++;
73 
74 		}finally{
75 
76 			class_mon.exit();
77 		}
78 	}
79 
80 	public void
setSerialisedSize( int len )81 	setSerialisedSize(
82 		int		len )
83 	{
84 		serialised_size	= len;
85 	}
86 
87 	public int
getSerialisedSize()88 	getSerialisedSize()
89 	{
90 		return( serialised_size );
91 	}
92 
93 	public boolean
hasContinuation()94 	hasContinuation()
95 	{
96 		return( false );
97 	}
98 
99 	public void
setPreviousPacket( PRUDPPacket p )100 	setPreviousPacket(
101 		PRUDPPacket	p )
102 	{
103 		previous_packet = p;
104 	}
105 
106 	public PRUDPPacket
getPreviousPacket()107 	getPreviousPacket()
108 	{
109 		return( previous_packet );
110 	}
111 
112 	public void
setAddress( InetSocketAddress _address )113 	setAddress(
114 		InetSocketAddress	_address )
115 	{
116 		address	= _address;
117 	}
118 
119 	public InetSocketAddress
getAddress()120 	getAddress()
121 	{
122 		return( address );
123 	}
124 
125 	public int
getAction()126 	getAction()
127 	{
128 		return( type );
129 	}
130 
131 	public int
getTransactionId()132 	getTransactionId()
133 	{
134 		return( transaction_id );
135 	}
136 
137 	public abstract void
serialise( DataOutputStream os )138 	serialise(
139 		DataOutputStream	os )
140 
141 		throws IOException;
142 
143 	public String
getString()144 	getString()
145 	{
146 		return( "type=" + type + ",addr=" + address );
147 	}
148 }
149