blob: 037cd962dae3dc2428395d90b4953ed60c088c91 [file] [log] [blame]
dulalsaurab20855442021-05-21 20:37:03 +00001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015-2021, The University of Memphis,
4# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
24"""
25This example demonstrates the functionality of the ndncatchunks and ndnputchunks. There are
26programs to transfer a file as Data segments in NDN.
27https://github.com/named-data/ndn-tools/tree/master/tools/chunks.
28"""
29
30from time import sleep
31import subprocess
32
33from mininet.log import setLogLevel, info
34from minindn.minindn import Minindn
35from minindn.util import MiniNDNCLI
36from minindn.apps.app_manager import AppManager
37from minindn.apps.nfd import Nfd
38from minindn.apps.nlsr import Nlsr
39
40def sendFile(node, prefix, file):
41 """
42 Publish a file using ndnputchunks
43 :parma mininet.node.Host node: mininet node object
44 :param string prefix: prefix to publish the chunks of the file
45 :param string file: file to publish
46 """
47 info ("File published:", file)
48 cmd = 'ndnputchunks {}/{} < {} > putchunks.log 2>&1 &'.format(prefix, "fname", file)
49 node.cmd(cmd)
50 # Sleep for appropriate time based on the file size
51 sleep(5)
52
53def receiveFile(node, prefix, filename):
54 """
55 Fetch a file using ndncatchunks
56 :parma mininet.node.Host node: mininet node object
57 :param string prefix: producer's prefix under which the file the published
58 :param string file: name given to the file that will be received
59 """
60 info ("Fething file: ", filename)
61 cmd = 'ndncatchunks {}/{} > {} 2> catchunks.log &'.format(prefix, "fname", filename)
62 node.cmd(cmd)
63
64if __name__ == '__main__':
65 setLogLevel('info')
66
67 # Create a test file to publish
68 testFile = "/tmp/test-chunks"
69 cmd = 'echo "demonstrate file transfer using catchunks and putchunks" > {}'.format(testFile)
70 subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()
71
72 Minindn.cleanUp()
73 Minindn.verifyDependencies()
74 ndn = Minindn()
75 ndn.start()
76
77 nfds = AppManager(ndn, ndn.net.hosts, Nfd)
78 nlsrs = AppManager(ndn, ndn.net.hosts, Nlsr)
79 sleep(70)
80
81 # Default topology is used in this experiment "/topologies/default-topology.conf"
82 # lets make node "a" as a producer node, and node "c" as a conumer node
83 producer = ndn.net['a']
84 producerPrefix = "/test-producer" # prefix under which the file will be published
85 consumer = ndn.net['c']
86
87 # Advertise the producer prefix to the network
88 producer.cmd('nlsrc advertise {}'.format(producerPrefix))
89 sleep (5) # sleep for routing convergence.
90
91 sendFile(producer, producerPrefix, testFile)
92 receiveFile(consumer, producerPrefix, "test-chunks")
93
94 MiniNDNCLI(ndn.net)
95 ndn.stop()