Source

imalse / core / ns3 / node / imalse_netns_node.py

import random
import string
import sys
import time

from core.real.node import PhysicalNode
from core.ns3.util.netns3 import NetnsNode

class ImalseNetnsNode(NetnsNode, PhysicalNode):
    """Imalse Netns Node, it will use linux namespace to construct virtual machine.
    And applications will run in these virtual machines.
    """
    NODE_TYPE = 'real_ns3'
    def __init__(self, *args, **kwargs):
        self.cmd_set = None
        NetnsNode.__init__(self, *args, **kwargs)
        PhysicalNode.__init__(self)

    def ping(self, sock, data, threaded):        
        cmd = ["ping", "-b", "-c", str(data.num_packets), "-i", str(data.interval),\
               "-l", str(data.preload), "-s", str(data.packet_size),\
                data.hostname]
        print "%s: %s" % (self.name, " ".join(cmd))
        
        p, stdin, stdout, stderr = self.popen(cmd)
        err = stderr.read()
        out = stdout.read()
        sys.stderr.write(err)
        sys.stdout.write(out)

    def stop_ping(self, sock):
        print 'stop_ping'

    def start_keylogger(self, duration):
        now = time.time()
        done = lambda: time.time() > now + duration
        
        log = ''
        
        while not done():
            time.sleep(.5)
            log += random.choice(string.ascii_letters)
        
        self.cmd_set.send_to_server(log)