: bharatvaj/forwarder

commit 1ee7add0451a41599a9e4d0ed24980a6dd91818e
parent 8ee79dd86d7f8c018316ac85e0ea8a8d9476c5fe
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Mon, 5 Aug 2019 02:16:07 +0530

Add enable and scan option, Remove add, select and clear
1 file changed, 36 insertions(+), 21 deletions(-)
M
forwarder.cpp
|
57
++++++++++++++++++++++++++++++++++++---------------------
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -33,16 +33,16 @@ void exit_handler(int sig) {
   exit(EXIT_SUCCESS);
 }
 
-void add(shell4c::Operation op, vector<string> msg) {
+void enable(shell4c::Operation op, vector<string> msg) {
   if (msg.size() == 0) {
-    shell.print(op.usage + "\n");
+    shell.println(op.usage);
     return;
   }
   Network *network = nc->networks[msg[0]];
-  if (network == nullptr) {
+  if (!network) {
     network = nc->activeNetworks[msg[0]];
-    if (network == nullptr) {
-      shell.print("Enter a valid network name\n");
+    if (!network) {
+      shell.println("Enter a valid network name");
       return;
     }
   }

@@ -53,11 +53,13 @@ void add(shell4c::Operation op, vector<string> msg) {
   nc->networks.erase(network->getUserFriendlyName());
 }
 
-void clear(shell4c::Operation op, vector<string>) { system("clear"); }
+void disable(shell4c::Operation op, vector<string> msg){
+  //
+}
 
 void config(shell4c::Operation op, vector<string> msg) {
   if (msg.size() == 0) {
-    shell.print(op.usage);
+    shell.println(op.usage);
     return;
   }
 }

@@ -89,15 +91,28 @@ void bind_(shell4c::Operation op, vector<string> msg) {
   nc->bind(msg[0], msg[1], msg[2], msg[3]);
 }
 
-// FIXME
-// void bind_(shell4c::Operation op, vector<string> msg) {
-//   if (msg.size() < 2) {
-//     shell.print(op.usageMessage);
-//     return;
-//   }
-// }
-
-void select_(shell4c::Operation op, vector<string> messages) {}
+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();

@@ -125,21 +140,21 @@ int main(int argc, char *argv[]) {
   Log::enable();
   nc = new NetworkController();
   shell.addOperation(new shell4c::Operation(
-      "add", "adds network to ready state", add, "add <network1> [options]"));
+      "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("clear", "clears the screen", clear, "clear"));
   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("select", "select a network to perform operations",
-                             select_, "select <network> [<network2>]"));
+      new shell4c::Operation("scan", "scan a network",
+                             scan, "scan <network>"));
   shell.setOnShellExit(exit_handler);
   shell.start();
   while (1)