package com.minxing.graduate.manager;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import com.minxing.graduate.model.EthernetFrame;
import com.minxing.graduate.model.IPDatagram;
import com.minxing.graduate.model.IPv4;
import com.minxing.graduate.model.MacAddress;
import com.minxing.graduate.model.Port;
import com.minxing.graduate.model.Router;
import com.minxing.graduate.util.VRMUtil;
// maintain a list of ports
public class PortManager {
// hash map
ConcurrentHashMap<Integer, Port> VRPorts;
// max ports
int maxPorts;
/*----------------------------------------------------------------------------------------*/
// constructor
// create hash map w/ initial capacity 16 and load factor 0.75
public PortManager(int maxPorts) {
VRPorts = new ConcurrentHashMap<Integer, Port>(16, (float) 0.75);
this.maxPorts = maxPorts;
}
/*----------------------------------------------------------------------------------------*/
// add port
public String addPort(int portNo, String myIP, int mtu) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
if (VRPorts.size() == maxPorts) {
builder.append(("Maximum ports: " + maxPorts));
builder.append(cr);
return builder.toString();
}
if (existsPort(portNo)) {
builder.append(("port " + portNo + " exists"));
builder.append(cr);
} else {
try {
Port p = new Port(portNo, myIP, mtu, builder);
VRPorts.put(portNo, p);
} catch (SocketException e) {
builder.append(("PortAdmin: add port " + portNo + " failed"));
builder.append(cr);
e.printStackTrace();
} catch (UnknownHostException e) {
builder.append(("PortAdmin: add port " + portNo + " failed"));
builder.append(cr);
e.printStackTrace();
}
}
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// delete port and call garbage collector
public String removePort(int portNo) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
if (existsPort(portNo)) {
disconnect(portNo);
VRPorts.get(portNo).datagramSocket.close();
VRPorts.remove(portNo);
builder.append(("Successful port " + portNo + " deleted"));
builder.append(cr);
try {
finalize();
} catch (Throwable e) {
builder.append(("PortAdmin: errror finalizing"));
builder.append(cr);
e.printStackTrace();
}
} else {
builder.append(("port " + portNo + " does not exist"));
builder.append(cr);
}
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// remove all ports
public String removeAll() {
StringBuilder builder = new StringBuilder();
Enumeration<Integer> keys = VRPorts.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
builder.append(removePort((Integer) key));
}
try {
finalize();
} catch (Throwable e) {
e.printStackTrace();
}
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// match ip and return port number
public int getPort(byte[] ipArray) {
Enumeration<Integer> keys = VRPorts.keys();
byte[] ipNetworkID = new byte[4];
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
byte[] subnet = VRPorts.get((Integer) key).getSubnet(); // get the
// ports
// subnet
byte[] portNetID = VRPorts.get((Integer) key).getNetworkID();// get
// the
// ports
// network
for (int i = 0; i < 4; i++)
ipNetworkID[i] = (byte) (ipArray[i] & subnet[i]); // turn
// ipArray
// into
// network
// id
if (Arrays.equals(portNetID, ipNetworkID))
return (VRPorts.get((Integer) key).getPortNum());
}
return (-1);
}/*----------------------------------------------------------------------------------------*/
public String connect(int portNo, String connectStr) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
if (existsPort(portNo)) {
return VRPorts.get(portNo).connect(connectStr);
} else
builder.append(("port " + portNo + " does not exists"));
builder.append(cr);
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
public String disconnect(int portNo) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
if (existsPort(portNo)) {
return VRPorts.get(portNo).disconnect();
} else
builder.append(("port " + portNo + " does not exists"));
builder.append(cr);
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// disconnect all ports
public String disconnectAll() {
StringBuilder builder = new StringBuilder();
Enumeration<Integer> keys = VRPorts.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
builder.append(VRPorts.get(key).disconnect());
}
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
private boolean existsPort(int portNo) {
return (VRPorts.containsKey(portNo));
}
/*----------------------------------------------------------------------------------------*/
// send string in UDP
public <syncronized> String usend(int portNo, byte[] data) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
if (existsPort(portNo)) {
try {
builder.append(VRPorts.get(portNo).send(data));
// System.out.println(data);
} catch (IOException e) {
builder.append(("PortAdmin: error sending data on port " + portNo));
builder.append(cr);
e.printStackTrace();
}
} else
builder.append(("port " + portNo + " does not exists"));
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// send string in UDP
public String asend(byte[] data) {
String cr = System.getProperty("os.name").matches("(W|w)indows.*") ? "\r\n" : "\n";
StringBuilder builder = new StringBuilder();
Enumeration<Integer> keys = VRPorts.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
try {
builder.append(VRPorts.get(key).send(data));
} catch (IOException e) {
builder.append(("PortAdmin: error sending data on port " + key));
builder.append(cr);
e.printStackTrace();
}
}
return builder.toString();
}
/*----------------------------------------------------------------------------------------*/
// return port configuration as a string
public String getAllPortsConfig() {
String s = "";
Enumeration<Integer> keys = VRPorts.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
s += VRPorts.get(key).getSettings();
}
return s;
}
/*----------------------------------------------------------------------------------------*/
// route an Ethernet frame with an IP datagram test packed
public <syncronized> String sendTestPacket(IPv4 srcIP, IPv4 dstIP, short id, int sampleLengt
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
(new)VirtualRouter-master.zip (39个子文件)
VirtualRouter-master
.classpath 295B
.settings
org.eclipse.jdt.core.prefs 587B
com.genuitec.eclipse.core.prefs 167B
src
com
minxing
graduate
thread
Listener.java 2KB
ClientWorker.java 2KB
command
ExitHandler.java 413B
SendHandler.java 1KB
ConnectHandler.java 1KB
TrouteHandler.java 764B
RouteHandler.java 1KB
CommandHandler.java 138B
UnknownCommandHandler.java 2KB
IncludeHandler.java 5KB
PortHandler.java 1KB
ASendHandler.java 696B
ConfigHandler.java 1KB
CommandHandlerFactory.java 1KB
USendHandler.java 733B
manager
PortManager.java 11KB
ConfigurationManager.java 2KB
model
Router.java 664B
EthernetFrame.java 5KB
IPv4.java 5KB
Port.java 7KB
RoutingTable.java 8KB
MacAddress.java 7KB
IPDatagram.java 7KB
ui
MainInterface.java 2KB
ServerLauncher.java 436B
TelnetServer.java 2KB
util
VRMUtil.java 4KB
Constants.java 632B
FileReader.java 737B
default 37B
server.properties 98B
kill 37KB
.project 367B
.gitignore 176B
setup 863B
共 39 条
- 1
资源评论
三季人G
- 粉丝: 136
- 资源: 2369
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功