examples: adding some basic examples

 * consumer/producer
 * ndnping
 * traffic generator
 * catchunks/putchunks

Change-Id: Ie4c37f08f29a945f2fb1ce0b4d36da1bfb3edd3b
diff --git a/examples/chunks.py b/examples/chunks.py
new file mode 100644
index 0000000..037cd96
--- /dev/null
+++ b/examples/chunks.py
@@ -0,0 +1,95 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2021, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+"""
+This example demonstrates the functionality of the ndncatchunks and ndnputchunks. There are
+programs to transfer a file as Data segments in NDN.
+https://github.com/named-data/ndn-tools/tree/master/tools/chunks.
+"""
+
+from time import sleep
+import subprocess
+
+from mininet.log import setLogLevel, info
+from minindn.minindn import Minindn
+from minindn.util import MiniNDNCLI
+from minindn.apps.app_manager import AppManager
+from minindn.apps.nfd import Nfd
+from minindn.apps.nlsr import Nlsr
+
+def sendFile(node, prefix, file):
+    """
+    Publish a file using ndnputchunks
+     :parma mininet.node.Host node: mininet node object
+     :param string prefix: prefix to publish the chunks of the file
+     :param string file: file to publish
+    """
+    info ("File published:", file)
+    cmd = 'ndnputchunks {}/{} < {} > putchunks.log 2>&1 &'.format(prefix, "fname", file)
+    node.cmd(cmd)
+    # Sleep for appropriate time based on the file size
+    sleep(5)
+
+def receiveFile(node, prefix, filename):
+    """
+    Fetch a file using ndncatchunks
+     :parma mininet.node.Host node: mininet node object
+     :param string prefix: producer's prefix under which the file the published
+     :param string file: name given to the file that will be received
+    """
+    info ("Fething file: ", filename)
+    cmd = 'ndncatchunks {}/{} > {} 2> catchunks.log &'.format(prefix, "fname", filename)
+    node.cmd(cmd)
+
+if __name__ == '__main__':
+    setLogLevel('info')
+
+    # Create a test file to publish
+    testFile = "/tmp/test-chunks"
+    cmd = 'echo "demonstrate file transfer using catchunks and putchunks" > {}'.format(testFile)
+    subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()
+
+    Minindn.cleanUp()
+    Minindn.verifyDependencies()
+    ndn = Minindn()
+    ndn.start()
+
+    nfds = AppManager(ndn, ndn.net.hosts, Nfd)
+    nlsrs = AppManager(ndn, ndn.net.hosts, Nlsr)
+    sleep(70)
+
+    # Default topology is used in this experiment "/topologies/default-topology.conf"
+    # lets make node "a" as a producer node, and node "c" as a conumer node
+    producer  = ndn.net['a']
+    producerPrefix = "/test-producer" # prefix under which the file will be published
+    consumer = ndn.net['c']
+
+    # Advertise the producer prefix to the network
+    producer.cmd('nlsrc advertise {}'.format(producerPrefix))
+    sleep (5) # sleep for routing convergence.
+
+    sendFile(producer, producerPrefix, testFile)
+    receiveFile(consumer, producerPrefix, "test-chunks")
+
+    MiniNDNCLI(ndn.net)
+    ndn.stop()
diff --git a/examples/consumer-producer.py b/examples/consumer-producer.py
new file mode 100644
index 0000000..459b3d2
--- /dev/null
+++ b/examples/consumer-producer.py
@@ -0,0 +1,70 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2021, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from time import sleep
+
+from mininet.log import setLogLevel, info
+from minindn.minindn import Minindn
+from minindn.util import MiniNDNCLI
+from minindn.apps.app_manager import AppManager
+from minindn.apps.nfd import Nfd
+from minindn.apps.nlsr import Nlsr
+
+
+"""
+This example demonstrates a basic consumer-producer using Mini-NDN. It uses ndnpeek and ndnpoke as
+a consumer and a producer respectively. If you want to build your own consumer/producer program, you
+can take help from here: https://github.com/named-data/ndn-cxx/tree/master/examples and
+here: https://github.com/dulalsaurab/multicast-supression-ndn/tree/main/ndn-src/consumer-producer
+"""
+
+if __name__ == '__main__':
+    setLogLevel('info')
+
+    Minindn.cleanUp()
+    Minindn.verifyDependencies()
+    ndn = Minindn()
+    ndn.start()
+
+    info('Starting nfd and nlsr on nodes')
+    nfds = AppManager(ndn, ndn.net.hosts, Nfd)
+    nlsrs = AppManager(ndn, ndn.net.hosts, Nlsr)
+    sleep(90)
+
+    # Default topology is used in this experiment "/topologies/default-topology.conf"
+    # lets make node "a" as a producer node, and node "c" as a consumer node
+    producer = ndn.net['a']
+    consumer = ndn.net['c']
+
+    # start producer
+    producerPrefix = "/example"
+    producer.cmd('nlsrc advertise {}'.format(producerPrefix))
+    sleep(5) # sleep for routing convergence
+
+    # Make sure that basic consumer/producer example are compiled and installed in the system
+    info('Starting consumer and producer application')
+    producer.cmd("echo 'HELLO WORLD' | ndnpoke {} &> producer.log &".format(producerPrefix))
+    consumer.cmd("ndnpeek -p {} &> consumer.log &".format(producerPrefix))
+
+    MiniNDNCLI(ndn.net)
+    ndn.stop()
\ No newline at end of file
diff --git a/examples/nlsr/prefix_propogation.py b/examples/nlsr/prefix_propogation.py
index 7ea027c..e27c74f 100644
--- a/examples/nlsr/prefix_propogation.py
+++ b/examples/nlsr/prefix_propogation.py
@@ -1,6 +1,6 @@
 # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
 #
-# Copyright (C) 2015-2019, The University of Memphis,
+# Copyright (C) 2015-2021, The University of Memphis,
 #                          Arizona Board of Regents,
 #                          Regents of the University of California.
 #
@@ -22,6 +22,7 @@
 # If not, see <http://www.gnu.org/licenses/>.
 
 import time
+import sys
 
 from mininet.log import setLogLevel, info
 
diff --git a/examples/ping-demo.py b/examples/ping-demo.py
new file mode 100644
index 0000000..550c0df
--- /dev/null
+++ b/examples/ping-demo.py
@@ -0,0 +1,109 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2021, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+from subprocess import PIPE
+
+from mininet.log import setLogLevel, info
+from mininet.topo import Topo
+
+from minindn.minindn import Minindn
+from minindn.apps.app_manager import AppManager
+from minindn.util import MiniNDNCLI, getPopen
+from minindn.apps.nfd import Nfd
+from minindn.helpers.nfdc import Nfdc
+
+PREFIX = "/example"
+
+def printOutput(output):
+    _out = output.decode("utf-8").split("\n")
+    for _line in _out:
+        info(_line + "\n")
+
+def run():
+    Minindn.cleanUp()
+    Minindn.verifyDependencies()
+
+    # Topology can be created/modified using Mininet topo object
+    topo = Topo()
+    info("Setup\n")
+    # add hosts
+    a = topo.addHost('a')
+    b = topo.addHost('b')
+    c = topo.addHost('c')
+
+    # add links
+    topo.addLink(a, b, delay='10ms', bw=10) # bw = bandwidth
+    topo.addLink(b, c, delay='10ms', bw=10)
+
+    ndn = Minindn(topo=topo)
+    ndn.start()
+
+    # configure and start nfd on each node
+    info("Configuring NFD\n")
+    AppManager(ndn, ndn.net.hosts, Nfd, logLevel="DEBUG")
+
+    """
+    There are multiple ways of setting up routes in Mini-NDN
+    refer: https://minindn.memphis.edu/experiment.html#routing-options
+    It can also be set manually as follows. The important bit to note here
+    is the use of the Nfdc command
+    """
+    links = {"a":["b"], "b":["c"]}
+    for first in links:
+        for second in links[first]:
+            host1 = ndn.net[first]
+            host2 = ndn.net[second]
+            interface = host2.connectionsTo(host1)[0][0]
+            interface_ip = interface.IP()
+            Nfdc.createFace(host1, interface_ip)
+            Nfdc.registerRoute(host1, PREFIX, interface_ip, cost=0)
+
+    # Start ping server
+    info("Starting pings...\n")
+    pingserver_log = open("/tmp/minindn/c/ndnpingserver.log", "w")
+    getPopen(ndn.net["c"], "ndnpingserver {}".format(PREFIX), stdout=pingserver_log,\
+             stderr=pingserver_log)
+
+    # start ping client
+    ping1 = getPopen(ndn.net["a"], "ndnping {} -c 5".format(PREFIX), stdout=PIPE, stderr=PIPE)
+    ping1.wait()
+    printOutput(ping1.stdout.read())
+
+    interface = ndn.net["b"].connectionsTo(ndn.net["a"])[0][0]
+    info("Failing link\n") # failing link by setting link loss to 100%
+    interface.config(delay="10ms", bw=10, loss=100)
+    info ("\n starting ping2 client \n")
+
+    ping2 = getPopen(ndn.net["a"], "ndnping {} -c 5".format(PREFIX), stdout=PIPE, stderr=PIPE)
+    ping2.wait()
+    printOutput(ping2.stdout.read())
+
+    interface.config(delay="10ms", bw=10, loss=0) # bringing back the link
+
+    info("\nExperiment Completed!\n")
+    MiniNDNCLI(ndn.net)
+    ndn.stop()
+
+if __name__ == '__main__':
+    setLogLevel("info")
+    run()
\ No newline at end of file
diff --git a/examples/traffic_generator.py b/examples/traffic_generator.py
new file mode 100644
index 0000000..48bbd07
--- /dev/null
+++ b/examples/traffic_generator.py
@@ -0,0 +1,105 @@
+# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+#
+# Copyright (C) 2015-2021, The University of Memphis,
+#                          Arizona Board of Regents,
+#                          Regents of the University of California.
+#
+# This file is part of Mini-NDN.
+# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
+#
+# Mini-NDN is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Mini-NDN is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Mini-NDN, e.g., in COPYING.md file.
+# If not, see <http://www.gnu.org/licenses/>.
+
+
+"""
+This example demonstrates the functionality of the Traffic generator. It consists of a traffic
+server and client. The server will listen for interest on the prefix specified in the server
+configuration file. The client will send a designated number of interests to the server and 
+get the data back.
+More details on traffic generator here: https://github.com/named-data/ndn-traffic-generator
+"""
+
+from time import sleep
+
+from mininet.log import setLogLevel, info
+from minindn.minindn import Minindn
+from minindn.util import MiniNDNCLI
+from minindn.apps.app_manager import AppManager
+from minindn.apps.nfd import Nfd
+from minindn.apps.nlsr import Nlsr
+from minindn.util import copyExistentFile
+from examples.nlsr.nlsr_common import getParser
+
+def trafficServer(node, serverConfFile):
+    """
+    Start traffic server
+     :parma mininet.node.Host node: mininet node object
+     :param string serverConfFile: server configuration file
+    """
+    info ("Starting traffic server \n")
+    # c = 10, i.e maximum number of Interests to respond
+    cmd = 'ndn-traffic-server -c {} {} &> traffic-server.log &'.format(10, serverConfFile)
+    node.cmd(cmd)
+    sleep(10)
+
+    # The server configuration file uses /example prefix to advertise its service
+    # thus, server needs to advertise this prefix for the client to reach it
+    serverPrefix = "/example"
+    server.cmd('nlsrc advertise {}'.format(serverPrefix))
+    sleep(5) # sleep for routing convergence
+
+def trafficClient(node, clientConfFile):
+    """
+    Start traffic client
+     :parma mininet.node.Host node: The expiration period in milliseconds, or None if not specified.
+     :param string clientConfFile: client configuration file
+    """
+    info ("Starting ndn traffic client \n")
+    # c = 10, total number of Interests to be generated each at 200ms interval
+    cmd = 'ndn-traffic-client -c {} -i {} {} &>  traffic-client.log &'.format(10, 200, clientConfFile)
+    node.cmd(cmd)
+
+if __name__ == '__main__':
+    setLogLevel('info')
+
+    # Traffic generator configuration files. For this example, we are using default conf files.
+    # More details on configuration files here: https://github.com/named-data/ndn-traffic-generator
+    possibleServerConfPath = ["/etc/ndn/ndn-traffic-server.conf.sample", "/usr/local/etc/ndn/ndn-traffic-server.conf.sample"]
+    possibleClientConfPath = ["/etc/ndn/ndn-traffic-client.conf.sample", "/usr/local/etc/ndn/ndn-traffic-client.conf.sample"]
+
+    Minindn.cleanUp()
+    Minindn.verifyDependencies()
+    ndn = Minindn(parser=getParser())
+    ndn.start()
+
+    nfds = AppManager(ndn, ndn.net.hosts, Nfd)
+    nlsrs = AppManager(ndn, ndn.net.hosts, Nlsr)
+    sleep(90)
+
+    # Default topology is used in this experiment "/topologies/default-topology.conf"
+    # lets make node "a" as a traffic-server node, and node "c" as a traffic-client node
+    server  = ndn.net['a']
+    client = ndn.net['c']
+    serverConf = '{}/{}/server-conf'.format(ndn.args.workDir, server.name)
+    clientConf = '{}/{}/client-conf'.format(ndn.args.workDir, client.name)
+
+    copyExistentFile(server, possibleServerConfPath, serverConf)
+    copyExistentFile(server, possibleClientConfPath, clientConf)
+
+    trafficServer(server, serverConf)
+    trafficClient(client, clientConf)
+    # default location for the results: /tmp/minindn/
+
+    MiniNDNCLI(ndn.net)
+    ndn.stop()
diff --git a/examples/wifi/wifi_ping.py b/examples/wifi/wifi_ping.py
index a712342..6ba0531 100644
--- a/examples/wifi/wifi_ping.py
+++ b/examples/wifi/wifi_ping.py
@@ -1,6 +1,6 @@
 # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
 #
-# Copyright (C) 2015-2020, The University of Memphis,
+# Copyright (C) 2015-2021, The University of Memphis,
 #                          Arizona Board of Regents,
 #                          Regents of the University of California.
 #
@@ -23,7 +23,7 @@
 
 from mininet.log import setLogLevel, info
 from minindn.wifi.minindnwifi import MinindnWifi
-from minindn.util import MiniNDNWifiCLI, getPopen
+from minindn.util import MiniNDNWifiCLI
 from minindn.apps.app_manager import AppManager
 from minindn.apps.nfd import Nfd
 from minindn.helpers.nfdc import Nfdc
@@ -61,7 +61,7 @@
     ndnwifi.start()
     info("Starting NFD")
     sleep(2)
-    nfds = AppManager(ndnwifi, ndnwifi.net.stations, Nfd)
+    AppManager(ndnwifi, ndnwifi.net.stations, Nfd)
 
     info("Starting pingserver...")
     NDNPing.startPingServer(b, "/example")