Merge remote-tracking branch 'origin/master'

This commit is contained in:
ChessNineeee
2021-04-15 23:05:02 +08:00
16 changed files with 410 additions and 28 deletions
@@ -1,4 +1,4 @@
package logicface;
package logicface.CallbackInterface;
import packet.Data;
import packet.Interest;
@@ -10,6 +10,6 @@ import packet.Interest;
* @Date: 10:21 2021/4/15
* @Copyright: MIN-Group国家重大科技基础设施未来网络北大实验室深圳市信息论与未来网络重点实验室
*/
public interface OnData {
public interface OnDataInterface {
void onData(Interest interest, Data data);
}
@@ -1,4 +1,4 @@
package logicface;
package logicface.CallbackInterface;
import packet.Interest;
@@ -9,6 +9,6 @@ import packet.Interest;
* @Date: 10:22 2021/4/15
* @Copyright: MIN-Group国家重大科技基础设施未来网络北大实验室深圳市信息论与未来网络重点实验室
*/
public interface OnInterest {
public interface OnInterestInterface {
void onInterest(Interest interest);
}
}
@@ -1,4 +1,4 @@
package logicface;
package logicface.CallbackInterface;
import packet.Interest;
import packet.Nack;
@@ -10,6 +10,6 @@ import packet.Nack;
* @Date: 10:23 2021/4/15
* @Copyright: MIN-Group国家重大科技基础设施未来网络北大实验室深圳市信息论与未来网络重点实验室
*/
public interface OnNack {
public interface OnNackInterface {
void onNack(Interest interest, Nack nack);
}
@@ -0,0 +1,14 @@
package logicface.CallbackInterface;
import component.Identifier;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:11 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public interface OnRegisterFailInterface {
void onRegisterFail(Identifier identifier);
}
@@ -0,0 +1,14 @@
package logicface.CallbackInterface;
import component.Identifier;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:12 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public interface OnRegisterSuccessInterface {
void onRegisterSuccess(Identifier identifier);
}
@@ -1,4 +1,4 @@
package logicface;
package logicface.CallbackInterface;
import packet.Interest;
@@ -9,6 +9,6 @@ import packet.Interest;
* @Date: 10:22 2021/4/15
* @Copyright: MIN-Group国家重大科技基础设施未来网络北大实验室深圳市信息论与未来网络重点实验室
*/
public interface OnTimeout {
public interface OnTimeoutInterface {
void onTimeOut(Interest interest);
}
+1 -1
View File
@@ -9,7 +9,7 @@ import packet.LpPacket;
* @Date: 19:44 2021/4/13
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public interface ITransport {
public interface ITransport{
/**
* 关闭
*/
+56 -13
View File
@@ -5,8 +5,8 @@ import component.Identifier;
import component.IdentifierWrapper;
import encoding.TLV;
import encoding.VlIntException;
import logicface.CallbackInterface.*;
import packet.*;
import util.CallbackFunction;
import util.ConcurrentHelper;
import util.TimeHelper;
@@ -29,19 +29,19 @@ public class LogicFaceICN extends LogicFace {
public Lock timeoutEventHeapLock; // 锁, mpit 、timeoutEventHeap、recentExpireTime
public TimeoutEventHeap timeoutEventHeap; // 超时事件堆, 以超时时间排序的最小堆
public Map<String, OnInterest> mFib;
public Map<String, OnInterestInterface> mFib;
/**
* 通过最长匹配原则查找FIB表,找到合适的兴趣包处理函数
* @param identifier
* @return
*/
public OnInterest lookUpFib(Identifier identifier) throws LogicFaceException {
public OnInterestInterface lookUpFib(Identifier identifier) throws LogicFaceException {
try {
int idComponentN = identifier.getComponents().length();
for (int i = idComponentN; i > 0; i--) {
Identifier prefix = identifier.getPrefix(i);
OnInterest fibEntry=this.mFib.get(prefix.toUri());
OnInterestInterface fibEntry=this.mFib.get(prefix.toUri());
if(fibEntry!=null) {
return fibEntry;
}
@@ -68,7 +68,7 @@ public class LogicFaceICN extends LogicFace {
}
IdentifierWrapper identifierWrapper=interest.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry=this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
OnInterest oI=this.lookUpFib(identifierWrapper.getIdentifier());
OnInterestInterface oI=this.lookUpFib(identifierWrapper.getIdentifier());
if(oI!=null){
oI.onInterest(interest);
}
@@ -111,7 +111,7 @@ public class LogicFaceICN extends LogicFace {
if(pitEntry==null){
return;
}
pitEntry.oD.onData(pitEntry.interest,data);
pitEntry.onDataInterface.onData(pitEntry.interest,data);
} catch (PacketException | ComponentException e) {
throw new LogicFaceException("LogicFaceICN.onReceiveData: "+e.getMessage());
}
@@ -133,7 +133,7 @@ public class LogicFaceICN extends LogicFace {
if(pitEntry==null){
return;
}
pitEntry.oN.onNack(pitEntry.interest,nack);
pitEntry.onNackInterface.onNack(pitEntry.interest,nack);
} catch (ComponentException e) {
throw new LogicFaceException("LogicFaceICN.onReceiveNack: "+e.getMessage());
}
@@ -163,7 +163,7 @@ public class LogicFaceICN extends LogicFace {
PITEntry pitEntry=this.mPit.get(timeEvent.key);
if(pitEntry!=null){
if(pitEntry.expireTime<=currentTime){
pitEntry.oT.onTimeOut(pitEntry.interest);
pitEntry.onTimeoutInterface.onTimeOut(pitEntry.interest);
this.mPit.remove(timeEvent.key);
}else{
timeEvent.timeoutTime=pitEntry.expireTime;
@@ -289,11 +289,13 @@ public class LogicFaceICN extends LogicFace {
/**
* 发送一个兴趣包
* @param interest
* @param oD
* @param oT
* @param oN
* @param onDataInterface
* @param onTimeoutInterface
* @param onNackInterface
*/
public void expressInterest(Interest interest,OnData oD,OnTimeout oT,OnNack oN) throws LogicFaceException {
public void expressInterest(Interest interest,
OnDataInterface onDataInterface, OnTimeoutInterface onTimeoutInterface,
OnNackInterface onNackInterface) throws LogicFaceException {
try {
IdentifierWrapper interestName=interest.minPacket.identifierField.getIdentifier(0);
if(interestName==null){
@@ -302,10 +304,51 @@ public class LogicFaceICN extends LogicFace {
}
PITEntry pitEntry = null;
pitEntry.interest=interest;
// pitEntry.oT;
pitEntry.onDataInterface=onDataInterface;
pitEntry.onTimeoutInterface =onTimeoutInterface;
pitEntry.onNackInterface=onNackInterface;
boolean isRepeate=this.insert2PIT(pitEntry,interestName.toUri());
if(isRepeate){
return;
}
if(!this.sendInterest(interest)){
// todo: 日志记录错误
}
} catch (ComponentException e) {
throw new LogicFaceException("LogicFaceICN.expressInterest: "+e.getMessage());
}
}
/**
* 发出一个数据包
* @param data
* @throws LogicFaceException
*/
public void putData(Data data) throws LogicFaceException {
if(!this.sendData(data)){
// todo: 日志记录错误
}
}
public void registerPrefix(Identifier identifier,OnInterestInterface onInterestInterface,
OnRegisterFailInterface onRegisterFailInterface,
OnRegisterSuccessInterface onRegisterSuccessInterface) throws LogicFaceException {
if(this.mFib==null){
this.mFib=new HashMap<>();
}
boolean res=this.registerIdentifier(identifier,3);
if(!res){
onRegisterFailInterface.onRegisterFail(identifier);
return;
}
try {
this.mFib.put(identifier.toUri(),onInterestInterface);
onRegisterSuccessInterface.onRegisterSuccess(identifier);
} catch (ComponentException e) {
throw new LogicFaceException("LogicFaceICN.registerPrefix: "+e.getMessage());
}
}
}
+6 -5
View File
@@ -1,8 +1,9 @@
package logicface;
import packet.Data;
import logicface.CallbackInterface.OnDataInterface;
import logicface.CallbackInterface.OnNackInterface;
import logicface.CallbackInterface.OnTimeoutInterface;
import packet.Interest;
import packet.Nack;
/*
* @Author: Wang Feng
@@ -14,7 +15,7 @@ import packet.Nack;
public class PITEntry{
public Interest interest = new Interest(); // 兴趣包
public long expireTime = 0; // 本PIT表项的超时时间
OnData oD = null; // 收到对应的数据包的处理函数
OnTimeout oT = null; // 兴趣包超时的处理函数
OnNack oN = null; // 兴趣包无路由的处理函数
OnDataInterface onDataInterface = null; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface = null; // 兴趣包超时的处理函数
OnNackInterface onNackInterface = null; // 兴趣包无路由的处理函数
}
@@ -0,0 +1,27 @@
package logicface;
import packet.LpPacket;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:48 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class StreamTransport extends Transport implements ITransport{
@Override
public boolean send(LpPacket lpPacket) {
return false;
}
@Override
public LpPacket receive() {
return null;
}
@Override
public boolean setReadTimeout(long duration) {
return false;
}
}
+87
View File
@@ -0,0 +1,87 @@
package logicface;
import encoding.*;
import packet.LpPacket;
import packet.PacketException;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:22 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Transport {
public String localAddr;
public String remoteAddr;
public String localUri;
public String remoteUri;
public LinkService linkService;
// TODO 虽然第一个版本可能用不到linkService分包与合包功能,
// 但以后可能需要客户端也能直接发出用以太网封装的MIN网络包,所以还是留着linkService
public void close(){
throw new Error("implement me");
}
/**
* 从[]byte中解析出LpPacket
* @param buf
* @return
*/
public LpPacket parseByteArray2LpPacket(byte[] buf) throws LogicFaceException {
try {
Block block=new Block(buf,true);
if(block.isValid()){
return null;
}
LpPacket lpPacket=new LpPacket();
if(!lpPacket.wireDecode(block)){
return null;
}
return lpPacket;
} catch (BlockException | PacketException e) {
throw new LogicFaceException("Transport.parseByteArray2LpPacket: "+e.getMessage());
}
}
/**
* 将lpPacket编码成byte数组
* @param lpPacket
* @return
*/
public byte[] encodeLpPacket2ByteArray(LpPacket lpPacket) throws LogicFaceException {
try {
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
int encodeBufLen=lpPacket.wireEncode(encoder);
if(encodeBufLen<0){
return null;
}
byte[] encodeBuf=encoder.getBuffer();
if(encodeBuf==null){
return null;
}
return encodeBuf;
} catch (EncoderException | PacketException e) {
throw new LogicFaceException("Transport.encodeLpPacket2ByteArray: "+e.getMessage());
}
}
public String getRemoteUri(){
return this.remoteUri;
}
public String getLocalUri(){
return this.localUri;
}
public String getRemoteAddr(){
return this.remoteAddr;
}
public String getLocalAddr(){
return this.localAddr;
}
}
+31
View File
@@ -0,0 +1,31 @@
package logicface;
import packet.LpPacket;
import java.net.*;
import java.io.*;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:16 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class UdpTransport extends Transport implements ITransport{
@Override
public boolean send(LpPacket lpPacket) {
return false;
}
@Override
public LpPacket receive() {
return null;
}
@Override
public boolean setReadTimeout(long duration) {
return false;
}
}
+48
View File
@@ -0,0 +1,48 @@
package others;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:58 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
import java.io.InputStream;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) {
Socket socket = null;
InputStream inputStream = null;
try {
//建立基站获取链接地址及端口号
socket = new Socket("localhost", 8899);
//获取服务器发过来的字节流
inputStream = socket.getInputStream();
//开始解析字节流
byte[] b = new byte[1024];
String str = "";
int length = -1;
while ((length = inputStream.read(b, 0, b.length)) != -1) {
str += new String(b, 0, length);
}
System.out.println(str);
/*
* int length = inputStream.read(b);
System.out.println(new String(b, 0, length));
*/
} catch (Exception e) {
e.printStackTrace();
} finally {
//最后关闭
try {
inputStream.close();
socket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
+39
View File
@@ -0,0 +1,39 @@
package others;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:57 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) {
ServerSocket socket = null;
OutputStream outputStream = null;
try {
//建立基站
socket = new ServerSocket(8899);
//开始开启接收模式,接到后返回客户端的socket对象
Socket client = socket.accept();
//获取向客户端发送消息的对象流
outputStream = client.getOutputStream();
//向客户端写数据
outputStream.write("你连上了服务器...".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
+43
View File
@@ -0,0 +1,43 @@
package others;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:55 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class UDPReceiveDemo {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(12345);//建立基站
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);//建立机房
socket.receive(packet);//开始接受数据
//获取对方的主机信息
InetAddress address = packet.getAddress();
System.out.println(address.getHostAddress());
//获取数据内容
byte[] data = packet.getData();
System.out.println("数据内容:" + new String(data, 0, packet.getLength()));
//获取数据长度
int length = packet.getLength();
System.out.println("数据长度:" + length);
//获取接收端口号
int port = packet.getPort();
System.out.println("接收端口号是:" + port);
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
}
+35
View File
@@ -0,0 +1,35 @@
package others;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:53 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class UDPSendDemo {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
//建立基站
socket = new DatagramSocket();
byte[] buf = "hello,UDP".getBytes();
InetAddress address;
address = InetAddress.getByName("localhost");
//建立仓库
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 12345);
//发送数据
socket.send(packet);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭基站
socket.close();
}
}
}