KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > TransportInfo


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Jan 14, 2004
48  * Manta LTD
49  */

50 package org.mr.core.net;
51
52 import java.io.IOException JavaDoc;
53 import java.net.InetAddress JavaDoc;
54 import java.net.InetSocketAddress JavaDoc;
55 import java.net.NetworkInterface JavaDoc;
56 import java.net.SocketAddress JavaDoc;
57 import java.util.Enumeration JavaDoc;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.mr.core.util.byteable.Byteable;
62 import org.mr.core.util.byteable.ByteableInputStream;
63 import org.mr.core.util.byteable.ByteableOutputStream;
64 import org.mr.core.util.byteable.ByteableRegistry;
65
66 /**
67  * TransportInfo holds the data of how to connect to a remote computer.
68  *
69  * Created Jan 14, 2004
70  * Ver 1.0
71  * @author Amir Shevat
72  */

73 public class TransportInfo implements Byteable {
74     
75     // the addr is the port and ip of the remote computer
76
InetSocketAddress JavaDoc addr;
77     TransportType transportType;
78     static Log log;
79     
80     
81     public TransportInfo() {
82     }
83
84     public TransportInfo(InetSocketAddress JavaDoc addr, TransportType type) {
85         this.addr = addr;
86         this.transportType = type;
87     }
88
89     public TransportInfo(InetAddress JavaDoc ip, int port, String JavaDoc transportInfoType) {
90         log=LogFactory.getLog("TransportInfo");
91         try {
92             this.addr = new InetSocketAddress JavaDoc(ip, port);
93         } catch (IllegalArgumentException JavaDoc e) {
94             if(log.isErrorEnabled()){
95                 log.error("illegal IP or port for transport, ip = " + ip + "port = " + port+".", e);
96             }
97             this.addr = null;
98         }
99         this.transportType = TransportType.getTransportTypeFromString(transportInfoType);
100     }
101
102     public TransportInfo(String JavaDoc ipStr, int port, String JavaDoc transportInfoType) {
103         log=LogFactory.getLog("TransportInfo");
104         try {
105             this.addr = new InetSocketAddress JavaDoc(ipStr, port);
106         } catch (IllegalArgumentException JavaDoc e) {
107             if(log.isErrorEnabled()){
108                 log.error("illegal IP or port for transport, ip = " + ipStr + "port = " + port+".", e);
109             }
110             this.addr = null;
111         }
112
113         if ((this.addr).isUnresolved()) {
114             if(log.isErrorEnabled()){
115                 log.error("Transport info created with invalid ip - ip="+ipStr);
116             }
117         }
118         // setIp(ipStr);
119
// this.port = port;
120
this.transportType = TransportType.getTransportTypeFromString(transportInfoType);
121     }
122     
123     
124     
125
126     /**
127      * @return The ip of the MantaRay layer.
128      */

129     public InetAddress JavaDoc getIp() {
130         return this.addr.getAddress();
131     }
132
133     /**
134      * @return The port of the MantatRay layer.
135      */

136     public int getPort() {
137         return this.addr.getPort();
138     }
139
140     /**
141      * Describe <code>getSocketAddress</code> method here.
142      *
143      * @return an <code>InetSocketAddress</code> value
144      */

145     public SocketAddress JavaDoc getSocketAddress() {
146         return this.addr;
147     }
148     
149     /**
150      * @return Returns the transportInfoType.
151      */

152     public TransportType getTransportInfoType() {
153         return transportType;
154     }
155
156     /**
157      * @param transportInfoType The transportInfoType to set.
158      */

159     public void setTransportInfoType(String JavaDoc transportInfoType) {
160         this.transportType = TransportType.getTransportTypeFromString(transportInfoType);
161     }
162
163     public String JavaDoc toString(){
164         return String.valueOf(addr)+"@"+String.valueOf(transportType);
165     }
166     
167     public final boolean equals(Object JavaDoc obj) {
168         if(this == obj)
169             return true;
170         if (obj == null || !(obj instanceof TransportInfo))
171             return false;
172         TransportInfo other = ((TransportInfo)obj);
173         return(other.addr.equals(this.addr)) && other.transportType.equals(this.transportType) ;
174     }
175
176     /*
177      * the next method was added by lital kasif
178      */

179     public int hashCode(){
180         return this.addr.hashCode()+this.transportType.hashCode();
181     }
182     
183     public String JavaDoc getByteableName() {
184         return "TransportInfo";
185     }
186
187     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
188         out.write(this.addr.getAddress().getAddress());
189         out.writeInt(this.addr.getPort());
190         out.writeASCIIString(this.transportType.toString());
191     }
192
193     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
194         byte[] addr = new byte[4];
195         int port;
196         String JavaDoc type;
197         TransportInfo result = new TransportInfo();
198
199         in.read(addr);
200         port = in.readInt();
201         type = in.readASCIIString();
202
203         result.transportType = TransportType.getTransportTypeFromString(type);
204         result.addr = new InetSocketAddress JavaDoc(InetAddress.getByAddress(addr),
205                                             port);
206
207         return result;
208     }
209
210     public void registerToByteableRegistry() {
211         ByteableRegistry.registerByteableFactory(getByteableName(), this);
212     }
213
214     public static void register() {
215         TransportInfo info = new TransportInfo();
216         
217         info.registerToByteableRegistry();
218     }
219
220     public static String JavaDoc getValidLocalAddress() {
221         try {
222             Enumeration JavaDoc ifs = NetworkInterface.getNetworkInterfaces();
223             while (ifs.hasMoreElements()) {
224                 NetworkInterface JavaDoc iface = (NetworkInterface JavaDoc)
225                     ifs.nextElement();
226                 Enumeration JavaDoc ips = iface.getInetAddresses();
227                 while (ips.hasMoreElements()) {
228                     InetAddress JavaDoc ip = (InetAddress JavaDoc) ips.nextElement();
229                     if (!ip.getHostAddress().equals("127.0.0.1")) {
230                         return ip.getHostAddress();
231                     }
232                 }
233             }
234         } catch (Throwable JavaDoc t) {}
235         
236         return "127.0.0.1"; // won't happen, trust me.
237
}
238
239 }
240
Popular Tags