PageRenderTime 106ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/limewire/components/gnutella-core/src/main/java/com/limegroup/gnutella/Main.java

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