acceptpacket.java

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

JAVA
206
字号
/* 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 p2pradio.packets;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import p2pradio.Metadata;

import stream2stream.XML.SettingsXML;

public class AcceptPacket extends TimePacket {
	private byte info;
	//1     1      1             1
	//8m+   4udp   2verified     1serverVerified
	private int streamMode;
	private boolean verifiedBandwidth;
	private boolean serverUsesVerifiedBandwidth;
	
	private int byterate;
	private InetSocketAddress multicastAddress;
	private String contentType;         
	private String stationName;   
	private String stationUrl; 
	private String stationGenre;
	private String stationDescription;
	private String httpHeaders;
	private String songTitle;
	private String songURL;
	private byte ttl;
	
	public AcceptPacket(int streamMode, boolean usesVerified, boolean serverUsesVerified, long timeStamp, Metadata metadata)
	{
		this(streamMode, usesVerified, serverUsesVerified, null, 0, timeStamp, metadata);
	}
	
	public AcceptPacket(int streamMode, boolean usesVerified, boolean serverUsesVerified, 
			InetSocketAddress multicastAddress, int ttl, long timeStamp, Metadata metadata)
	{
		this (Packet.ACCEPT, new byte[getMaxLength()], streamMode, usesVerified, serverUsesVerified, multicastAddress, ttl, timeStamp, metadata);
	}
	public AcceptPacket(byte packetType, byte content[], int streamMode, boolean usesVerified, 
			boolean serverUsesVerified, InetSocketAddress multicastAddress, int ttl, long timeStamp, Metadata metadata)
	{
		super(packetType, content, timeStamp);
		info = 0x0;
		if (serverUsesVerified)  //Server uses verified
			info += 1;
		if (usesVerified)        //Requestor uses verified
			info += 2;
		if (streamMode == SettingsXML.UDP)
			info += 4;
		if (streamMode == SettingsXML.MULTICAST_PLUS)
			info += 8;
		this.serverUsesVerifiedBandwidth = serverUsesVerified;
		this.verifiedBandwidth = usesVerified;
		this.streamMode = streamMode;
		this.multicastAddress = multicastAddress;
		writeByte(info);
		if (multicastAddress != null)
		{
			this.ttl = (byte)(0x0 + ttl);
			writeByte(this.ttl);
			writeInt(this.multicastAddress.getPort());
			writeBytes(this.multicastAddress.getAddress().getAddress());
		}
		contentType = metadata.getContentType();
		stationName = metadata.getStationName();
		stationUrl = metadata.getStationURL();
		stationGenre = metadata.getGenre();
		stationDescription = metadata.getDescription();
		httpHeaders = metadata.getHttpHeaders();
		songTitle = metadata.getSongTitle();
		songURL = metadata.getSongURL();
		byterate = metadata.getByterate();
		
		writeByteArray(contentType.getBytes());
		writeByteArray(stationName.getBytes());
		writeByteArray(stationUrl.getBytes());
		writeByteArray(stationGenre.getBytes());
		writeByteArray(stationDescription.getBytes());
		writeByteArray(httpHeaders.getBytes());
		writeByteArray(songTitle.getBytes());
		writeByteArray(songURL.getBytes());
		writeInt(byterate);
	}
	public AcceptPacket(byte content[], int contentLength)
	{
		super(content, contentLength);
		info = readByte();
		if (info - 8 >= 0x0)
		{
			info -= 8;
			streamMode = SettingsXML.MULTICAST_PLUS;
			byte ttl = readByte();
			int port = readInt();
			byte[] address = readBytes(4);
			try {
				this.multicastAddress = new InetSocketAddress(InetAddress.getByAddress(address), port);
			} catch (UnknownHostException e) {
			}
		}
		else if (info - 4 >= 0x0)
		{
			info -= 4;
			streamMode = SettingsXML.UDP;
		}
		else
			streamMode = SettingsXML.TCP;
		
		verifiedBandwidth = (info - 2 >= 0x0); //We use verified
		if (verifiedBandwidth)
			info -= 2;
		serverUsesVerifiedBandwidth = (info - 1 >= 0x0);
		contentType = new String(readByteArray());
		stationName = new String(readByteArray());
		stationUrl = new String(readByteArray());
		stationGenre = new String(readByteArray());
		stationDescription = new String(readByteArray());
		httpHeaders = new String(readByteArray());
		songTitle = new String(readByteArray());
		songURL = new String(readByteArray());
		byterate = readInt();
	}
	protected static int getMaxLength()
	{
		return 1000 + TimePacket.getMaxLength();
	}
	public int getStreamMode()
	{
		return streamMode;
	}
	public boolean getUseVerified()
	{
		return verifiedBandwidth; //We use verified
	}
	public boolean getServerUsesVerified()
	{
		return serverUsesVerifiedBandwidth;
	}
	public InetSocketAddress getMulticastAddress()
	{
		return multicastAddress;
	}
	public String getContentType()
	{
		return contentType;
	}
	public String getStationName()
	{
		return stationName;
	}
	public String getStationUrl()
	{
		return stationUrl;
	}
	public String getStationGenre()
	{
		return stationGenre;
	}
	public String getStationDescription()
	{
		return stationDescription;
	}
	public String getHttpHeaders()
	{
		return httpHeaders;
	}
	public String getSongTitle()
	{
		return songTitle;
	}
	public String getSongURL()
	{
		return songURL;
	}
	public int getByterate()
	{
		return byterate;
	}
	public int getTTL()
	{
		return (int) (0 + ttl);
	}
}

⌨️ 快捷键说明

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