This commit is contained in:
carry 2023-07-10 13:42:42 +08:00
commit 810d5954b8
9 changed files with 284 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea
out
fit.carry.bridge/fit.carry.bridge.iml
bridge.iml

View File

@ -0,0 +1,60 @@
import java.util.ArrayList;
import java.util.HashMap;
import static java.lang.Thread.sleep;
public class Bridge implements Runnable{
private HashMap<Integer,ArrayList<Frame>> forwardTable;
private ArrayList<ArrayList<Frame>> subNetList;
public Bridge(ArrayList<ArrayList<Frame>> subNetList, ArrayList<Frame> postList) {
this.subNetList = subNetList;
this.postList = postList;
forwardTable = new HashMap<>();
}
private ArrayList<Frame> postList;
@Override
public void run() {
try {
ArrayList<Frame> target = null;
Frame seeker = null;
for (Frame toPost : postList) {
target = forwardTable.get(toPost.getDes());
if (null != target) {
System.out.println("找到"+toPost.getDes()+"的网段,已发送");
target.add(toPost);
} else {
seeker = new Frame(0, toPost.getDes(), "SEEK");
for (ArrayList<Frame> subNet : subNetList) {
synchronized (subNet) {
subNet.add(seeker);
}
}
sleep(5);
out:
for (ArrayList<Frame> subNet : subNetList) {
synchronized (subNet) {
for (Frame frame : subNet) {
if (frame.getDes() == 0) {
forwardTable.put(toPost.getDes(), subNet);
System.out.println("已找到" + toPost.getDes() + "的网段,已记入转发表");
subNet.add(toPost);
subNet.remove(frame);
break out;
}
}
}
}
}
}
}
catch (Exception e){
}
}
}

View File

@ -0,0 +1,3 @@
public class ForwardTable {
}

View File

@ -0,0 +1,43 @@
public class Frame {
private int src;
private int des;
public int getRtt() {
return rtt;
}
private int rtt;
private String data;
public int getSrc() {
return src;
}
public void setSrc(int src) {
this.src = src;
}
public int getDes() {
return des;
}
public void setDes(int des) {
this.des = des;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Frame(){
}
public Frame(int src, int des, String data) {
this.src = src;
this.des = des;
this.data = data;
this.rtt = 0;
}
}

View File

@ -0,0 +1,53 @@
import java.util.ArrayList;
import static java.lang.Thread.sleep;
public class Host implements Runnable {
public Host(int addr, ArrayList<Frame> subNet) {
this.addr = addr;
this.subNet = subNet;
}
@Override
public void run() {
while(true){
try {
synchronized (subNet){
if(subNet.size() != 0) {
for (Frame each : subNet) {
if (each.getDes() == this.addr) {
System.out.println("主机" + addr + "收到:" + each.getData());
if(each.getData().equals("SEEK")){
subNet.add(new Frame(addr,0,"REPLY"));
System.out.println("网桥广播查找,以向网桥汇报");
}
subNet.remove(each);
}
}
}
}
sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public int getAddr() {
return addr;
}
public void setAddr(int addr) {
this.addr = addr;
}
public ArrayList<Frame> getSubNet() {
return subNet;
}
public void setSubNet(ArrayList<Frame> subNet) {
this.subNet = subNet;
}
private int addr;
private ArrayList<Frame> subNet;
}

View File

@ -0,0 +1,90 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static ArrayList<Frame> readFrame(String fileName1,String fileName2){
ArrayList<Frame> frameList = new ArrayList<>();
ArrayList<Frame> frameList1 = new ArrayList<>();
ArrayList<Frame> frameList2 = new ArrayList<>();
// 读取文件中的数据
try (BufferedReader br = new BufferedReader(new FileReader(fileName1))) {
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
int src = Integer.parseInt(tokens[0]);
int des = Integer.parseInt(tokens[1]);
String data = tokens[2];
frameList1.add(new Frame(src, des, data)); // 将读取的数据存入列表中
}
} catch (IOException e) {
e.printStackTrace();
}
// 读取文件中的数据
try (BufferedReader br = new BufferedReader(new FileReader(fileName2))) {
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
int src = Integer.parseInt(tokens[0]);
int des = Integer.parseInt(tokens[1]);
String data = tokens[2];
frameList2.add(new Frame(src, des, data)); // 将读取的数据存入列表中
}
} catch (IOException e) {
e.printStackTrace();
}
int i = 0;
int j = 0;
while (i < frameList1.size() && j < frameList2.size()) {
frameList.add(frameList1.get(i));
frameList.add(frameList2.get(j));
i++;
j++;
}
// 将list1或list2中可能剩余的元素加入到interleavedList中
while (i < frameList1.size()) {
frameList.add(frameList1.get(i));
i++;
}
while (j < frameList2.size()) {
frameList.add(frameList2.get(j));
j++;
}
return frameList;
}
public static void main(String[] args){
ArrayList<Frame> frameList = readFrame("frame1.txt","frame2.txt");
ArrayList<Frame> subNet1 = new ArrayList<>();
ArrayList<Frame> subNet2 = new ArrayList<>();
ArrayList<ArrayList<Frame>> subNetList = new ArrayList<>(Arrays.asList(subNet1,subNet2));
Thread host = null;
ArrayList<Thread> hostList = new ArrayList<>();
for(int i = 1;i <= 3; i++){
host = new Thread(new Host(i,subNet1));
host.setDaemon(true);
hostList.add(host);
}
for(int i = 4;i <=6; i++){
host = new Thread(new Host(i,subNet2));
host.setDaemon(true);
hostList.add(host);
}
for(Thread each : hostList){
each.start();
}
Thread bridge = new Thread(new Bridge(subNetList,frameList));
bridge.start();
}
}

View File

@ -0,0 +1,25 @@
import java.util.ArrayList;
public class map{
private int addr;
private ArrayList<Frame> net;
public map(int addr, ArrayList<Frame> net) {
this.addr = addr;
this.net = net;
}
public int getAddr() {
return addr;
}
public void setAddr(int addr) {
this.addr = addr;
}
public ArrayList<Frame> getNet() {
return net;
}
public void setNet(ArrayList<Frame> net) {
this.net = net;
}
}

3
frame1.txt Normal file
View File

@ -0,0 +1,3 @@
2 6 asd
1 6 fdasf
3 4 asdf

3
frame2.txt Normal file
View File

@ -0,0 +1,3 @@
5 2 jsadf
5 1 sadf
4 3 sda