📄 portgate.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* 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 edu.tsinghua.lumaqq.qq.net;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
/*
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.packets.OutPacket;
import edu.tsinghua.lumaqq.qq.packets.PacketHistory;
*/
/**
* 管理所有IPort对象,一个QQClient一个Porter,一个Porter上面可以有
* 多个IPort,PortGate用来集中管理这些IPort。一个Port代表了一个连接,
* 这个连接可能被多个地方使用,所以port有一个引用计数,计数为0时才真正释放
* port
*
* @author luma
*/
public class PortGate {
// IPort映射器,key是名称,value是IPort对象
private Map<String, IPort> registry;
// Porter
private Porter porter;
// packet monitor
private PacketHistory monitor;
/**
* 创建
*/
public PortGate(QQClient client, Porter porter) {
this.porter = porter;
registry = new HashMap<String, IPort>();
monitor = client.getHistory();
}
/**
* 检查是否存在指定名称的port
*
* @param name
* port name
* @return
* true表示存在这个port
*/
public boolean hasPort(String name) {
return registry.containsKey(name);
}
/**
* 关闭所有Port
*
* @param porter
* 这些port从属的Porter
*/
public void disposeAll() {
Map<String, IPort> temp = new HashMap<String, IPort>();
temp.putAll(registry);
for(String portName : temp.keySet())
disposePort(portName);
registry.clear();
}
/**
* 关闭一个port
*
* @param name
* port name
* @param porter
* port从属的porter
*/
public void disposePort(String name) {
if(porter != null) {
IPort port = getPort(name);
if(port != null) {
port.decreaseReference();
if(port.getReference() <= 0) {
removePort(name);
porter.addDisposeRequest(port);
}
}
}
}
/**
* 检查是否存在一个到指定远程地址的连接
*
* @param address
* @return
*/
public IPort getPort(InetSocketAddress address) {
for(IPort port : registry.values()) {
if(port.getRemoteAddress().equals(address))
return port;
}
return null;
}
/**
* 得到IPort
*
* @param name
* port name
* @return
* IPort对象,没有对应的port返回null
*/
public IPort getPort(String name) {
return registry.get(name);
}
/**
* 清空包监视缓冲区
*/
public void clearMonitorCache() {
monitor.clear();
}
/**
* 清空某个port的发送队列
*
* @param name
*/
public void clearSendQueue(String name) {
IPort port = getPort(name);
if(port != null)
port.clear();
}
/**
* 使用指定的port发送一个包
*
* @param name
* port name
* @param packet
* OutPacket子类
* @param addMonitor
* true表示需要把这个包推入PacketMonitor
* @return
* true表示包发送成功,false表示失败
*/
public boolean send(String name, OutPacket packet, boolean addMonitor) {
IPort port = getPort(name);
if(port != null) {
if(addMonitor)
monitor.putRequest(packet);
port.add(packet);
return true;
} else
return false;
}
/**
* 删除一个IPort
*
* @param name
* port的名字
* @return
* 被删除的IPort,没有对应的port返回null
*/
public IPort removePort(String name) {
return registry.remove(name);
}
/**
* 添加一个port到注册表中,并不会注册到porter中
*
* @param port
* IPort对象
*/
public void addPort(IPort port) {
registry.put(port.getName(), port);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -