1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 private HttpConnection conn;
39 private String url;
40
41 private HashSet filter = new HashSet();
42 private HashSet fakeNetworks = new HashSet();
43
44
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
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
80
81
82
83
84
85
86
87
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
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 }