Skip to content

Instantly share code, notes, and snippets.

@zhehaowang
Created September 2, 2015 16:09
Show Gist options
  • Save zhehaowang/7cb410d1ee442fd081e1 to your computer and use it in GitHub Desktop.
Save zhehaowang/7cb410d1ee442fd081e1 to your computer and use it in GitHub Desktop.
Test echo consumer and producer for multiple times data in interest re-expression scenario
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
#
# Copyright (C) 2014-2015 Regents of the University of California.
# Author: Jeff Thompson <jefft0@remap.ucla.edu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# A copy of the GNU Lesser General Public License is in the file COPYING.
# Question: Is nfd going to reply only once, or multiple times, for a retransmitted interest? What is the client's expectation?
# In the case of retransmissino, should nfd only forward the retranmistted interest, but not leave a trace in its own PIT?
import sys
import time
from pyndn import Name
from pyndn import Face, Interest
def dump(*list):
result = ""
for element in list:
result += (element if type(element) is str else repr(element)) + " "
print(result)
class Counter(object):
def __init__(self):
self._callbackCount = 0
def onData(self, interest, data):
self._callbackCount += 1
dump("Got data packet with name", data.getName().toUri())
# Use join to convert each byte to chr.
dump(data.getContent().toRawStr())
def onTimeout(self, interest):
self._callbackCount += 1
dump("Time out for interest", interest.getName().toUri())
def main():
# The default Face will connect using a Unix socket, or to "localhost".
face = Face()
counter = Counter()
if sys.version_info[0] <= 2:
word = raw_input("Enter a word to echo: ")
else:
word = input("Enter a word to echo: ")
name = Name("/testecho")
name.append(word)
dump("Express interest ", name.toUri())
interest = Interest(name)
interest.setInterestLifetimeMilliseconds(20000)
face.expressInterest(interest, counter.onData, counter.onTimeout)
sleepThreshold = 20
sleepCount = 0
numOfInst = 5
sendCount = 0
while True:
face.processEvents()
# We need to sleep for a few milliseconds so we don't use 100% of the CPU.
time.sleep(0.01)
if sleepCount != sleepThreshold:
pass
elif sendCount < numOfInst:
dump("Express interest ", name.toUri())
sendCount += 1
sleepCount = 0
face.expressInterest(interest, counter.onData, counter.onTimeout)
sleepCount += 1
face.shutdown()
main()
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
#
# Copyright (C) 2014-2015 Regents of the University of California.
# Author: Jeff Thompson <jefft0@remap.ucla.edu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# A copy of the GNU Lesser General Public License is in the file COPYING.
import time
from pyndn import Name
from pyndn import Data
from pyndn import Face
from pyndn.security import KeyChain
def dump(*list):
result = ""
for element in list:
result += (element if type(element) is str else repr(element)) + " "
print(result)
class Echo(object):
def __init__(self, keyChain, certificateName):
self._keyChain = keyChain
self._certificateName = certificateName
self._responseCount = 0
def onInterest(self, prefix, interest, face, interestFilterId, filter):
print("interest received: " + interest.getName().toUri())
self._responseCount += 1
# Wait 2 seconds before answering
time.sleep(2)
# Make and sign a Data packet.
data = Data(interest.getName())
content = "Echo " + interest.getName().toUri()
data.setContent(content)
self._keyChain.sign(data, self._certificateName)
dump("Sent content", content)
face.putData(data)
time.sleep(2)
# Only answers one interest from consumer
face.shutdown()
exit()
def onRegisterFailed(self, prefix):
self._responseCount += 1
dump("Register failed for prefix", prefix.toUri())
def main():
# The default Face will connect using a Unix socket, or to "localhost".
face = Face()
# Use the system default key chain and certificate name to sign commands.
keyChain = KeyChain()
face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
# Also use the default certificate name to sign data packets.
echo = Echo(keyChain, keyChain.getDefaultCertificateName())
prefix = Name("/testecho")
dump("Register prefix", prefix.toUri())
face.registerPrefix(prefix, echo.onInterest, echo.onRegisterFailed)
while echo._responseCount < 1:
face.processEvents()
# We need to sleep for a few milliseconds so we don't use 100% of the CPU.
time.sleep(0.01)
face.shutdown()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment