Skip to content

Commit 6a3ef43

Browse files
author
151ali
committed
flc
1 parent eba04de commit 6a3ef43

File tree

216 files changed

+27083
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

216 files changed

+27083
-0
lines changed

BFT_draft/BFT_draft.cc

+222
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* BFT_draft.cc
3+
*
4+
* Created on: Nov 9, 2020
5+
* Author: lina
6+
*/
7+
8+
/*
9+
* Breadth-First Spanning Tree
10+
* Built Without Centralized Control
11+
*/
12+
13+
#include <string.h>
14+
#include <omnetpp.h>
15+
#include "Snail_m.h"
16+
#include "Set.h"
17+
18+
using namespace omnetpp;
19+
20+
enum {
21+
Token = 0,
22+
Yes = 1,
23+
No = 2
24+
};
25+
26+
class Node : public cSimpleModule {
27+
private:
28+
int my_Id,d,
29+
gates_num;
30+
int level;
31+
int expected_msg;
32+
int parent_Id;
33+
34+
bool have_parent = false;
35+
Set Nx,
36+
children;
37+
38+
protected:
39+
virtual void initialize() override;
40+
virtual void handleMessage(cMessage* msg) override;
41+
42+
43+
44+
};
45+
46+
47+
Define_Module(Node);
48+
49+
void Node::initialize(){
50+
gates_num = gateSize("gate$o");
51+
int initiator = par("initiator").intValue();
52+
my_Id = getIndex();
53+
54+
// initialize N(x)
55+
for (int i = 0; i < gateSize("gate$o"); i++) {
56+
int neighbor = i;//gate("gate$o",i)->getPathEndGate()->getOwnerModule()->getIndex();
57+
Nx.Add(neighbor);
58+
}
59+
60+
Nx.Print();
61+
62+
if(my_Id == initiator){
63+
64+
Snail *msg = new Snail("Message");
65+
msg->setKind(Token);
66+
msg->setDistance(-1);
67+
scheduleAt(simTime() + 1, msg); // a self message !
68+
69+
}
70+
}
71+
72+
void Node::handleMessage(cMessage* msg){
73+
int sender_gate;
74+
Snail *smsg = check_and_cast<Snail*>(msg);
75+
d = smsg->getDistance();
76+
int message_kind = smsg->getKind();
77+
Snail *yes,*no,*tok;
78+
79+
if(msg->isSelfMessage()){
80+
81+
parent_Id = my_Id;
82+
have_parent = true;
83+
level = 0;
84+
85+
if(Nx.Size() != 0){
86+
for(int i=0;i<gates_num;i++){
87+
Snail *m = new Snail("Token");
88+
m->setKind(Token);
89+
m->setDistance(d + 1);
90+
send(m,"gate$o",i);
91+
}
92+
}else bubble("idle mutation :{ ");
93+
94+
95+
}else {
96+
/// ***********
97+
sender_gate = smsg->getArrivalGate()->getIndex();
98+
99+
if(message_kind == Token){
100+
101+
if(have_parent == false){
102+
103+
parent_Id = sender_gate;
104+
children.Clear();
105+
level = d + 1;
106+
have_parent = true;
107+
108+
109+
// displat stuff ...
110+
cDisplayString connDisplay = gate("gate$o",parent_Id)->getDisplayString();
111+
connDisplay.setTagArg("ls",0,"blue");
112+
connDisplay.setTagArg("ls",1,"3");
113+
gate("gate$o",parent_Id)->setDisplayString(connDisplay.str());
114+
115+
116+
// expected_msg <- | N(x) - {sender_gate }|;
117+
Nx.Remove(sender_gate);
118+
expected_msg = Nx.Size();
119+
Nx.Add(sender_gate);
120+
// EV << "expected_msg "<<expected_msg <<"\n";
121+
122+
if(expected_msg == 0){
123+
// send(Yes, d + 1) to parent
124+
yes = new Snail("Yes");
125+
yes->setKind(Yes);
126+
yes->setDistance(d + 1);
127+
send(yes,"gate$o",parent_Id);
128+
}else{
129+
// send(T, d + 1) to Nx-sender_gate
130+
for(int i=0;i<gates_num;i++){
131+
if(i == sender_gate) continue;
132+
tok = new Snail("Token");
133+
tok->setKind(Token);
134+
tok->setDistance(d + 1);
135+
send(tok,"gate$o",i);
136+
}
137+
}
138+
139+
}else{ //I have parent
140+
if(level > d + 1){
141+
parent_Id = sender_gate;
142+
children.Clear();
143+
level = d +1;
144+
have_parent = true;
145+
146+
147+
// displat stuff ...
148+
cDisplayString connDisplay = gate("gate$o",parent_Id)->getDisplayString();
149+
connDisplay.setTagArg("ls",0,"blue");
150+
connDisplay.setTagArg("ls",1,"3");
151+
gate("gate$o",parent_Id)->setDisplayString(connDisplay.str());
152+
153+
154+
Nx.Remove(sender_gate);
155+
expected_msg = Nx.Size();
156+
Nx.Add(sender_gate);
157+
158+
if(expected_msg == 0){
159+
// Send(yes, level) to Parent
160+
yes = new Snail("Yes");
161+
yes->setKind(Yes);
162+
yes->setDistance(level);
163+
send(yes,"gate$o",parent_Id);
164+
165+
}else{
166+
// send(T, d + 1) to Nx-sender_gate
167+
for(int i=0;i<gates_num;i++){
168+
if(i == sender_gate) continue;
169+
yes = new Snail("Token");
170+
yes->setKind(Token);
171+
yes->setDistance(d + 1);
172+
send(yes,"gate$o",i);
173+
}
174+
}
175+
}else{
176+
// send (No, d + 1) to sender_gate
177+
no = new Snail("No");
178+
no->setKind(No);
179+
no->setDistance(d + 1);
180+
send(no,"gate$o",sender_gate);
181+
}
182+
}
183+
}else{
184+
// response = either Yes or No
185+
if(d == level +1){
186+
if(message_kind == Yes){
187+
children.Add(sender_gate);
188+
189+
// displat stuff ...
190+
cDisplayString connDisplay = gate("gate$o",sender_gate)->getDisplayString();
191+
connDisplay.setTagArg("ls",0,"blue");
192+
connDisplay.setTagArg("ls",1,"3");
193+
gate("gate$o",sender_gate)->setDisplayString(connDisplay.str());
194+
}
195+
expected_msg--;
196+
197+
if(expected_msg == 0){
198+
if(parent_Id != my_Id){
199+
200+
// send (yes, level) to parent
201+
yes = new Snail("Yes");
202+
yes->setKind(Yes);
203+
yes->setDistance(level);
204+
send(yes,"gate$o",parent_Id);
205+
206+
}else{// I am the initiator
207+
// the node learns that bft tree is built
208+
EV << "I am : "<<my_Id <<"\n";
209+
bubble("Done !");
210+
}
211+
}
212+
}
213+
}
214+
}
215+
216+
// delete(smsg);
217+
// delete(yes);
218+
// delete(no);
219+
// delete(tok);
220+
221+
222+
}

BFT_draft/BFT_v0.png

186 KB
Loading

BFT_draft/Network.ned

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
simple Node{
2+
parameters:
3+
@display("i=block/routing");
4+
int initiator = default(1);
5+
gates:
6+
inout gate[];
7+
}
8+
9+
network Network0 {
10+
11+
types:
12+
channel Channel extends ned.DelayChannel {
13+
delay = 100 ms;
14+
}
15+
submodules:
16+
node[6] : Node;
17+
18+
connections:
19+
node[0].gate++ <--> Channel <--> node[1].gate++;
20+
node[1].gate++ <--> Channel <--> node[2].gate++;
21+
node[1].gate++ <--> Channel <--> node[4].gate++;
22+
node[3].gate++ <--> Channel <--> node[4].gate++;
23+
node[4].gate++ <--> Channel <--> node[5].gate++;
24+
25+
node[3].gate++ <--> Channel <--> node[5].gate++;
26+
27+
node[2].gate++ <--> Channel <--> node[0].gate++;
28+
29+
}
30+
31+
32+
network Network {
33+
34+
types:
35+
channel Channel extends ned.DelayChannel {
36+
delay = 100 ms;
37+
}
38+
submodules:
39+
node[7] : Node;
40+
41+
connections:
42+
node[0].gate++ <--> Channel <--> node[1].gate++;
43+
node[0].gate++ <--> Channel <--> node[2].gate++;
44+
45+
46+
node[1].gate++ <--> Channel <--> node[3].gate++;
47+
node[1].gate++ <--> Channel <--> node[4].gate++;
48+
49+
50+
51+
node[2].gate++ <--> Channel <--> node[5].gate++;
52+
node[2].gate++ <--> Channel <--> node[6].gate++;
53+
54+
}

BFT_draft/Set.cc

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// This program is free software: you can redistribute it and/or modify
3+
// it under the terms of the GNU Lesser General Public License as published by
4+
// the Free Software Foundation, either version 3 of the License, or
5+
// (at your option) any later version.
6+
//
7+
// This program is distributed in the hope that it will be useful,
8+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
// GNU Lesser General Public License for more details.
11+
//
12+
// You should have received a copy of the GNU Lesser General Public License
13+
// along with this program. If not, see http://www.gnu.org/licenses/.
14+
//
15+
16+
#include "Set.h"
17+
#include <omnetpp.h>
18+
19+
using namespace omnetpp;
20+
Set::Set() {
21+
// TODO Auto-generated constructor stub
22+
23+
}
24+
25+
Set::~Set() {
26+
// TODO Auto-generated destructor stub
27+
}
28+
29+
void Set::Add(int i){
30+
this->content.insert(i);
31+
}
32+
33+
void Set::Remove(int i){
34+
this->content.erase(i);
35+
}
36+
37+
bool Set::Empty(){
38+
return this->content.empty();
39+
}
40+
41+
void Set::Clear(){
42+
this->content.clear();
43+
}
44+
45+
Set Set::Union(Set& s2){
46+
Set tmp;
47+
set<int, greater<int> >::iterator itr;
48+
49+
for (itr = s2.content.begin();itr != s2.content.end(); ++itr){
50+
tmp.content.insert(*itr);
51+
}
52+
53+
for (itr = content.begin();itr != content.end(); ++itr){
54+
tmp.content.insert(*itr);
55+
}
56+
57+
return tmp;
58+
}
59+
60+
void Set::Print(){
61+
set<int, greater<int> >::iterator itr;
62+
for (itr = content.begin();itr != content.end(); ++itr){
63+
EV << *itr << " ";
64+
}
65+
EV <<"\n";
66+
67+
}
68+

0 commit comments

Comments
 (0)