1. David G. Quintas
  2. BoincVM

Source

BoincVM / VMStompEngine.py

import logging

import stomper
import MsgInterpreter
import netifaces
import words
import BaseStompEngine
from destinations import *

NETWORK_IFACE = "eth1"

#FIXME: this is an abstract class... is there any way to force
#python to treat it as such? metaclasses? black magic?
class VMStompEngine(BaseStompEngine.BaseStompEngine):
  """ This basically models a client (ie, VM instance)"""
  
  logger = logging.getLogger('VMStompEngine')

  def connected(self, msg):
    res = []
    #once connected, subscribe
    res.append(stomper.subscribe(CMD_REQ_DESTINATION))

    #announce ourselves
    res.append( words.HELLO.howToSay(self) )

    return tuple(res)


class VM(VMStompEngine):

  def __init__(self):
    VMStompEngine.__init__(self)
    self._msgInterpreter = MsgInterpreter.MsgInterpreter(self)
    #FIXME: this can be improved
    self._ip = netifaces.ifaddresses(NETWORK_IFACE)[netifaces.AF_INET][0]['addr']
    self.logger.debug("Engine instantiated with ip %s" % self._ip )

  @property
  def ip(self):
    return self._ip

  def dealWithExecutionResults(self, results):
    resultsFields = ('cmd-id', 'out', 'err', 'finished', 'exitCodeOrSignal' )
    resultsDict = dict( zip( resultsFields, results ) )
    #self.protocol got injected by StompProtocol
    self.protocol.sendStompMessage( words.CMD_RESULT.howToSay(self, resultsDict) )


  def __repr__(self):
    return "VM with uid <%s>" % self._ip