私は、ソース、中級、および行き先のクラスを持っています。ソースクラスには、送信する新しいパケットをある確率で受信するメソッドがあります。パケットが受信されるたびに、すべての隣接中間ノードにブロードキャストされる(いくつかのsetPacketメソッドによってシミュレートされる)必要があります。中間ノードは、パケットが宛先に到達するまで、それらのパケットを近隣ノードにブロードキャストする必要があります。問題は、パケット送信のチェーンが開始されるたびに、ソースノードが新しいパケットを取得する方法の実行を停止することです。したがって、基本的には、連鎖が機能している間はソースクラスがフリーズします。これら2つのプロセスを同時に実行する方法はありますか? (それはソースノードが中級クラスにリクエストを送り、それが独立して仕事をするようです)私は3つのクラスのそれぞれをThreadクラスに拡張しようとしましたが、まだ送信チェーンはSourceクラスをフリーズしています。使用される言語はJavaですが、仕事をすることができる他の言語があれば、私はその言語に切り替えることができます。アドホックネットワークノードをシミュレートする方法は?
public class Main {
public static void main(String[] args) {
// init nodes
SourceNode sourceNode = new SourceNode();
IntermediateNode intermediateNode1 = new IntermediateNode();
IntermediateNode intermediateNode2 = new IntermediateNode();
IntermediateNode intermediateNode3 = new IntermediateNode();
DestinationNode destinationNode = new DestinationNode();
// create network topology, S - I - I - I - D
sourceNode.setNextNode(intermediateNode1);
intermediateNode1.setNextNode(intermediateNode2);
intermediateNode2.setNextNode(intermediateNode3);
intermediateNode3.setNextNode(destinationNode);
// setup listeners
sourceNode.setSetupMessageListener(intermediateNode1);
intermediateNode1.setSetupMessageListener(intermediateNode2);
intermediateNode2.setSetupMessageListener(intermediateNode3);
intermediateNode3.setSetupMessageListener(destinationNode);
sourceNode.run();
}
}
public interface SetupMessageListener {
void onNewSetupMessage();
}
public class Node {
protected SetupMessageListener setupMessageListener;
protected Node nextNode;
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
public void setSetupMessageListener(SetupMessageListener setupMessageListener) {
this.setupMessageListener = setupMessageListener;
}
}
import java.util.Random;
public class SourceNode extends Node implements Runnable {
@Override
public void run() {
while(true) {
// simulate generating new setup message with probability 1/10
Random random = new Random();
int rv = random.nextInt(10);
if (rv == 0) {
createNewSetupMessage();
}
}
}
public void createNewSetupMessage() {
System.out.println("New setup message was created in Source Node");
if (setupMessageListener != null) {
setupMessageListener.onNewSetupMessage();
}
}
}
public class IntermediateNode extends Node implements SetupMessageListener {
public static int count = 0;
private int id;
public IntermediateNode() {
id = count++;
}
@Override
public void onNewSetupMessage() {
System.out.println("Intermediate Node " + id + " got notified about setup message");
// pass setup message to next neighbor
setupMessageListener.onNewSetupMessage();
}
}
public class DestinationNode extends Node implements SetupMessageListener {
@Override
public void onNewSetupMessage() {
System.out.println("Destination Node got notified about new setup message");
}
}
そして例の出力は
New setup message was created in Source Node
Intermediate Node 0 got notified about setup message
Intermediate Node 1 got notified about setup message
Intermediate Node 2 got notified about setup message
Destination Node got notified about new setup message
New setup message was created in Source Node
Intermediate Node 0 got notified about setup message
あるしかし、私はそれが
New setup message was created in Source Node
Intermediate Node 0 got notified about setup message
New setup message was created in Source Node
Intermediate Node 1 got notified about setup message
Intermediate Node 0 got notified about setup message
Intermediate Node 2 got notified about setup message
Destination Node got notified about new setup message
あなたのコードを見ることなく、何が間違っているかを判断することは不可能です。 –
コードとサンプル出力の追加 –