PageRenderTime 101ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/limewire/core/com/limegroup/gnutella/Main.java

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