📄 player.java
字号:
package com.grail.comm.core;
import java.util.*;
/**
* <p>Title: 短信 项目</p>
*
* <p>Description:协议层</p>
*
* <p>Copyright: Copyright (c) 2004 合力阳光</p>
*
* <p>Company: 合力阳光</p>
* @author ray/刘有为
* @version 1.0
*/
public abstract class PLayer
{
public static final int maxId = 0x3b9aca00;//最大ID数
protected int id;//标识id
protected int nextChildId;//下一个信息的id号
protected PLayer parent;//层的根源
private HashMap children;//用于存放子节点
private List listeners;//存放监听
/**
* <p>构造方法</p>
* @param theParent
*/
protected PLayer(PLayer theParent)
{
if(theParent != null)
{
id = ++theParent.nextChildId;
if(theParent.nextChildId >= 0x3b9aca00)
theParent.nextChildId = 0;
if(theParent.children == null)
theParent.children = new HashMap();
theParent.children.put(new Integer(id), this);
parent = theParent;
}
}
/**
* <p>发送短消息</p>
* @param pmessage 存储短消息的对象
* @throws PException 协议异常
*/
public abstract void send(PMessage pmessage)
throws PException;
/**
* <p>接收短消息</p>
* @param message
*/
public void onReceive(PMessage message)
{
int childId = getChildId(message);
if(childId == -1)
{
System.out.println("++++++++ jie shuo =====");
PLayer child = createChild();
child.onReceive(message);
fireEvent(new PEvent(2, this, child));
} else
{
PLayer child = (PLayer)children.get(new Integer(getChildId(message)));
if(child == null)
fireEvent(new PEvent(64, this, message));
else
child.onReceive(message);
}
}
/**
* <p>返回当前的协议层</p>
* @return PLayer
*/
public PLayer getParent()
{
return parent;
}
/**
* <p>返回协议层的个数</p>
* @return int
*/
public int getChildNumber()
{
if(children == null)
return 0;
else
return children.size();
}
/**
* <p>创建一个协议层</p>
* @return PLayer
*/
protected PLayer createChild()
{
throw new UnsupportedOperationException("Not implement");
}
/**
* <p>通过协议消息返回id</p>
* @param message 协议消息
* @return int
*/
protected int getChildId(PMessage message)
{
throw new UnsupportedOperationException("Not implement");
}
/**
* <p>清除方法,用于清除协议层</p>
*/
public void close()
{
if(parent == null)
{
throw new UnsupportedOperationException("Not implement");
} else
{
parent.children.remove(new Integer(id));
return;
}
}
/**
* <p>添加监听器</p>
* @param l
*/
public void addEventListener(PEventListener l)
{
if(listeners == null)
listeners = new ArrayList();
listeners.add(l);
}
/**
* <p>删除监听器</p>
* @param l
*/
public void removeEventListener(PEventListener l)
{
listeners.remove(l);
}
/**
* <p>点火事件方法</p>
* @param e 协议事件
*/
protected void fireEvent(PEvent e)
{
if(listeners == null)
return;
for(Iterator i = listeners.iterator(); i.hasNext(); ((PEventListener)i.next()).handle(e));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -