public static void main ( String [ ] args ) { BasicConfigurator . configure ( new ConsoleAppender ( new PatternLayout ( "%d [%-25t] %-5p: %m%n" ) ) ) ; CmdLineParser parser = new CmdLineParser ( ) ; CmdLineParser . Option help = parser . addBooleanOption ( ' ' , "help" ) ; CmdLineParser . Option port = parser . addIntegerOption ( ' ' , "port" ) ; try { parser . parse ( args ) ; } catch ( CmdLineParser . OptionException oe ) { System . err . println ( oe . getMessage ( ) ) ; usage ( System . err ) ; System . exit ( 1 ) ; } // Display help and exit if requested if ( Boolean . TRUE . equals ( ( Boolean ) parser . getOptionValue ( help ) ) ) { usage ( System . out ) ; System . exit ( 0 ) ; } Integer portValue = ( Integer ) parser . getOptionValue ( port , Integer . valueOf ( Tracker . DEFAULT_TRACKER_PORT ) ) ; String [ ] otherArgs = parser . getRemainingArgs ( ) ; if ( otherArgs . length > 1 ) { usage ( System . err ) ; System . exit ( 1 ) ; } // Get directory from command-line argument or default to current // directory String directory = otherArgs . length > 0 ? otherArgs [ 0 ] : "." ; FilenameFilter filter = new FilenameFilter ( ) { @ Override public boolean accept ( File dir , String name ) { return name . endsWith ( ".torrent" ) ; } } ; try { Tracker t = new Tracker ( portValue ) ; File parent = new File ( directory ) ; for ( File f : parent . listFiles ( filter ) ) { logger . info ( "Loading torrent from " + f . getName ( ) ) ; t . announce ( TrackedTorrent . load ( f ) ) ; } logger . info ( "Starting tracker with {} announced torrents..." , t . getTrackedTorrents ( ) . size ( ) ) ; t . start ( true ) ; } catch ( Exception e ) { logger . error ( "{}" , e . getMessage ( ) , e ) ; System . exit ( 2 ) ; } }
Main function to start a tracker .