lanpages.java

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

JAVA
99
字号
/* Stream-2-Stream - Peer to peer television and radio
 * Project homepage: http://s2s.sourceforge.net/
 * Copyright (C) 2005-2006 Jason Hooks
 * ---------------------------------------------------------------------------
 * 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 stream2stream.stationlist;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import p2pradio.Metadata;
import p2pradio.Radio;
import p2pradio.sources.BroadcastBuffer;
import stream2stream.network.MessageDispatcher;
import stream2stream.network.Peer;

public class LANPages extends Thread {
	private MulticastSocket socket; 
																	   //224.0.0.3 is unassigned
	public static final String MULTICAST_ADDRESS = "224.0.1.179"; //"224.0.0.3";  //224.0.0.0 - 224.0.0.255  (224.0.0/24) Local Network Control Block 
	public static final int MULTICAST_PORT = 30000;
	public static final int TIME_TO_WAIT = 100; //100 ms 
	public static final int FIRST_TIME_TO_WAIT = 1000;  //1 second
	private BroadcastBuffer buffer;
	//private boolean metadataHasChanged;
	private Metadata metadata;
	private InetAddress group;
	private Peer peer;
	
	public LANPages(BroadcastBuffer buffer)
	{
		//metadataHasChanged = true;
		metadata = buffer.getMetadata();
		this.buffer = buffer;
		peer = buffer.getPeer();
	}
	public void run()
	{
		try {
			sleep(FIRST_TIME_TO_WAIT);
		} catch (InterruptedException e1) {
		}
		try {
			group = InetAddress.getByName(MULTICAST_ADDRESS);
			socket = new MulticastSocket();
			//if(buffer.getPeer().getXML().This_Server_Uses_Verified_Bandwidth())
			socket.joinGroup(group);
		} catch (SocketException e) {
		}
		catch (IOException e) {
		}
		String addr = null;
		try {
			addr = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e1) {
		}
		String contentTransport = Radio.getContentTransport(metadata, peer);
		while (true)
		{ 	
			metadata = buffer.getMetadata();
			String toSend = "\\n" + metadata.getStationName() + "\\s" + metadata.getSongTitle() 
			+ "\\t"+ contentTransport + "\\g" + metadata.getGenre() +
			"\\r" + ((metadata.getAverageByterate() / 1024) * 8) + "\\l" + MessageDispatcher.acceptedPeers.size() + "\\u" + addr + ":"  + buffer.getPeer().getPort();
			DatagramPacket packet = new DatagramPacket(toSend.getBytes(), toSend.getBytes().length, group, MULTICAST_PORT);
			try {
				socket.send(packet);
			} catch (IOException e) {
			}
			try {
				sleep(TIME_TO_WAIT);
			} catch (InterruptedException e) {
			}
			
		}
	}
}

⌨️ 快捷键说明

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