def stop_hosts(self):
     for host in self.running_hosts:
         stop = message.PlatformStopMessage()
         msg = message.SimulationMessage(stop)
         id = self.topology.routing_table[host]
         if id is not None:
             self.topology.send_message(msg, id)
 def start_vms(self, vms, address):
     status = True
     for vm in vms:
         status = self.start_vm(vm)
         started = message.VmStartedMessage(vm, status)
         msg = message.SimulationMessage(started)
         id = self.topology.routing_table[self.root_address]
         if id is not None:
             self.topology.send_message(msg, id)
    def distribute_nodes(self):
        print 'Distributing vms'
        self.pending_vms = []
        for vm in self.commands.vms:
            self.pending_vms.append(vm.name)
        self.vm_set = {}
        self.vm_to_host = {}
        for host in self.running_hosts:
            self.vm_set[host] = []

        # add the local node
        self.vm_set[None] = []
        # map vms to nodes
        for i in range(len(self.vm_set)):
            for j in range(i, len(self.pending_vms), len(self.vm_set)):
                if self.vm_set.keys()[i] is not None:
                    print 'Distributing ', self.pending_vms[
                        j], ' to ', self.vm_set.keys()[i]
                else:
                    print 'Distributing ', self.pending_vms[
                        j], ' to local node'
                self.vm_set.values()[i].append(self.pending_vms[j])

        # send commands for remote nodes
        for host in self.vm_set:
            if host is not None:
                start = message.StartVmsMessage(self.vm_set[host])
                msg = message.SimulationMessage(start)
                id = self.topology.routing_table[host]
                if id is not None:
                    self.topology.send_message(msg, id)
                    for vm in self.vm_set[host]:
                        self.vm_to_host[vm] = host
                else:
                    print 'Error: Could not find host'

        local_vms = self.vm_set[None]
        for vm in local_vms:
            if self.start_vm(vm):
                self.vm_started(vm)
            else:
                self.stop_hosts()
                self.topology.stop()
                sys.exit(1)
            self.vm_to_host[vm] = None
        self.sem.acquire()
 def run(self, commands):
     self.commands = commands
     if self.commands is None:  # not root
         start = message.PlatformStartedMessage()
         msg = message.SimulationMessage(start)
         id = self.topology.routing_table[self.root_address]
         if id is not None:
             self.topology.send_message(msg, id)
         self.sem.acquire()
     else:  # root
         self.start_hosts(self.commands.hosts)
         self.distribute_nodes()
         print 'Node distribution: ', self.vm_set
         self.event_queue.init(self.commands.simulation.duration,
                               self.commands.simulation.events,
                               self.treat_simulation_event)
         self.event_queue.run()
         self.stop_hosts()
     self.topology.stop()