/* Snark - Main snark program startup class.
Copyright (C) 2003 Mark J. Wielaard
This file is part of Snark.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.klomp.snark;
import java.io.*;
import java.net.*;
import java.util.*;
import org.klomp.snark.bencode.*;
/**
* Main Snark program startup class.
*
* @author Mark Wielaard (mark@klomp.org)
*/
public class Snark
implements StorageListener, CoordinatorListener, ShutdownListener
{
private final static int MIN_PORT = 6881;
private final static int MAX_PORT = 6889;
// Error messages (non-fatal)
public final static int ERROR = 1;
// Warning messages
public final static int WARNING = 2;
// Notices (peer level)
public final static int NOTICE = 3;
// Info messages (protocol policy level)
public final static int INFO = 4;
// Debug info (protocol level)
public final static int DEBUG = 5;
// Very low level stuff (network level)
public final static int ALL = 6;
/**
* What level of debug info to show.
*/
public static int debug = NOTICE;
// Whether or not to ask the user for commands while sharing
private static boolean command_interpreter = true;
private static final String newline = System.getProperty("line.separator");
private static final String copyright =
"The Hunting of the Snark Project - Copyright (C) 2003 Mark J. Wielaard"
+ newline + newline
+ "Snark comes with ABSOLUTELY NO WARRANTY. This is free software, and"
+ newline
+ "you are welcome to redistribute it under certain conditions; read the"
+ newline
+ "COPYING file for details.";
private static final String usage =
"Press return for help. Type \"quit\" and return to stop.";
private static final String help =
"Commands: 'info', 'list', 'quit'.";
// String indicating main activity
static String activity = "Not started";
public static void main(String[] args)
{
System.out.println(copyright);
System.out.println();
// Parse debug, share/ip and torrent file options.
Snark snark = parseArguments(args);
SnarkShutdown snarkhook
= new SnarkShutdown(snark.storage,
snark.coordinator,
snark.acceptor,
snark.trackerclient,
snark);
Runtime.getRuntime().addShutdownHook(snarkhook);
Timer timer = new Timer(true);
TimerTask monitor = new PeerMonitorTask(snark.coordinator);
timer.schedule(monitor,
PeerMonitorTask.MONITOR_PERIOD,
PeerMonitorTask.MONITOR_PERIOD);
// Start command interpreter
if (Snark.command_interpreter)
{
boolean quit = false;
System.out.println();
System.out.println(usage);
System.out.println();
try
{
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
String line = br.readLine();
while(!quit && line != null)
{
line = line.toLowerCase();
if ("quit".equals(line))
quit = true;
else if ("list".equals(line))
{
synchronized(coordinator.peers)
{
System.out.println(coordinator.peers.size()
+ " peers -"
+ " (i)nterested,"
+ " (I)nteresting,"
+ " (c)hoking,"
+ " (C)hoked:");
Iterator it = coordinator.peers.iterator();
while (it.hasNext())
{
Peer peer = (Peer)it.next();
System.out.println(peer);
System.out.println("\ti: " + peer.isInterested()
+ " I: " + peer.isInteresting()
+ " c: " + peer.isChoking()
+ " C: " + peer.isChoked());
}
}
}
else if ("info".equals(line))
{
System.out.println("Name: " + meta.getName());
System.out.println("Torrent: " + torrent);
System.out.println("Tracker: " + meta.getAnnounce());
List files = meta.getFiles();
System.out.println("Files: "
+ ((files == null) ? 1 : files.size()));
System.out.println("Pieces: " + meta.getPieces());
System.out.println("Piece size: "
+ meta.getPieceLength(0) / 1024
+ " KB");
System.out.println("Total size: "
+ meta.getTotalLength() / (1024 * 1024)
+ " MB");
}
else if ("".equals(line) || "help".equals(line))
{
System.out.println(usage);
System.out.println(help);
}
else
{
System.out.println("Unknown command: " + line);
System.out.println(usage);
}
if (!quit)
{
System.out.println();
line = br.readLine();
}
}
}
catch(IOException ioe)
{
debug("ERROR while reading stdin: " + ioe, ERROR);
}
// Explicit shutdown.
Runtime.getRuntime().removeShutdownHook(snarkhook);
snarkhook.start();
}
}
static String torrent;
static MetaInfo meta;
static Storage storage;
static PeerCoordinator coordinator;
static ConnectionAcceptor acceptor;
static TrackerClient trackerclient;
private Snark(String torrent, String ip, int user_port,
StorageListener slistener, CoordinatorListener clistener)
{
if (slistener == null)
slistener = this;
if (clistener == null)
clistener = this;
this.torrent = torrent;
activity = "Network setup";
// "Taking Three as the subject to reason about--
// A convenient number to state--
// We add Seven, and Ten, and then multiply out
// By One Thousand diminished by Eight.
//
// "The result we proceed to divide, as you see,
// By Nine Hundred and Ninety Two:
// Then subtract Seventeen, and the answer must be
// Exactly and perfectly true.
// Create a new ID and fill it with something random. First nine
// zeros bytes, then three bytes filled with snark and then
// sixteen random bytes.
byte snark = (((3 + 7 + 10) * (1000 - 8)) / 992) - 17;
byte[] id = new byte[20];
Random random = new Random();
int i;
for (i = 0; i < 9; i++)
id[i] = 0;
id[i++] = snark;
id[i++] = snark;
id[i++] = snark;
while (i < 20)
id[i++] = (byte)random.nextInt(256);
Snark.debug("My peer id: " + PeerID.idencode(id), Snark.INFO);
int port;
IOException lastException = null;
ServerSocket serversocket = null;
if (user_port != -1)
{
port = user_port;
try
{
serversocket = new ServerSocket(port);
}
catch(IOException ioe)
{
lastException = ioe;
}
}
else
{
for (port = MIN_PORT; serversocket == null && port <= MAX_PORT; port++)
{
try
{
serversocket = new ServerSocket(port);
}
catch(IOException ioe)
{
lastException = ioe;
}
}
}
if (serversocket == null)
{
String message = "Cannot accept incoming connections ";
if (user_port == -1)
message = message + "tried ports " + MIN_PORT + " - " + MAX_PORT;
else
message = message + "on port " + user_port ;
if (ip != null || user_port != -1)
fatal(message, lastException);
else
debug("WARNING: " + message, WARNING);
port = -1;
}
else
{
port = serversocket.getLocalPort();
debug("Listening on port: " + port, Snark.INFO);
}
// Figure out what the torrent argument represents.
meta = null;
File f = null;
try
{
InputStream in;
f = new File(torrent);
if (f.exists())
in = new FileInputStream(f);
else
{
activity = "Getting torrent";
URL u = new URL(torrent);
UR
snark-0.5.tar.gz
需积分: 9 185 浏览量
2008-09-11
02:18:16
上传
评论
收藏 57KB GZ 举报
blueice_02
- 粉丝: 0
- 资源: 12
最新资源
- vb题库管理系统设计(源代码+论文).rar
- [信息办公]PHP Classifieds v7.3_classifieds.rar
- [检测统计]北雨访问记数器系统_bycount.rar
- HonoCMS 内容发布系统 v1.0_honocms_cn_CMS程序开发模板(使用说明+源代码+html).zip
- 手机在线点餐模板.zip
- [搜索链接]Page Rank查询_pagerank.rar
- matlab imu和视觉里程计 kalman滤波器 进行融合.rar
- matlab 该模拟器由构成IEEE802.11a协议物理层的多个部分集成
- mmexport1713552623466.jpg
- matlab Physical layer of the IEEE 802.11ac simulator.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈