📄 radio.java
字号:
/* Stream-2-Stream - Peer to peer television and radio
* October 13, 2005 - This file has been modified from the original P2P-Radio source
* Project homepage: http://s2s.sourceforge.net/
* Copyright (C) 2005-2006 Jason Hooks
*/
/*
* P2P-Radio - Peer to peer streaming system
* Project homepage: http://p2p-radio.sourceforge.net/
* Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
*
* ---------------------------------------------------------------------------
* 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 of the License, 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 p2pradio;
import p2pradio.gui.*;
import p2pradio.logging.*;
import p2pradio.sources.*;
import p2pradio.players.*;
import p2pradio.webinterface.*;
import stream2stream.XML.*;
import stream2stream.network.Peer;
import p2pradio.event.*;
import java.io.*;
import java.net.*;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Level;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JComboBox;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import ndt.Tcpbw100;
/**
* Starts everything and provides the command-line interface.
* It also manages the message handler for important and
* debug messages.
*
* @author Michael Kaufmann
*/
public class Radio implements Filter, BandwidthReceiver, UI
{
public static final String Name = Messages.getString("Radio.NAME"); //$NON-NLS-1$
public static final String Version = "2.0"; //$NON-NLS-1$
public static final int ypVersion = 3; //$NON-NLS-1$
public static final String s2sName = Messages.getString("Radio.S2S_NAME");
public static final String s2sHyphenName = Messages.getString("Radio.S2S_HYPHEN_NAME");
public static final String s2sVersion = "1.0";
public static final short networkVersion = 3;
public static final String NameSlashVersion = s2sHyphenName + "/" + ypVersion; //$NON-NLS-1$
public static final String HOMEPAGE = "http://s2s.sourceforge.net/"; //$NON-NLS-1$
public static boolean displayDebugMessages;
//public static boolean startMediaPlayerInListeningMode = true;
//public static boolean startMediaPlayerInBroadcastingMode = false;
public static boolean startMinimized = false;
public static boolean enableMonitor;
public static boolean isServer;
public static boolean startMediaPlayer;
public static boolean signOrVerify;
private Peer peer;
private NetworkComponentsContainer networkComponents;
private RadioGUI radioGUI;
private SettingsXML xml;
private double LANUpload;
private double LANDownload;
private String IP;
private double internetMaxUpload;
private double internetMaxDownload;
private double internetUpload;
private double internetDownload;
private TreeMap urlToStationname;
private boolean log;
private String logFilename;
private boolean negativeLag, addYP;
private BufferedReader commandLine;
private String url;
private Metadata metadata;
private boolean isConnected;
private UIPlayer uiPlayer;
private String stationName;
private double playerGain;
private void printUsageInformation()
{
System.out.println(Messages.getString("Radio.USAGE_INFORMATION", new Object[]{new Integer(Peer.DEFAULT_PORT_NR), new Integer(Peer.MAX_NUMBER_OF_CHILDREN)})); //$NON-NLS-1$
System.out.println();
System.exit(0);
}
private void printIntroduction()
{
System.out.println();
System.out.println(Messages.getString("Radio.WELCOME", s2sName)); //$NON-NLS-1$
System.out.println(Messages.getString("Radio.CONSOLE_SERVER_HELP"));
//System.out.println();
}
private void checkGlobals(String command)
{
if (command.equals("exit")) //$NON-NLS-1$
{
if (peer != null)
peer.shutdown();
System.exit(0);
}
else
System.out.println(Messages.getString("Radio.UNKNOWN_COMMAND")); //$NON-NLS-1$
}
public Radio(String argsString[], boolean isServer)
{
// Handler zuerst sowohl f黵 Kommandozeilen- als auch
// f黵 GUI-Bedienung aktivieren, damit man alle Fehler
// w鋒rend dem Starten sieht
Radio.isServer = isServer;
Handler logHandler = new ConsoleLogHandler();
logHandler.setFilter(this);
logHandler.setFormatter(new LogFormatter());
Logger.getLogger().addHandler(logHandler);
// Parameter in einen Vektor kopieren
Vector args = new Vector();
for (int i=0; i < argsString.length; i++)
{
args.add(argsString[i]);
}
// Keine grafische Oberfl鋍he?
boolean noGUI = parseBooleanArg(args, "nogui", false); //$NON-NLS-1$
boolean consoleUI = parseBooleanArg(args, "consoleui", false); //$NON-NLS-1$
String xmlName = parseStringArg(args, "x", SettingsXML.serverFilename);
// Keine grafische Oberfl鋍he bei Konsolenbedienung anzeigen
if (consoleUI)
{
noGUI = true;
}
if ((args.size() == 0) && !noGUI)
{
// Grafische Oberfl鋍he anzeigen und
// Fragen stellen
radioGUI = new RadioGUI(logHandler, isServer); //Set server to true
return;
}
/*
if (args.size() < 2)
{
// Zuwenige Parameter angegeben
printUsageInformation();
}*/
// Entweder grafische Oberfl鋍he ohne Fragen oder
// nur Kommandozeile
if (isServer) //$NON-NLS-1$
{
// Befehle entgegennehmen
xml = new SettingsXML(isServer, xmlName);
readXML();
printIntroduction();
commandLine = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
String command = null;
command = input();
if (command != null)
{
command = command.toLowerCase();
command = command.trim();
if (command.equals("help") || command.equals("?")) //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Messages.getString("Radio.CONSOLE_SERVER_HELP"));
else if (command.equals("cfg"))
{
System.out.println(Messages.getString("Radio.CONFIG", SettingsXML.serverFilename));
while (true)
{
command = input();
if (command.equals("\\b"))
{
System.out.println(Messages.getString("Radio.CONSOLE_SERVER_HELP"));
break;
}
else if (command.equals("1"))
doBandwidthTest();
else if (command.equals("help") || command.equals("?"))
System.out.println(Messages.getString("Radio.CONFIG", SettingsXML.serverFilename));
else
checkGlobals(command);
}
}
else if (command.equals("links"))
{
Set keys = urlToStationname.keySet();
Iterator keyIter = keys.iterator();
int link = 1;
while (keyIter.hasNext())
{
Object url = keyIter.next();
Object stationName = urlToStationname.get(url);
System.out.println("#" + link + "\t" + stationName + "@@" + url);
link++;
}
if (link == 1)
System.out.println(Messages.getString("Radio.NO_HISTORY"));
}
else if (command.equals("pri") || command.equals("pub") || command.equals("s"))
{
if(command.equals("pri"))
addYP = false;
else
addYP = true;
System.out.print(Messages.getString("Radio.INPUT_SHOUTICE"));
command = input();
if (command.equals("\\b"))
{
System.out.println(Messages.getString("Radio.CONSOLE_SERVER_HELP"));
continue;
}
else if (command.charAt(0) == '#')
{
Set keys = urlToStationname.keySet();
Iterator keyIter = keys.iterator();
command = command.substring(1, command.length());
int number = Integer.parseInt(command);
int link = 1;
while (keyIter.hasNext())
{
Object url = keyIter.next();
Object stationName = urlToStationname.get(url);
if (number == link)
command = (String) url;
link++;
}
}
url = command;
networkComponents = new NetworkComponentsContainer(this);
networkComponents.start();
}
else
checkGlobals(command);
}
}
}
if (!noGUI)
{
// Grafische Oberfl鋍he ab jetzt anzeigen
radioGUI = new RadioGUI(logHandler);
}
}
//jhooks - Previously only the default port was being checked
//I changed this to check the default, default + 1(player), default + 2 (web)
public static int getBestPortNumber()
{
int port = Peer.DEFAULT_PORT_NR;
/*if (isPortNrOK(port))
System.out.println("Port: " + port + " is OK");
if (isPortNrOK(port + 1))
System.out.println("Port: " + (port + 1) + " is OK");
if (isPortNrOK(port + 2))
System.out.println("Port: " + (port + 2) + " is OK");*/
if (isPortNrOK(port) && isPortNrOK(port + Peer.WEBINTERFACE_PORT_OFFSET) && isPortNrOK(port + Peer.PLAYER_PORT_OFFSET))
{
return port;
}
else
{
return getRandomPortNumber();
}
}
/**
* Returns a random non-privileged (bigger than 1023) port number. This method checks that
* the returned port number and the two following port numbers are free for both TCP and UDP.
*/
private static int getRandomPortNumber()
{
// Drei Portnummern nacheinander m黶sen frei sein
// (es wird sowohl TCP als auch UDP getestet)
Random random = new Random();
int portNr = 1024 + random.nextInt((65535 - 2) - 1024);
if (!isPortNrOK(portNr)) return getRandomPortNumber();
if (!isPortNrOK(portNr+1)) return getRandomPortNumber();
if (!isPortNrOK(portNr+2)) return getRandomPortNumber();
// portNr ist OK
return portNr;
}
private static boolean isPortNrOK(int portNr)
{
try
{
ServerSocket serverSocket = new ServerSocket(portNr);
serverSocket.close();
}
catch(IOException e)
{
return false;
}
try
{
DatagramSocket datagramSocket = new DatagramSocket(portNr);
datagramSocket.close();
}
catch(IOException e)
{
return false;
}
return true;
}
protected static final boolean parseBooleanArg(Vector args, String parameter, boolean defaultValue)
{
for (int i=0; i < args.size(); i++)
{
String arg = ((String)args.get(i)).toLowerCase();
if (arg.equals("-" + parameter.toLowerCase()) || arg.equals("--" + parameter.toLowerCase()) || arg.equals("/" + parameter.toLowerCase())) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
{
args.remove(i);
return true;
}
}
return defaultValue;
}
protected static final int parseIntArg(Vector args, String parameter, int defaultValue)
{
for (int i=0; i < args.size(); i++)
{
String arg = ((String)args.get(i)).toLowerCase();
if (arg.equals("-" + parameter.toLowerCase()) || arg.equals("--" + parameter.toLowerCase()) || arg.equals("/" + parameter.toLowerCase())) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
{
try
{
int result = Integer.parseInt((String)args.get(i+1));
args.remove(i+1);
args.remove(i);
return result;
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -