1 /*
2  * Created on 20-Dec-2005
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 package com.aelitis.azureus.core.instancemanager.impl;
21 
22 import java.net.Inet4Address;
23 import java.net.InetAddress;
24 import java.util.*;
25 
26 import org.gudy.azureus2.core3.util.Debug;
27 import org.gudy.azureus2.core3.util.SystemProperties;
28 import org.gudy.azureus2.core3.util.SystemTime;
29 
30 
31 public class
32 AZOtherInstanceImpl
33 	extends AZInstanceImpl
34 {
35 	protected static AZOtherInstanceImpl
decode( InetAddress internal_address, Map map )36 	decode(
37 		InetAddress		internal_address,
38 		Map				map )
39 	{
40 		String	id			= new String((byte[])map.get( "id" ));
41 		String	int_ip		= new String((byte[])map.get( "iip" ));
42 		String	ext_ip		= new String((byte[])map.get( "eip" ));
43 		int		tcp			= ((Long)map.get("tp" )).intValue();
44 		int		udp			= ((Long)map.get("dp" )).intValue();
45 
46 		Long	l_udp_other = (Long)map.get("dp2" );
47 
48 		int		udp_other	= l_udp_other==null?udp:l_udp_other.intValue();
49 
50 		byte[]	app_id_bytes = (byte[])map.get( "ai" );
51 
52 		String app_id;
53 
54 		if ( app_id_bytes == null ){
55 
56 			app_id = SystemProperties.AZ_APP_ID + "_4.2.0.2";	// we dont know, but this is most likely
57 
58 		}else{
59 
60 			app_id = new String( app_id_bytes );
61 		}
62 
63 		Map<String,Object>	props = (Map<String,Object>)map.get( "pr" );
64 
65 		try{
66 			if ( !int_ip.equals("0.0.0.0")){
67 
68 				internal_address = InetAddress.getByName( int_ip );
69 			}
70 
71 			InetAddress	external_address = InetAddress.getByName( ext_ip );
72 
73 				// ignore incompatible address mappings
74 
75 			if ( internal_address instanceof Inet4Address == external_address instanceof Inet4Address ){
76 
77 				return( new AZOtherInstanceImpl(id, app_id, internal_address, external_address, tcp, udp, udp_other, props ));
78 			}
79 
80 			return( null );
81 
82 		}catch( Throwable e ){
83 
84 			Debug.printStackTrace(e);
85 		}
86 
87 		return( null );
88 	}
89 
90 	private String					id;
91 	private String					app_id;
92 	private List					internal_addresses	= new ArrayList();
93 	private InetAddress				external_address;
94 	private int						tcp_port;
95 	private int						udp_port;
96 	private int						udp_non_data_port;
97 	private Map<String,Object>		props;
98 
99 	private long	alive_time;
100 
101 
102 	protected
AZOtherInstanceImpl( String _id, String _app_id, InetAddress _internal_address, InetAddress _external_address, int _tcp_port, int _udp_port, int _udp_non_data_port, Map<String,Object> _props )103 	AZOtherInstanceImpl(
104 		String					_id,
105 		String					_app_id,
106 		InetAddress				_internal_address,
107 		InetAddress				_external_address,
108 		int						_tcp_port,
109 		int						_udp_port,
110 		int						_udp_non_data_port,
111 		Map<String,Object>		_props )
112 	{
113 		id					= _id;
114 		app_id				= _app_id;
115 
116 		internal_addresses.add( _internal_address );
117 
118 		external_address	= _external_address;
119 		tcp_port			= _tcp_port;
120 		udp_port			= _udp_port;
121 		udp_non_data_port	= _udp_non_data_port;
122 
123 		props				= _props;
124 
125 		alive_time	= SystemTime.getCurrentTime();
126 	}
127 
128 	protected boolean
update( AZOtherInstanceImpl new_inst )129 	update(
130 		AZOtherInstanceImpl	new_inst )
131 	{
132 		alive_time	= SystemTime.getCurrentTime();
133 
134 		InetAddress	new_address = new_inst.getInternalAddress();
135 
136 		boolean	same = true;
137 
138 		if ( !internal_addresses.contains( new_address )){
139 
140 			same	= false;
141 
142 			List	new_addresses = new ArrayList( internal_addresses );
143 
144 			new_addresses.add( 0, new_address );
145 
146 			internal_addresses	= new_addresses;
147 		}
148 
149 		same	 = 	same &&
150 					external_address.equals( new_inst.external_address ) &&
151 					tcp_port == new_inst.tcp_port  &&
152 					udp_port == new_inst.udp_port;
153 
154 
155 		external_address	= new_inst.external_address;
156 		tcp_port			= new_inst.tcp_port;
157 		udp_port			= new_inst.udp_port;
158 
159 		return( !same );
160 	}
161 
162 	public String
getID()163 	getID()
164 	{
165 		return( id );
166 	}
167 
168 	public String
getApplicationID()169 	getApplicationID()
170 	{
171 		return( app_id );
172 	}
173 
174 	public InetAddress
getInternalAddress()175 	getInternalAddress()
176 	{
177 		return((InetAddress)internal_addresses.get(0));
178 	}
179 
180 	public List
getInternalAddresses()181 	getInternalAddresses()
182 	{
183 		return( new ArrayList( internal_addresses ));
184 	}
185 
186 	public InetAddress
getExternalAddress()187 	getExternalAddress()
188 	{
189 		return( external_address );
190 	}
191 
192 	public int
getTCPListenPort()193 	getTCPListenPort()
194 	{
195 		return( tcp_port );
196 	}
197 
198 	public int
getUDPListenPort()199 	getUDPListenPort()
200 	{
201 		return( udp_port );
202 	}
203 
204 	public int
getUDPNonDataListenPort()205 	getUDPNonDataListenPort()
206 	{
207 		return( udp_non_data_port );
208 	}
209 
210 	public Map<String, Object>
getProperties()211 	getProperties()
212 	{
213 		return( props );
214 	}
215 
216 	protected long
getAliveTime()217 	getAliveTime()
218 	{
219 		long	now = SystemTime.getCurrentTime();
220 
221 		if ( now < alive_time ){
222 
223 			alive_time	= now;
224 		}
225 
226 		return( alive_time );
227 	}
228 }
229