public void receiveTarget() { // 接收用户输入的地址,向targets队列中加入任务 try { BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while ((msg = localReader.readLine()) != null) { if (!msg.equals("bye")) { Target target = new Target(msg); addTarget(target); } else { shutdown = true; selector.wakeup(); break; } } } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) throws FileNotFoundException, IOException { /*Get variables ready*/ String torrentFN; String saveFN; ArrayList<Peer> peers = null; byte[] b = null; /*check command line arguments*/ if (args.length == 2) { torrentFN = args[0]; saveFN = args[1]; file_destination = saveFN; } else { System.out.println("Invalid number of command line arguments"); return; } RandomAccessFile fSave = null; try { fSave = new RandomAccessFile(new File(saveFN), "rw"); } catch (FileNotFoundException e) { } /*read torrent file*/ BufferedReader reader = null; try { b = Files.readAllBytes(Paths.get(torrentFN)); } catch (FileNotFoundException e) { System.out.println("Caught Exception File not found"); // e.printStackTrace(); } catch (IOException e) { System.out.println("Caught Exception IOException"); // e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { System.out.println("Caught Exception IOException"); // e.printStackTrace(); } } /*send bytes to helper class*/ try { tInfo = new TorrentInfo(b); try { fSave.setLength((long) tInfo.file_length); byte[] empty = new byte[tInfo.file_length]; fSave.write(empty, 0, empty.length); } catch (IOException e) { } globalMemory = new MemCheck(tInfo, fSave); System.out.println("File mem length is " + fSave.length()); System.out.println(tInfo.file_name); } catch (BencodingException e) { System.out.println("Bencoding Exception"); } try { tResponse = TrackerResponse.getTrackerResponseStart(tInfo, 0, 0, tInfo.file_length); } catch (Exception e) { System.out.println("Problem with GET Request, program exiting"); return; } /*decode the tracker response*/ try { tResponseDecoded = TrackerResponse.decodeTrackerResponse(tResponse); } catch (Exception e) { System.out.println("Problem decoding tracker response"); return; } peers = tResponseDecoded.peers; int peerIndex; info_hash = tInfo.info_hash.array(); TrackerThread announceThread = new TrackerThread(); // in actual production peerIndex < peers.size(); for (peerIndex = 0; peerIndex < peers.size(); peerIndex++) { if (!peers.get(peerIndex).ipAdd.equals("128.6.171.132")) { Download peer = new Download(peers.get(peerIndex).ipAdd, peers.get(peerIndex).port); /*Lets schedule everything together*/ new Thread(peer).start(); } } new Thread(announceThread).start(); }