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
三季人G
- 粉丝: 139
- 资源: 2369
最新资源
- 基于JAVA的社团管理系统的设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于spring boot的小型诊疗预约平台的设计与开发-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 引入注意力机制的resnet鸟类识别
- 毕业设计A048-基于Java的抗疫物资管理系统的设计与实现
- 433JSP基于SpringMVC+Hibernate自动补货商品采购系统毕业课程源码设计+论文资料
- Java毕业设计图书推荐系统源码+数据库(高分毕业设计项目).zip
- MATLAB仿真轴棱锥产生贝塞尔高斯光束 以及环形光束光强图像
- B双馈风电机组仿真,matlab simulink仿真源文件,有详细报告说明书,双馈风力发电机仿真,风力发电机仿真,风力发电控制系统仿真
- 基于YOLOv8的红领巾目标检测项目源码+模型文件+文档说明(高分项目)
- SpringBoot+Vue测评系统及部署教程
- gige vision协议工业相机采图及存储实现,gvcp相机控制通道实现,gvsp流通道实现,千兆以太网实现以及ddr3控制器实现 代码已经用于实际工程,纯Verilog开发实现
- Qt实现使用QAxObject在word中实现文本替换和插入图片
- ABS模型 汽车制动防抱死模型ABS模型 基于MATLAB Simulink搭建电动汽车直线abs模型,相关模型文件可为初学者提供便利,有详细的建模过程,有Word说明文件
- (嵌入式毕设)实验室环境检测-主要是多个传感器组成的检测小项目,主要检测多种气体,当出现气体超标等危险情况是,进行实时报警与通风
- 基于高频HF注入的永磁同步电机PMSM 无速度传感器矢量控制仿真模型 Matlab Simulink仿真(成品)
- TEE:多CA调用同一个TA的迸发场景
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈