package bt;

import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.RemoteDevice;
import javax.microedition.io.StreamConnection;

import Game.Global;

/**
 *每个EndPoint对象对应一个活动的蓝牙连接节点的所有连接及其属性
 * 
 * @author DZF
 *@version 1.0.0
 */
public class EndPoint {

	// 客户端属性，该点对点连接的远端是否是客户端所在的房主
	boolean remoteDeviceIsHost = false;

	// 服务端属性，该点对点连接的远端是否在我的房间里
	boolean inRoom = false;

	byte netPlayerNumber = -1;

	byte netPlayerRole = Global.JACK;

	RemoteDevice remoteDev;

	DeviceClass remoteClass;

	String remoteUrl;

	StreamConnection con;

	int transId = -1;

	Sender sender;

	Reader reader;

	String localName;

	String remoteName;

	BTListener callback;

	NetLayer btnet;

	Vector msgs = new Vector();

	public EndPoint(NetLayer btnet, RemoteDevice rdev, StreamConnection c) {
		this.btnet = btnet;
		remoteDev = rdev;

		try {
			remoteName = rdev.getFriendlyName(false);
		} catch (IOException ex) {
			remoteName = "UnKnown";
		}
		
		localName = btnet.localName;
		callback = btnet.callback;
		con = c;

		sender = new Sender();
		sender.endpt = this;

		reader = new Reader();
		reader.endpt = this;
	}

	public synchronized void putString(byte signal, byte[] information) {

		msgs.addElement(new MsgPacket(signal, information));
		synchronized (sender) {
			sender.notify();
		}
	}

	public synchronized MsgPacket getString() {
		if (msgs.size() > 0) {
			MsgPacket m = (MsgPacket) msgs.firstElement();
			msgs.removeElementAt(0);
			return m;
		} else {
			return null;
		}
	}

	public synchronized boolean peekString() {
		return (msgs.size() > 0);
	}
}
