networkcomponentscontainer.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 435 行

JAVA
435
字号
/* Stream-2-Stream - Peer to peer television and radio
 * December 19, 2005 - This is a new file, but parts were taken from the original P2P-Radio source
 * February 6, 2006 - Renamed to NetworkComponentsContainer
 * 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 java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JOptionPane;

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

import p2pradio.gui.MainFrame;
import p2pradio.gui.RadioGUI;
import p2pradio.logging.Logger;
import p2pradio.packets.PacketFactory;
import p2pradio.players.HttpPlayer;
import p2pradio.players.MediaPlayerLauncher;
import p2pradio.sources.HttpSource;
import p2pradio.webinterface.WebServer;
import stream2stream.XML.SettingsXML;
import stream2stream.network.Peer;
import stream2stream.network.PeerException;
import stream2stream.network.RemotePeer;
import p2pradio.event.*;

public class NetworkComponentsContainer extends Thread {
	private String url;
	private UI ui ;
	private boolean isServer,portSet,maxUploadBandwidthSet, shutdown;
	private int port;
	private double LANUpload;
	private boolean addYP;
	private SettingsXML xml;
	
	//The components
	private Peer peer;
	private HttpPlayer httpPlayer;
	private WebServer webServer;
	private BasicPlayer player;
	
	public NetworkComponentsContainer (UI ui){
		super(Messages.getString("ConnectThread.THREAD_NAME"));
		shutdown = false;
		url = ui.getUrl();
		xml = ui.getSettingsXML();
		this.ui = ui;
		isServer = ui.getIsServer();
		addYP = ui.getAddYP();
		this.port = xml.getPort();
		this.LANUpload = xml.getLANUpload();
		//this.maxUploadBandwidthSet = mainFrame.getMaxUploadBandwidthSet();
		this.portSet = port != 2000;
	}
	
	private void handleError(String message){
		if (ui instanceof MainFrame)
		{
			MainFrame ui = (MainFrame) this.ui;
			if (message != null)
			{
				JOptionPane.showMessageDialog(ui, message, Messages.getString("GENERAL_ERROR"), JOptionPane.ERROR_MESSAGE, null); //$NON-NLS-1$
			}
		}
		else
			System.out.println(Messages.getString("GENERAL_ERROR") + ":  " + message);
		
		if (peer != null)
		{
			peer.leave();
		}
		
	}
	private void exit()
	{
		if (ui instanceof MainFrame)
		{
			MainFrame ui = (MainFrame) this.ui;
			ui.comeOutButton1();
		}
	}
	public void run()
	{
		if (isServer)
		{
			// String[] shoutcastURLs = getChoices("Shoutcast Stations.txt");
			if (xml.getStreamMode()  == SettingsXML.TCP)
				PacketFactory.STREAM_PACKET_MAX_SIZE = PacketFactory.STREAM_PACKET_MAX_SIZE_TCP_OGG;
			else
				PacketFactory.STREAM_PACKET_MAX_SIZE = PacketFactory.STREAM_PACKET_MAX_SIZE_UDP;
			
			URL playlist = null;
									
			try
			{
				// Leere Adresse?
				if (((String)url).length() == 0  || url == null)
				{
					throw new MalformedURLException();
				}
				playlist = new URL((String)url);
			}
			catch (MalformedURLException e)
			{
				handleError(Messages.getString("INVALID_PLAYLIST_ADDRESS")); 
				return; //$NON-NLS-1$
			}
			if (shutdown)
				return;
			
			
			InetSocketAddress monitorAddress = null;
				
			// Monitoradresse lesen
			if (xml.getEnableMonitor())
			{
				String monitorAddr = xml.getMonitorAddress();//JOptionPane.showInputDialog(ui, Messages.getString("RadioGUI.ENTER_MONITOR_ADDRESS"), Messages.getString("RadioGUI.ENTER_MONITOR_ADDRESS_TITLE"), JOptionPane.QUESTION_MESSAGE, null, null, ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				
				if ((monitorAddr != null) && (((String)monitorAddr).length() != 0))
				{
					String addr = (String)monitorAddr;
					String remoteHost;
					int remotePort;
					int pos;
	
					if ((pos = addr.indexOf(":")) == -1) //$NON-NLS-1$
					{
						remoteHost = addr;
						remotePort = Peer.MONITOR_PORT;
					}
					else
					{
						try
						{
							remoteHost = addr.substring(0, pos);
							remotePort = Integer.parseInt(addr.substring(pos+1, addr.length()));
						}
						catch (NumberFormatException e)
						{
							remoteHost = addr;
							remotePort = Peer.MONITOR_PORT;
						}
					}
					
					try
					{
						monitorAddress = new InetSocketAddress(remoteHost, remotePort);
			
						if (monitorAddress.getAddress() == null)
						{
							 throw new IllegalArgumentException();
						}
					}
					catch (Exception e)
					{
						handleError(Messages.getString("INVALID_MONITOR_ADDRESS")); //$NON-NLS-1$
						return;
					}
					
					if (monitorAddress.getAddress().isLoopbackAddress())
					{
						if (ui instanceof MainFrame)
						{
							MainFrame ui = (MainFrame) this.ui;
							JOptionPane.showMessageDialog(ui, Messages.getString("RadioGUI.LOCAL_MONITOR_ADDRESS", addr), Messages.getString("RadioGUI.LOCAL_MONITOR_ADDRESS_TITLE"), JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
						}
						Logger.info("CreatePeerThread", "RadioGUI.LOCAL_MONITOR_ADDRESS");
						
					}	
				}
			}
			if (shutdown)
				return;
			
			HttpSource source = new HttpSource(addYP, playlist);
			
			Logger.info("RadioGUI", "CONNECTING_TO_SERVER"); //$NON-NLS-1$ //$NON-NLS-2$
			if (shutdown)
				return;
			
			if (!source.connect())
			{
				handleError(Messages.getString("COULD_NOT_CONNECT_TO_SERVER")); //$NON-NLS-1$
				return;
			}
			if (shutdown)
				return;
			
			
			
			if (!portSet)
			{
				port = Radio.getBestPortNumber();
			}
			/*
			if (!maxUploadBandwidthSet)
			{
				maxUploadBandwidth = inputMaxUploadBandwidth();
			}*/
			try
			{	
				peer = new Peer(port, monitorAddress, ui);
			}
			catch (PeerException e)
			{
				handleError(e.getMessage());
				return;
			}
			if (shutdown)
				return;
			source.setBroadcastBuffer(peer.getBroadcastBuffer());
			source.start();
			
			
			Logger.finer("RadioGUI", "I_AM_THE_SERVER", peer); //$NON-NLS-1$ //$NON-NLS-2$
				
			
			
		}
		
		
		
		else
		{			
			
			if (url == null || ((String)url).length() == 0)
			{
				handleError(Messages.getString("INVALID_SERVER_ADDRESS")); 
				//System.out.println("" + url);
				exit();
				return; //$NON-NLS-1$
			}
			
			// Serveradresse lesen
			
			String addr = Radio.removePrefix((String)url);
			String remoteHost;
			int remotePort;
			int pos;
			//System.out.println("1");
			if ((pos = addr.indexOf(":")) == -1) //$NON-NLS-1$
			{
				remoteHost = addr;
				remotePort = Peer.DEFAULT_PORT_NR;
			}
			else
			{
				try
				{
					remoteHost = addr.substring(0, pos);
					remotePort = Integer.parseInt(addr.substring(pos+1, addr.length()));
				}
				catch (NumberFormatException e)
				{
					remoteHost = addr;
					remotePort = Peer.DEFAULT_PORT_NR;
				}
			}
			
			if (shutdown)
				return;
			//System.out.println("2");
			// Die Adresse des Servers
			InetSocketAddress serverAddress = null;
			try
			{
				// Leere Adresse?
				if (remoteHost.length() == 0)
				{
					throw new MalformedURLException();
				}
				serverAddress = new InetSocketAddress(remoteHost, remotePort);
				
				if (serverAddress.getAddress() == null)
				{
					throw new IllegalArgumentException();
				} 
			}
			catch (Exception e)
			{
				handleError(Messages.getString("INVALID_SERVER_ADDRESS")); //$NON-NLS-1$
				exit();
				return;
			}
			if (shutdown)
				return;
				
			//System.out.println("3");
			if (!portSet)
			{
				port = Radio.getBestPortNumber();
			}
			//System.out.println("Port: " + port);
			/*
			if (!maxUploadBandwidthSet)
			{
				maxUploadBandwidth = inputMaxUploadBandwidth();
			}
			*/
			try
			{
				peer = new Peer(new RemotePeer(serverAddress), port, ui);
				// peer = new Peer(new RemotePeer(serverAddress), port, maxUploadBandwidth);
			}
			catch (PeerException e)
			{
				handleError(e.getMessage());
				return;
			}
			Logger.finer("RadioGUI", "I_AM_A_CLIENT", peer); //$NON-NLS-1$ //$NON-NLS-2$
		}
		if (shutdown)
			return;
		httpPlayer = new HttpPlayer(peer);
		httpPlayer.start();
		if (shutdown)
			return;
		
		webServer = new WebServer(peer, httpPlayer);
		webServer.start();
		ui.setPeer(peer);

		// Media Player nur automatisch starten, falls der Peer
		// kein Server ist
		if (xml.startMediaPlayerAtStartup() && !xml.getUseIntegratedPlayer())
		{
				new MediaPlayerLauncher(peer.getListenBuffer(), httpPlayer).start();
		}				
		
	}
	public HttpPlayer getHttpPlayer()
	{
		return httpPlayer;
	}
	public void shutdown()
	{
		shutdown = true;
		
		if (webServer != null)
			webServer.shutdown();
		if (player != null)
			try {
				player.stop();
			} catch (BasicPlayerException e) {
			}
		if (httpPlayer != null)
			httpPlayer.shutdown();
		if (peer != null)
			peer.shutdown();
	}
	public void launchIntegratedPlayer()
	{
		/*
		while(ui.getMetadata() == null)
		{
			try {
				wait(100);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}*/
		String contentType = ui.getMetadata().getContentType();
		if (contentType.equals("application/ogg") || contentType.equals("audio/mpeg"))
		{
			if (player == null)
			{
				player = new BasicPlayer();
				String url = "http://127.0.0.1:" + (port + Peer.PLAYER_PORT_OFFSET);
				try {
					player.open(new URL(url));
					player.play();
					player.setGain(ui.getGain());
					//gain 0, min -80 max 13.9794
					System.out.println("Gain:" + player.getGainValue() + "Min:" + player.getMinimumGain() + "Max:" + player.getMaximumGain());
				} catch (MalformedURLException e) {
				} catch (BasicPlayerException e) {
				}
			}
			else
			{
				try {
					player.play();
				} catch (BasicPlayerException e) {
				}
			}
			Logger.info("Peer", "Peer.INTEGRATED_MEDIA_PLAYER_LAUNCHED");
		}
	}
	public BasicPlayer getIntegratedPlayer()
	{
		return player;
	}
	public void setPlayerGain(double gain)
	{
		if (player != null)
		{
			try {
				player.setGain(gain);
			} catch (BasicPlayerException e) {
			}
		}
	}
	
}

⌨️ 快捷键说明

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