Exemplo n.º 1
0
  def handle(self,obj):
    """ This function takes a LMCP.object and does the right thing with it
    obj can contain ServicePlannerConfiguration or ServicePlannerRequest
    if it is a Configuration we modify config and load a new region.
    This type of message will reset the infostate, so one must resend any
    outstanding requests.
    This type of message is typically only used once at the beginning.

    if it is a plan request, we update the infostate and return a plan.
    This is the normal usage.
    """
    if isinstance(obj,ServicePlannerConfiguration.ServicePlannerConfiguration):
      #config.enableModification()
      nameprefix = '_%s__'%obj.__class__.__name__
      for k,v in confnames.items():
        attrname = nameprefix+v
        if hasattr(obj,attrname):
          setattr(config,k,getattr(obj,attrname))
      
      for k,v in obj.__dict__.items():
        if (k.startswith('__') and not k.endswith('_') and 
            k not in notconfnames):
          print k
        setattr(config,k,v)
      #config.disableModification()
      try:
        region = lmcpio.createRegion(obj.get_Region())
      except:
        region  = None
      if region is None:
        print ("expected a region in Configuration message\n"
               "falling back to plannermodule.region")
        #pdb.set_trace()
      else:
        self.region = region
        self.planner = self.plannerfactory(region)
      self.infostate = InfoState(
        self.region, sensor_radius=config.sensor_radius, nagents=config.nagents,
        locations=None, speed=config.speed)
      networkregion = lmcpio.createNetwork(self.region)
      return LMCPFactory.packMessage(networkregion,True)
    elif isinstance(obj,ServicePlannerRequest.ServicePlannerRequest):
      if True:#try:
        lmcplocs = obj.get_AgentLocations()
        lmcpevent = obj.get_NewEvent()
        dvrpevent = lmcpio.lmcpEvent2dvrpEvent(lmcpevent)
        dvrplocs = [lmcpio.lmcpLoc2dvrpLoc(l) for l in lmcplocs]
      else:#except Exception, err:
        print err
        pdb.set_trace()
      event = dvrpevent
      self.infostate.update(event,dvrplocs)
      self.logStatus(event)
      requests,belief = self.infostate.getRequests(),self.infostate.getBelief()
      plan = self.planner.plan(belief,requests,dvrplocs)
      #pdb.set_trace()
      self.infostate.setPlan(plan)
      #pdb.set_trace()
      message = lmcpio.plansToMessage(plan)#,self.infostate.region)
      return message
    elif(isinstance(obj,RequestConfiguration.RequestConfiguration)):
      try:
        networkregion = lmcpio.createNetwork(self.region)
        return LMCPFactory.packMessage(networkregion,True)
      except Exception, err:
        print err
        pdb.set_trace()
Exemplo n.º 2
0
class PlannerModule(object):
  """ The planner module runs a server socket that accepts a connection
  then reads the socket once for a message, writes a plan to the socket
  and then closes the connection.
  
  One message per connection"""
  def __init__(self,plannerfactory,infostate,region,host=None):
    self.infostate = infostate
    self.plannerfactory = plannerfactory
    self.region = region
    self.planner = self.plannerfactory(region)
    self.shelf = shelve.open(config.logfilename)
    self.sock = socket.socket()
    if not host:
      self.host = ('localhost',PORT)
    else:
      self.host = host
    self.sock.bind(self.host)
    self.sock.listen(5)

  def listen(self):
    """ the listen loop """
    socket, addy = self.sock.accept()
    message = socket.recv(1e6)
    obj = LMCPFactory.internalFactory.getObject(message)
    retmessage = self.handle(obj)
    #pdb.set_trace()
    socket.send(retmessage)
    socket.close()

  def logStatus(self,event):
    """ log the event and our internal state """
    logentry = dict([('0',self._logStatus(event.time))])
    logentry['event'] = event
    self.shelf[str(event.id)] = logentry

  def _logStatus(self,time):
    """ what to actually put in the log """
    return {'handlerclass':'MultiAgent',
            'locations':self.infostate.getAgentlocs(time),
            'plan':self.infostate.getPlan(),
            'belief':self.infostate.getBelief(),
            'known':[r.id for r in self.infostate.requests],
            'updatetime':self.infostate.updatetime}

  def handle(self,obj):
    """ This function takes a LMCP.object and does the right thing with it
    obj can contain ServicePlannerConfiguration or ServicePlannerRequest
    if it is a Configuration we modify config and load a new region.
    This type of message will reset the infostate, so one must resend any
    outstanding requests.
    This type of message is typically only used once at the beginning.

    if it is a plan request, we update the infostate and return a plan.
    This is the normal usage.
    """
    if isinstance(obj,ServicePlannerConfiguration.ServicePlannerConfiguration):
      #config.enableModification()
      nameprefix = '_%s__'%obj.__class__.__name__
      for k,v in confnames.items():
        attrname = nameprefix+v
        if hasattr(obj,attrname):
          setattr(config,k,getattr(obj,attrname))
      
      for k,v in obj.__dict__.items():
        if (k.startswith('__') and not k.endswith('_') and 
            k not in notconfnames):
          print k
        setattr(config,k,v)
      #config.disableModification()
      try:
        region = lmcpio.createRegion(obj.get_Region())
      except:
        region  = None
      if region is None:
        print ("expected a region in Configuration message\n"
               "falling back to plannermodule.region")
        #pdb.set_trace()
      else:
        self.region = region
        self.planner = self.plannerfactory(region)
      self.infostate = InfoState(
        self.region, sensor_radius=config.sensor_radius, nagents=config.nagents,
        locations=None, speed=config.speed)
      networkregion = lmcpio.createNetwork(self.region)
      return LMCPFactory.packMessage(networkregion,True)
    elif isinstance(obj,ServicePlannerRequest.ServicePlannerRequest):
      if True:#try:
        lmcplocs = obj.get_AgentLocations()
        lmcpevent = obj.get_NewEvent()
        dvrpevent = lmcpio.lmcpEvent2dvrpEvent(lmcpevent)
        dvrplocs = [lmcpio.lmcpLoc2dvrpLoc(l) for l in lmcplocs]
      else:#except Exception, err:
        print err
        pdb.set_trace()
      event = dvrpevent
      self.infostate.update(event,dvrplocs)
      self.logStatus(event)
      requests,belief = self.infostate.getRequests(),self.infostate.getBelief()
      plan = self.planner.plan(belief,requests,dvrplocs)
      #pdb.set_trace()
      self.infostate.setPlan(plan)
      #pdb.set_trace()
      message = lmcpio.plansToMessage(plan)#,self.infostate.region)
      return message
    elif(isinstance(obj,RequestConfiguration.RequestConfiguration)):
      try:
        networkregion = lmcpio.createNetwork(self.region)
        return LMCPFactory.packMessage(networkregion,True)
      except Exception, err:
        print err
        pdb.set_trace()
    else: