path:
/forwarder.cpp
4.36 KB | plain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <csignal>
#include <iostream>
#include <map>
#include <thread>
#include "Operation.hpp"
#include "Shell.hpp"
#include <Configuration.hpp>
#include <ConfigurationBuilder.hpp>
#include <Log.hpp>
#include <NetworkController.hpp>
#include <fmt/format.h>
#include <model/DeviceInfo.hpp>
#include <network/Network.hpp>
#include <network/NetworkIndexer.hpp>
using namespace std;
using namespace wa;
using namespace shell4c;
NetworkController *nc;
Shell shell(false, "adapter> ");
void exit_handler(int sig) {
if (nc != nullptr) {
nc->stop();
}
// delete left and right initiators*
// TODO unregister callbacks
for (auto network : nc->activeNetworks) {
delete network.second;
}
exit(EXIT_SUCCESS);
}
void enable(shell4c::Operation op, vector<string> msg) {
if (msg.size() == 0) {
shell.println(op.usage);
return;
}
Network *network = nc->networks[msg[0]];
if (!network) {
network = nc->activeNetworks[msg[0]];
if (!network) {
shell.println("Enter a valid network name");
return;
}
}
if (msg.size() > 1) {
network->setNetworkParams(msg[1]);
}
nc->activeNetworks[msg[0]] = network;
nc->networks.erase(network->getUserFriendlyName());
}
void disable(shell4c::Operation op, vector<string> msg){
//
}
void config(shell4c::Operation op, vector<string> msg) {
if (msg.size() == 0) {
shell.println(op.usage);
return;
}
}
void list(shell4c::Operation op, vector<string> msg) {
if (nc->activeNetworks.size() > 0) {
shell.print(fmt::format("\u001b[32m"));
for (auto network : nc->activeNetworks) {
shell.print(fmt::format("{}", network.first));
if (!network.second->getNetworkParams().empty()) {
shell.print(fmt::format(" [{}]", network.second->getNetworkParams()));
}
shell.print("\n");
}
shell.print("\u001b[0m");
}
if (nc->networks.size() > 0) {
for (auto network : nc->networks) {
shell.print(fmt::format("{}\n", network.first));
}
}
}
void bind_(shell4c::Operation op, vector<string> msg) {
if (msg.size() < 4) {
shell.println(op.usage);
return;
}
nc->bind(msg[0], msg[1], msg[2], msg[3]);
}
void scan(shell4c::Operation op, vector<string> msgs){
if(msgs.empty()){
shell.println(op.usage);
return;
}
Network *network = nc->networks[msgs[0]];
if (!network) {
network = nc->activeNetworks[msgs[0]];
if (!network) {
shell.println("Enter a valid network name");
return;
}
}
auto devices = network->discover();
if(devices.empty()){
shell.println("No devices found");
return;
}
for(auto d : devices){
shell.println(d.address);
}
}
void help(shell4c::Operation op, vector<string> messages) {
auto ops = shell.getOperations();
if (messages.size() == 0) {
for (auto op : ops) {
if (op.second == nullptr)
continue;
shell.println(fmt::format("\u001b[33m{}\u001b[0m\t- {}", op.first,
op.second->description));
}
return;
}
for (auto msg : messages) {
if (ops.find(msg) != ops.end()) {
shell4c::Operation *op = ops[msg];
if (op == nullptr)
continue;
shell.println(op->usage);
}
}
}
int main(int argc, char *argv[]) {
signal(SIGINT, exit_handler);
Log::enable();
nc = new NetworkController();
shell.addOperation(new shell4c::Operation(
"enable", "adds network to ready state", enable, "enable <network1> [options]"));
// shell.addOperation(new shell4c::Operation(
// "disable", "adds network to ready state", disable, "disable <network1> [network2 ...]"));
shell.addOperation(new shell4c::Operation("bind", "binds ip address to mac",
bind_, "bind <ip> <port> <mac>"));
shell.addOperation(
new shell4c::Operation("config", "configures medium to network", config,
"config <network1> [<network2> ...]"));
shell.addOperation(new shell4c::Operation("ls", "list all available network",
list, "ls [network]"));
shell.addOperation(new shell4c::Operation("help", "prints this menu", help,
"help [operation1 ...]"));
shell.addOperation(
new shell4c::Operation("scan", "scan a network",
scan, "scan <network>"));
shell.setOnShellExit(exit_handler);
shell.start();
while (1)
;
return 0;
}