View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
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   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net;
21  
22  import java.io.IOException;
23  import java.util.HashSet;
24  import java.util.StringTokenizer;
25  
26  import org.xnap.net.HttpConnection;
27  import org.xnap.plugin.opennap.OpenNapPlugin;
28  import org.xnap.util.PortRange;
29  
30  /***
31   * Connects to the OpenNapigator index server and creates 
32   * {@link OpenNapServer} objects.
33   */
34  public class NapigatorReader implements OpenNapServerReader {
35  
36      //--- Data Field(s) ---
37  
38      private HttpConnection conn;
39      private String url;
40  
41      private HashSet filter = new HashSet();
42      private HashSet fakeNetworks = new HashSet();
43  
44      //--- Constructor(s) ---
45  	
46      public NapigatorReader(String url)
47      {
48  		this.url = url;
49  
50  		String s = OpenNapPlugin.getPreferences().getFilterNetworks();
51  		StringTokenizer t = new StringTokenizer(s, ";");
52  		while (t.hasMoreTokens()) {
53  			filter.add(t.nextToken().trim());
54  		}
55  
56  		s = OpenNapPlugin.getPreferences().getFakeNetworks();
57  		t = new StringTokenizer(s, ";");
58  		while (t.hasMoreTokens()) {
59  			fakeNetworks.add(t.nextToken().trim());
60  		}
61      }
62  
63      //--- Method(s) ---
64  
65      public void close()
66      {
67  		if (conn != null) {
68  			conn.close();
69  		}
70      }
71  
72      public void open() throws IOException
73      {
74  		conn = new HttpConnection();
75  		conn.connect(url);	
76      }
77  
78      /*
79       * the lines look like this:
80       *
81       * <ip> <port> <network name> <# users> <# files> <# gigabytes> 
82       * <dns name>\n
83       *
84       * and the last line like this:
85       *
86       * <# of servers> <total # of users> <total # of files> 
87       * <total # of gigabytes>\n
88       *
89       */
90      public OpenNapServer read() throws IOException 
91      {
92  		String line;
93  		while ((line = conn.nextLine()) != null) {
94  			StringTokenizer t = new StringTokenizer(line, " ");
95  	    
96  			int count = t.countTokens();
97  			if (count < 6) {
98  				continue;
99  			}
100 	    
101 			try {
102 				if (count == 14) {
103 					// some strange number
104 					int i = Integer.parseInt(t.nextToken()); 
105 				}
106 				String ip = t.nextToken();
107 				int port = Integer.parseInt(t.nextToken());
108 				if (port < PortRange.MIN_PORT || port > PortRange.MAX_PORT) {
109 					throw new NumberFormatException();
110 				}
111 				String network = t.nextToken();
112 				if (count == 14) {
113 					int connectionSpeed = Integer.parseInt(t.nextToken()); 
114 				}
115 				int users = Integer.parseInt(t.nextToken());
116 				int files = Integer.parseInt(t.nextToken());
117 				int total = Integer.parseInt(t.nextToken());
118 				String host = t.hasMoreTokens() ? t.nextToken() : "";
119 				
120 				if (filter.contains(network)) {
121 					continue;
122 				}
123 				else if (fakeNetworks.contains(network)) {
124 					network = "";
125 				}
126 
127 				if (host.length() == 0) {
128 					host = ip;
129 					ip = null;
130 				}
131 		
132 				return new OpenNapServer(host, ip, port, network, 
133 									 files, total, users, false);
134 			}
135 			catch (NumberFormatException e) {
136 			}
137 		} 
138 
139         return null;
140     }
141 
142 }