/source/com/limegroup/gnutella/Main.java
Java | 308 lines | 207 code | 54 blank | 47 comment | 24 complexity | fd868980ad3b68fe6da98d1328b1f3fb MD5 | raw file
1package com.limegroup.gnutella;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.util.Set;
8import java.util.Vector;
9
10import org.limewire.bittorrent.Torrent;
11import org.limewire.core.api.download.DownloadAction;
12import org.limewire.core.api.download.DownloadException;
13import org.limewire.io.GUID;
14import org.limewire.io.IpPort;
15import org.limewire.net.SocketsManager.ConnectType;
16
17import com.google.inject.Guice;
18import com.google.inject.Inject;
19import com.google.inject.Injector;
20import com.google.inject.Singleton;
21import com.google.inject.Stage;
22import com.limegroup.gnutella.browser.MagnetOptions;
23import com.limegroup.gnutella.connection.ConnectionLifecycleEvent;
24import com.limegroup.gnutella.connection.RoutedConnection;
25import com.limegroup.gnutella.messages.QueryReply;
26import com.limegroup.gnutella.messages.QueryRequest;
27import com.limegroup.gnutella.version.UpdateInformation;
28
29/**
30 * The command-line UI for the Gnutella servent.
31 */
32public class Main {
33
34 @Inject private LifecycleManager lifecycleManager;
35 @Inject private NetworkManager networkManager;
36 @Inject private ConnectionServices connectionServices;
37 @Inject private SearchServices searchServices;
38
39
40 public static void main(String args[]) {
41 Injector injector = Guice.createInjector(Stage.PRODUCTION, new LimeWireCoreModule(MainCallback.class));
42 Main main = injector.getInstance(Main.class);
43 main.start();
44 }
45
46 private void start() {
47 lifecycleManager.start();
48
49 System.out.println("For a command list type help.");
50 BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
51 for ( ; ;) {
52 System.out.print("LimeRouter> ");
53 try {
54 String command=in.readLine();
55 if (command==null)
56 break;
57 else if (command.equals("help")) {
58 System.out.println("catcher "+
59 "Print host catcher.");
60 System.out.println("connect <host> [<port>] "+
61 "Connect to a host[:port].");
62 System.out.println("help "+
63 "Print this message.");
64 System.out.println("listen <port> "+
65 "Set the port you are listening on.");
66 // System.out.println("push "+
67 // "Print push routes.");
68 System.out.println("query <string> "+
69 "Send a query to the network.");
70 System.out.println("quit "+
71 "Quit the application.");
72 // System.out.println("route "+
73 // "Print routing tables.");
74 // System.out.println("stat "+
75 // "Print statistics.");
76 System.out.println("update "+
77 "Send pings to update the statistics.");
78 }
79 else if (command.equals("quit"))
80 break;
81 // //Print routing tables
82 // else if (command.equals("route"))
83 // RouterService.dumpRouteTable();
84 // //Print connections
85 // else if (command.equals("push"))
86 // RouterService.dumpPushRouteTable();
87 //Print push route
88
89 String[] commands=split(command);
90 //Connect to remote host (establish outgoing connection)
91 if (commands.length>=2 && commands[0].equals("connect")) {
92 try {
93 int port=6346;
94 if (commands.length>=3)
95 port=Integer.parseInt(commands[2]);
96 System.out.println("Connecting...");
97 connectionServices.connectToHostAsynchronously(commands[1], port, ConnectType.PLAIN);
98 } catch (NumberFormatException e) {
99 System.out.println("Please specify a valid port.");
100 }
101 } else if (commands.length>=2 && commands[0].equals("query")) {
102 //Get query string from command (possibly multiple words)
103 int i=command.indexOf(' ');
104 assert(i!=-1 && i<command.length());
105 String query=command.substring(i+1);
106 searchServices.query(searchServices.newQueryGUID(), query);
107 } else if (commands.length==2 && commands[0].equals("listen")) {
108 try {
109 int port=Integer.parseInt(commands[1]);
110 networkManager.setListeningPort(port);
111 } catch (NumberFormatException e) {
112 System.out.println("Please specify a valid port.");
113 } catch (IOException e) {
114 System.out.println("Couldn't change port. Try another value.");
115 }
116 }
117 } catch (IOException e) {
118 System.exit(1);
119 }
120 }
121 System.out.println("Good bye.");
122 lifecycleManager.shutdown(); //write gnutella.net
123 }
124
125 /** Returns an array of strings containing the words of s, where
126 * a word is any sequence of characters not containing a space.
127 */
128 public static String[] split(String s) {
129 s=s.trim();
130 int n=s.length();
131 if (n==0)
132 return new String[0];
133 Vector<String> buf=new Vector<String>();
134
135 //s[i] is the start of the word to add to buf
136 //s[j] is just past the end of the word
137 for (int i=0; i<n; ) {
138 assert(s.charAt(i)!=' ');
139 int j=s.indexOf(' ',i+1);
140 if (j==-1)
141 j=n;
142 buf.add(s.substring(i,j));
143 //Skip past whitespace (if any) following s[j]
144 for (i=j+1; j<n ; ) {
145 if (s.charAt(i)!=' ')
146 break;
147 i++;
148 }
149 }
150 String[] ret=new String[buf.size()];
151 for (int i=0; i<ret.length; i++)
152 ret[i]= buf.get(i);
153 return ret;
154 }
155
156
157
158 @Singleton
159 private static class MainCallback implements ActivityCallback {
160
161 /////////////////////////// ActivityCallback methods //////////////////////
162
163 public void connectionInitializing(RoutedConnection c) {
164 }
165
166 public void connectionInitialized(RoutedConnection c) {
167 // String host = c.getOrigHost();
168 // int port = c.getOrigPort();
169 ;//System.out.println("Connected to "+host+":"+port+".");
170 }
171
172 public void connectionClosed(RoutedConnection c) {
173 // String host = c.getOrigHost();
174 // int port = c.getOrigPort();
175 //System.out.println("Connection to "+host+":"+port+" closed.");
176 }
177
178 public void knownHost(Endpoint e) {
179 //Do nothing.
180 }
181
182 // public void handleQueryReply( QueryReply qr ) {
183 // synchronized(System.out) {
184 // System.out.println("Query reply from "+qr.getIP()+":"+qr.getPort()+":");
185 // try {
186 // for (Iterator iter=qr.getResults(); iter.hasNext(); )
187 // System.out.println(" "+((Response)iter.next()).getName());
188 // } catch (BadPacketException e) { }
189 // }
190 // }
191
192 /**
193 * Add a query string to the monitor screen
194 */
195 @Override
196 public void handleQuery(QueryRequest query, String address, int port) {
197 }
198
199
200 public void error(int errorCode) {
201 error(errorCode, null);
202 }
203
204 public void error(Throwable problem, String msg) {
205 problem.printStackTrace();
206 System.out.println(msg);
207 }
208
209 /**
210 * Implements ActivityCallback.
211 */
212 public void error(Throwable problem) {
213 problem.printStackTrace();
214 }
215
216 public void error(int message, Throwable t) {
217 System.out.println("Error: "+message);
218 t.printStackTrace();
219 }
220
221 ///////////////////////////////////////////////////////////////////////////
222
223
224 public void addDownload(Downloader mgr) {}
225
226 public void removeDownload(Downloader mgr) {}
227
228 public void addUpload(Uploader mgr) {}
229
230 public void removeUpload(Uploader mgr) {}
231
232 public boolean warnAboutSharingSensitiveDirectory(final File dir) { return false; }
233
234 public void handleSharedFileUpdate(File file) {}
235
236 public void downloadsComplete() {}
237
238 public void uploadsComplete() {}
239
240 public void promptAboutCorruptDownload(Downloader dloader) {
241 dloader.discardCorruptDownload(false);
242 }
243
244 public void dangerousDownloadDeleted(String filename) {}
245
246 public void restoreApplication() {}
247
248 public void showDownloads() {}
249
250 public String getHostValue(String key){
251 return null;
252 }
253 public void browseHostFailed(GUID guid) {}
254
255 public void updateAvailable(UpdateInformation update) {
256 if (update.getUpdateCommand() != null)
257 System.out.println("there's a new version out "+update.getUpdateVersion()+
258 ", to get it shutdown limewire and run "+update.getUpdateCommand());
259 else
260 System.out.println("You're running an older version. Get " +
261 update.getUpdateVersion() + ", from " + update.getUpdateURL());
262 }
263
264 public boolean isQueryAlive(GUID guid) {
265 return false;
266 }
267
268 public void componentLoading(String state, String component) {
269 System.out.println("Loading component: " + component);
270 }
271
272 public void handleMagnets(final MagnetOptions[] magnets) {
273 }
274
275 public void handleTorrent(File torrentFile){}
276
277 public void handleAddressStateChanged() {
278 }
279
280 public void handleConnectionLifecycleEvent(ConnectionLifecycleEvent evt) {
281 }
282 public void installationCorrupted() {
283
284 }
285 public void handleDAAPConnectionError(Throwable t) { }
286 public String translate(String s) { return s;}
287
288 @Override
289 public void handleDownloadException(DownloadAction downLoadAction,
290 DownloadException e, boolean supportsNewSaveDir) {
291
292 }
293
294 @Override
295 public boolean promptTorrentUploadCancel(Torrent torrent) {
296 return true;
297 }
298
299 @Override
300 public void handleQueryResult(RemoteFileDesc rfd, QueryReply queryReply,
301 Set<? extends IpPort> locs) {
302 synchronized(System.out) {
303 System.out.println("Query hit from "+rfd.getAddress() + ":");
304 System.out.println(" "+rfd.getFileName());
305 }
306 }
307 }
308}