mainframe.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 1,252 行 · 第 1/3 页

JAVA
1,252
字号
/* 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.gui;

import p2pradio.*;
import p2pradio.logging.*;
import p2pradio.players.*;
import p2pradio.tools.BrowserLauncher;
import stream2stream.XML.*;
import stream2stream.network.Peer;
import p2pradio.event.*;
import stream2stream.stationlist.YellowPages;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.TreeMap;
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.crypto.spec.SecretKeySpec;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;

import ndt.Tcpbw100;


/**
 * The main frame of the GUI.
 * 
 * @author Michael Kaufmann
 */
public class MainFrame extends JFrame implements Filter, BandwidthReceiver, UI
{
	public final static String p2pradioIcon = "Icon.png";
	public final static String s2sIcon = "s2s.png";
	public final static String s2sSmallIcon = "s2ssmall.png";
	public final static int VOLUME_SLIDER_MAX = 100;
	private Peer peer;	
	private NetworkComponentsContainer networkComponents;
	private Metadata metadata;
	
		 
	// Men黮eiste
	private JMenuBar MenuBar = new JMenuBar();
	//File
	private JMenu MenuFile = new JMenu(Messages.getString("MainFrame.MENU_FILE")); //$NON-NLS-1$
	private JMenuItem MenuYellowPages = new JMenuItem(Messages.getString("MainFrame.CHOOSE_NEW_STATION")); //$NON-NLS-1$
	private JMenuItem MenuInformationenAnzeigen = new JMenuItem(Messages.getString("MainFrame.DISPLAY_VERBOSE_INFORMATION")); //$NON-NLS-1$
	
	private JMenuItem MenuLogLoeschen = new JMenuItem(Messages.getString("MainFrame.MENU_CLEAR_LOG")); //$NON-NLS-1 //$NON-NLS-1$
	private JMenuItem MenuLogAs = new JMenuItem(Messages.getString("MainFrame.MENU_LOG_AS")); //$NON-NLS-1 //$NON-NLS-1$
	private JMenuItem MenuLog = new JMenuItem(Messages.getString("MainFrame.MENU_LOG")); //$NON-NLS-1 //$NON-NLS-1$
	
	private JMenuItem MenuBeenden = new JMenuItem(Messages.getString("MainFrame.MENU_EXIT")); //$NON-NLS-1$
	
	private JMenu MenuMedia = new JMenu(Messages.getString("MainFrame.MENU_MEDIA"));
	private JMenuItem MenuLaunchIntegratedPlayer = new JMenuItem(Messages.getString("MainFrame.MENU_LAUNCH_INTEGRATED_PLAYER"));
	private JMenuItem MenuMediaPlayerStarten = new JMenuItem(Messages.getString("MainFrame.MENU_LAUNCH_MEDIA_PLAYER")); //$NON-NLS-1$
	private JMenuItem MenuRecordStream = new JMenuItem(Messages.getString("MainFrame.MENU_RECORD_STREAM")); //$NON-NLS-1 
	
	//Settings
	private JMenu MenuEinstellungen = new JMenu(Messages.getString("MainFrame.MENU_SETTINGS")); //$NON-NLS-1$
	private JMenuItem MenuBandbreiteSetzen = new JMenuItem(Messages.getString("MainFrame.MENU_SET_BANDWIDTH")); //$NON-NLS-1$
	private JMenuItem MenuOptions = new JMenuItem(Messages.getString("MainFrame.MENU_OPTIONS"));
	private JCheckBoxMenuItem MenuDebugNachrichtenAnzeigen = new JCheckBoxMenuItem(Messages.getString("MainFrame.MENU_SHOW_DEBUG_MESSAGES"), Radio.displayDebugMessages); //$NON-NLS-1$
			//Help
	private JMenu MenuHilfe = new JMenu(Messages.getString("MainFrame.MENU_HELP")); //$NON-NLS-1$
	private JMenuItem MenuHomepageBesuchen = new JMenuItem(Messages.getString("MainFrame.MENU_VISIT_HOMEPAGE", Radio.s2sName)); //$NON-NLS-1$
	private JMenuItem MenuInfo = new JMenuItem(Messages.getString("MainFrame.MENU_INFO")); //$NON-NLS-1$
	
	private JSlider volumeSlider = new JSlider(0, VOLUME_SLIDER_MAX);
	
	
	// Anzeigeelemente
	private JPanel LabelsPanel;
	private JPanel TopPanel;
	private JPanel InputPanel;
	private OptionsPanel optionsPanel;
	private StationPanel stationPanel;
	private JPanel AboutPanel;
	
	private JLabel LabelStation = new JLabel(Messages.getString("MainFrame.LABEL_STATION")); //$NON-NLS-1$
	private JLabel LabelStream = new JLabel(Messages.getString("MainFrame.LABEL_STREAM")); //$NON-NLS-1$
	private JLabel LabelBandwidth = new JLabel(Messages.getString("MainFrame.LABEL_BANDWIDTH"));
	private JLabel LabelSong = new JLabel(Messages.getString("MainFrame.LABEL_SONG")); //$NON-NLS-1$
	
	//private JLabel LabelLag = new JLabel(Messages.getString("MainFrame.LABEL_LAG_MS")); //$NON-NLS-1$
	private JLabel LabelServerUptime = new JLabel(Messages.getString("MainFrame.LABEL_SERVER_UPTIME")); //$NON-NLS-1$
	
	private JLabel LabelStationContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
	private JLabel LabelStreamContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
	private JLabel LabelBandwidthContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL"));
	private JLabel LabelSongContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
	//private JLabel LabelLagContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
	private JLabel LabelServerUptimeContent = new JLabel(Messages.getString("MainFrame.EMPTY_LABEL")); //$NON-NLS-1$
	
	private JComboBox urlInput;
	private JLabel urlInputLabel;
	private JButton button1;
	private JButton button2;
	
	private JTextArea messages = new JTextArea();
	private JScrollPane messagesPane = new JScrollPane(messages);
	
	private JPanel MainPanel = new JPanel(new BorderLayout());
	private long Time_Server_Has_Been_Running_At_Startup;  //The time the server has been running when we start P2PRadio (in milliseconds)
															//Will always be 0 if we are the server
	private long Server_Running_Minus_Start_Time, startTime, timeStamp, lag;
	
	private boolean lagIsValid, isServer, isConnected, isConnecting, addYP, LANUploadSet, portSet
	, log, startMediaPlayerAtStartup, negativeLag, testBandwidth, serverUsesVerifiedBandwidth;
	private int MinimumChildrenClientsMustServe;
	
	private double LANUpload, LANDownload, internetMaxUpload, internetMaxDownload, internetUpload, internetDownload, playerGain;
	private int port;
	private String url, logFilename, IP;
	private String verifiedBandwidth;
	
	private RadioGUI gui;
	private MainFrame selfReference;
	private UIPlayer uiPlayer;
	private File recordStreamTo;
	
	private TreeMap selectionToUrl;
	private TreeMap urlToStationname;
	
	private JTabbedPane tabbedPane = new JTabbedPane();
	private SettingsXML xml;
	private boolean test;
	
	public MainFrame(RadioGUI gui)
	{
		this.isServer = gui.getIsServer();
		this.gui = gui;
	
		isConnecting=isConnected=addYP=lagIsValid=false;
		lag=timeStamp=Server_Running_Minus_Start_Time=startTime=0;
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		selfReference = this;
		Handler logHandler = new GUILogHandler(messages);
		logHandler.setFilter(this);
		logHandler.setFormatter(new LogFormatter());
		Logger.getLogger().addHandler(logHandler);
		String filename = SettingsXML.clientFilename;
		if (isServer)
			filename = SettingsXML.serverFilename;
		xml = new SettingsXML(isServer, filename);
		readXML();
		try 
		{
			Init();
		}
		catch (Exception e)
		{
			Logger.severe("MainFrame", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
		}
		
		
		
	}
	private Object[] ComboBoxFormat(){
		selectionToUrl = new TreeMap();
		//ArrayList selections = new ArrayList();
		Set keys = urlToStationname.keySet();
		 Iterator keyiter = keys.iterator();
		  while(keyiter.hasNext()){
			  String url = (String) keyiter.next();
			  String stationName = (String) urlToStationname.get(url);
			  makeSelection(url, stationName);
		  }
		  Object[] comboBoxFormat = selectionToUrl.keySet().toArray(); 
		  return comboBoxFormat;
	}
	private String makeSelection(String url, String stationName)
	{
		  String selection = stationName + " @@ " + url;
		  //selections.add(selection);
		  selectionToUrl.put(selection, url);
		  return selection;
	}
	public void readXML()
	{
		//
		
		Object[] selections = null;
		
		//Get the settings we just read from the xml file
		//If the xml file cannot be found or is invalid, defaults will be read
		
		//LAN
		if (isServer)
		{
			serverUsesVerifiedBandwidth = xml.This_Server_Uses_Verified_Bandwidth();
			MinimumChildrenClientsMustServe = xml.getMinimumChildrenClientsMustServe();
		}
		LANUpload = xml.getLANUpload();
		LANDownload = xml.getLANDownload();
		if (LANUpload != -2)
		{
			String[] logInputs = {"" + LANUpload, "" + LANDownload};
			Logger.info("MainFrame","MainFrame.BANDWIDTH_LIMIT_SET_TO", logInputs);
		}
		
		//INTERNET
		IP = xml.getIP();
		if (IP == null)
		{
			test = xml.testBandwidth();
			if (test == true)
			{
				Logger.info("MainFrame", "MainFrame.VERIFIED_BANDWIDTH_LIMIT_NOT_SET_FIRST");
			}
			else
			{
				Logger.info("MainFrame", "MainFrame.VERIFIED_BANDWIDTH_LIMIT_NOT_SET");
			}
		}
		else
		{
			internetMaxUpload = xml.Get_Internet_Upload_Max();
			internetMaxDownload = xml.Get_Internet_Download_Max();
			internetUpload = xml.Get_Internet_Upload_Limit();
			internetDownload = xml.Get_Internet_Download_Limit();
		
			String[] logInputs = {IP, "" + internetUpload, "" + internetDownload};
			Logger.info("MainFrame", "MainFrame.VERIFIED_BANDWIDTH_LIMIT_SET_TO", logInputs);
		}
		//HISTORY
		urlToStationname = xml.getLinks();
		if (urlInput == null)
		{
			if (urlToStationname != null){
				selections = ComboBoxFormat();
				urlInput = new JComboBox(selections);
			}
			else 
				urlInput = new JComboBox();
			urlInput.setEditable(true);
		}
		
		port = xml.getPort();
		log = xml.getLogByDate();
		if (log)
		{
			Radio.logByDate(this);
			MenuLog.setEnabled(false);
		}
		Radio.startMediaPlayer = xml.startMediaPlayerAtStartup();
		Radio.displayDebugMessages = xml.getDisplayDebug();
		MenuDebugNachrichtenAnzeigen.setSelected(xml.getDisplayDebug());
		
		logFilename = xml.getLogAs();
		if (!(logFilename == null || logFilename.equals("")))
		{
			Radio.logAs(logFilename, this);
			MenuLogAs.setEnabled(false);
		}
		Radio.enableMonitor = xml.getEnableMonitor();
		Radio.signOrVerify = xml.getSignOrVerify();
		//End of getting settings
		negativeLag = xml.getNegativeLagEnabled();
		volumeSlider.setValue((int)(xml.getPlayerGain() * (double) VOLUME_SLIDER_MAX));
		
	}
	
	private void Init() throws Exception 
	{
		if (isServer)
		{
			urlInputLabel = new JLabel(Messages.getString("MainFrame.LABEL_INPUT_SERVER"));
			button1 = new JButton (Messages.getString("MainFrame.BUTTON_SERVE_PUBLIC"));
			button2 = new JButton (Messages.getString("MainFrame.BUTTON_SERVE_PRIVATE"));
		}
		else
		{
			urlInputLabel = new JLabel(Messages.getString("MainFrame.LABEL_INPUT_CLIENT"));
			button1 = new JButton (Messages.getString("MainFrame.BUTTON_JOIN"));
			button2 = new JButton (Messages.getString("MainFrame.BUTTON_BUMP"));
			button2.setVisible(false);
		}
		button1.addActionListener(new ActionListener()//public
		{
			public void actionPerformed(ActionEvent e)
			{
				if  (isServer)
				{
					if(isConnecting)
					{
						//peer.leave();
						if(addYP)
						{
							peer.getBroadcastBuffer().addToYP(selfReference);
							button1.setText(Messages.getString("MainFrame.BUTTON_REMOVE_YP"));
							addYP = false;
						}
						else //Remove from yellow pages
						{
							peer.getBroadcastBuffer().removeFromYP();
							button1.setText(Messages.getString("MainFrame.BUTTON_ADD_YP"));
							button1.setEnabled(false);
							addYP = true;
						}
					}
					else
					{ 
						url = (String)urlInput.getSelectedItem();
						if (selectionToUrl != null && selectionToUrl.containsKey(url))
							url = (String) selectionToUrl.get(url);
						
						addYP = true;
						networkComponents = new NetworkComponentsContainer(selfReference);
						networkComponents.start();
							
					}
				}
				else //if Client
				{
					if(isConnecting)  //leave
					{
						networkComponents.shutdown();
						
						isConnecting = false;
						button2.setVisible(false);
						button1.setText(Messages.getString("MainFrame.BUTTON_JOIN"));
					}
					else              //join
					{
						isConnecting = true;
						url = (String)urlInput.getSelectedItem();
						if (selectionToUrl != null && selectionToUrl.containsKey(url))
						{
							url = (String) selectionToUrl.get(url);
						}
						
										//We are inside an inner class
										//selfRefence is a reference to the MainFrame itself
						networkComponents = new NetworkComponentsContainer(selfReference);
						networkComponents.start();
					}
				}	
			}
		});
		button2.addActionListener(new ActionListener()//public
		{
			public void actionPerformed(ActionEvent e)
			{
				
				if  (isServer)
				{
					
					url = (String)urlInput.getSelectedItem();
					if (selectionToUrl != null && selectionToUrl.containsKey(url))
						url = (String) selectionToUrl.get(url);
					addYP = false;
					networkComponents = new NetworkComponentsContainer(selfReference);
					networkComponents.start();
						
					
				}
				else if (isConnected) //if Client
				{
					System.out.println("BUMP");
					peer.changeSupplier(true, false);
				}	
			}
		});
		
			
		
		if (isServer)
		{

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?