def _set_private_state(self, state): """Deserialize and apply state common to all actors""" get_calvinsys().deserialize(actor=self, csobjects=state["_calvinsys"]) for port in state['inports']: # Uses setdefault to support shadow actor self.inports.setdefault(port, actorport.InPort( port, self))._set_state(state['inports'][port]) for port in state['outports']: # Uses setdefault to support shadow actor self.outports.setdefault(port, actorport.OutPort( port, self))._set_state(state['outports'][port]) self._component_members = set(state['_component_members']) # FIXME: The objects in _private_state_keys are well known, they are private after all, # and we shouldn't need this generic handler. for key in self._private_state_keys: if key not in self.__dict__: self.__dict__[key] = state.pop(key, None) else: obj = self.__dict__[key] if _implements_state(obj): obj.set_state(state.pop(key)) else: self.__dict__[key] = state.pop(key, None)
def set_migration_info(self, reply): if reply and reply.status == 200 and reply.data["node_id"]: self._migration_info = reply.data self.fsm.transition_to(Actor.STATUS.MIGRATABLE) _log.info("Migrate actor %s to node %s" % (self._name, self._migration_info["node_id"])) # Inform the scheduler that the actor is ready to migrate. get_calvinsys().scheduler_maintenance_wakeup() else: _log.info("No possible migration destination found for actor %s" % self._name) # Try to enable/migrate actor again after a delay. get_calvinsys().scheduler_maintenance_wakeup(delay=True)
def set_migration_info(self, reply): if reply and reply.status == 200 and reply.data["node_id"]: self._migration_info = reply.data self.fsm.transition_to(Actor.STATUS.MIGRATABLE) _log.info("Migrate actor %s to node %s" % (self._name, self._migration_info["node_id"])) # Inform the scheduler that the actor is ready to migrate. get_calvinsys().scheduler_maintenance_wakeup() else: _log.info("No possible migration destination found for actor %s" % self._name) # Try to enable/migrate actor again after a delay. get_calvinsys().scheduler_maintenance_wakeup(delay=True)
def _private_state(self, remap): """Serialize state common to all actors""" state = {} state['inports'] = { port: self.inports[port]._state(remap=remap) for port in self.inports } state['outports'] = { port: self.outports[port]._state(remap=remap) for port in self.outports } state['_component_members'] = list(self._component_members) # FIXME: The objects in _private_state_keys are well known, they are private after all, # and we shouldn't need this generic handler. for key in self._private_state_keys: obj = self.__dict__[key] if _implements_state(obj): try: state[key] = obj.state(remap) except: state[key] = obj.state() else: state[key] = obj state["_calvinsys"] = get_calvinsys().serialize(actor=self) return state
def _private_state(self): """Serialize state common to all actors""" state = {} state['inports'] = { port: self.inports[port]._state() for port in self.inports } state['outports'] = { port: self.outports[port]._state() for port in self.outports } state['_component_members'] = list(self._component_members) # Place requires in state, in the event we become a ShadowActor state['_requires'] = self.requires if hasattr(self, 'requires') else [] # FIXME: The objects in _private_state_keys are well known, they are private after all, # and we shouldn't need this generic handler. for key in self._private_state_keys: obj = self.__dict__[key] if _implements_state(obj): state[key] = obj.state() else: state[key] = obj state["_calvinsys"] = get_calvinsys().serialize(actor=self) return state
def handle_get_node_capabilities(self, handle, connection, match, data, hdr): """ GET /capabilities Get capabilities of this calvin node Response status code: OK Response: list of capabilities """ self.send_response(handle, connection, json.dumps(get_calvinsys().list_capabilities() + get_calvinlib().list_capabilities()))
def setup_calvinsys(): import calvin.runtime.north.calvinsys as calvinsys calvinsys.TESTING = True from calvin.runtime.north.calvinsys import get_calvinsys sys = get_calvinsys() sys.init(capabilities={ "sys.schedule": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} } }) return sys
def handle_get_node_capabilities(self, handle, connection, match, data, hdr): """ GET /capabilities Get capabilities of this calvin node Response status code: OK Response: list of capabilities """ self.send_response( handle, connection, json.dumps(self.node._calvinsys.list_capabilities() + get_calvinsys().list_capabilities() + get_calvinlib().list_capabilities()))
def _set_private_state(self, state): """Deserialize and apply state common to all actors""" if "_calvinsys" in state: get_calvinsys().deserialize(actor=self, csobjects=state["_calvinsys"]) for port in state['inports']: # Uses setdefault to support shadow actor self.inports.setdefault(port, actorport.InPort(port, self))._set_state(state['inports'][port]) for port in state['outports']: # Uses setdefault to support shadow actor self.outports.setdefault(port, actorport.OutPort(port, self))._set_state(state['outports'][port]) self._component_members= set(state['_component_members']) # FIXME: The objects in _private_state_keys are well known, they are private after all, # and we shouldn't need this generic handler. for key in self._private_state_keys: if key not in self.__dict__: self.__dict__[key] = state.get(key, None) else: obj = self.__dict__[key] if _implements_state(obj): obj.set_state(state.get(key)) else: self.__dict__[key] = state.get(key, None)
def check_requirements_and_sec_policy(self, requirements, security=None, actor_id=None, signer=None, decision_from_migration=None, callback=None): for req in requirements: if not get_calvinsys().has_capability(req) and not get_calvinlib().has_capability(req): raise Exception("Actor requires %s" % req) if security_enabled(): # Check if access is permitted for the actor by the security policy. # Will continue directly with callback if authorization is not enabled. security.check_security_policy(callback, actor_id=actor_id, requires=['runtime'] + requirements, element_type="actor", element_value=signer, decision_from_migration=decision_from_migration) else: callback()
def check_requirements_and_sec_policy(self, requirements, security=None, actor_id=None, signer=None, decision_from_migration=None, callback=None): for req in requirements: if not get_calvinsys().has_capability( req) and not get_calvinlib().has_capability(req): raise Exception("Actor requires %s" % req) if security_enabled(): # Check if access is permitted for the actor by the security policy. # Will continue directly with callback if authorization is not enabled. security.check_security_policy( callback, actor_id=actor_id, requires=['runtime'] + requirements, element_type="actor", element_value=signer, decision_from_migration=decision_from_migration) else: callback()
def _private_state(self): """Serialize state common to all actors""" state = {} state['inports'] = { port: self.inports[port]._state() for port in self.inports} state['outports'] = { port: self.outports[port]._state() for port in self.outports} state['_component_members'] = list(self._component_members) # Place requires in state, in the event we become a ShadowActor state['_requires'] = self.requires if hasattr(self, 'requires') else [] # FIXME: The objects in _private_state_keys are well known, they are private after all, # and we shouldn't need this generic handler. for key in self._private_state_keys: obj = self.__dict__[key] if _implements_state(obj): state[key] = obj.state() else: state[key] = obj state["_calvinsys"] = get_calvinsys().serialize(actor=self) return state
def close(ref): return get_calvinsys().close(ref)
def _will_end(self): if hasattr(self, "will_end") and callable(self.will_end): self.will_end() get_calvinsys().close_all(self)
def can_read(ref): return get_calvinsys().can_read(ref)
def read(ref): return get_calvinsys().read(ref)
def actor(): get_calvinsys()._node = Mock() return create_actor(DummyNode())
def write(ref, data): return get_calvinsys().write(ref, data)
def did_migrate(self): self.node_id = get_calvinsys()._node.id self.jumps += 1
def setup_calvinsys(): import calvin.runtime.north.calvinsys as calvinsys calvinsys.TESTING = True from calvin.runtime.north.calvinsys import get_calvinsys sys = get_calvinsys() sys.init( capabilities={ "io.button": { "module": "mock.MockInput", "attributes": { "data": [1, 0, 1, 0] } }, "io.digitalin": { "module": "mock.MockInput", "attributes": { "data": [1, 0, 1, 0] } }, "io.knob": { "module": "mock.MockInput", "attributes": { "data": [-1, 1, 0, 1] } }, "io.hallswitch": { "module": "mock.MockInput", "attributes": { "data": [False, True, False, True] } }, "io.switch": { "module": "mock.MockInput", "attributes": { "data": [0, 1, 0, 1] } }, "io.tiltswitch": { "module": "mock.MockInput", "attributes": { "data": [1, 0, 1, 0] } }, "io.lightbreaker": { "module": "mock.MockInput", "attributes": { "data": [False, True, False, True] } }, "io.pickupgesture": { "module": "mock.MockInput", "attributes": { "data": [False, True, False, True] } }, "io.buzzer": { "module": "mock.MockOutput", "attributes": {} }, "io.digitalout": { "module": "mock.MockOutput", "attributes": {} }, "io.light": { "module": "mock.MockOutput", "attributes": {} }, "io.stdout": { "module": "mock.MockOutput", "attributes": {} }, "io.pwm": { "module": "mock.MockOutput", "attributes": {} }, "io.servomotor": { "module": "mock.MockOutput", "attributes": {} }, "log.info": { "module": "mock.MockOutput", "attributes": {} }, "log.warning": { "module": "mock.MockOutput", "attributes": {} }, "log.error": { "module": "mock.MockOutput", "attributes": {} }, "web.twitter.post": { "module": "mock.MockOutput", "attributes": {} }, "notify.bell": { "module": "mock.MockOutput", "attributes": {} }, "image.render": { "module": "mock.MockOutput", "attributes": {} }, "web.pushbullet.channel.post": { "module": "mock.MockOutput", "attributes": {} }, "sys.schedule": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "sys.timer.once": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "sys.timer.repeating": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "sys.attribute.public": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "sys.attribute.indexed": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "image.facedetection": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "image.facefinding": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "image.source": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "image.objectdetection": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "image.objectfinding": { "module": "mock.MockInputOutput", "attributes": { 'data': ["dummy"] } }, "io.distance": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.temperature": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.pressure": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.accelerometer": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.gyroscope": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.soilmoisture": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.humidity": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.stepcounter": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "io.filesize": { "module": "mock.MockInput", "attributes": { "data": [44] } }, "io.filereader": { "module": "mock.MockInput", "attributes": { "data": ["the quick brown fox jumped over the lazy dog"] } }, "io.filewriter": { "module": "mock.MockOutput", "attributes": {} }, "io.stdin": { "module": "mock.MockInput", "attributes": { "data": ["the quick brown fox jumped over the lazy dog"] } }, "weather": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } }, "weather.local": { "module": "mock.MockInputOutput", "attributes": { "data": ["dummy"] } } }) return sys
def _will_end(self): if hasattr(self, "will_end") and callable(self.will_end): self.will_end() get_calvinsys().close_all(self)
def actor(): get_calvinsys()._node = Mock() return create_actor(DummyNode())
def close(ref): return get_calvinsys().close(ref)
def read(ref): return get_calvinsys().read(ref)
def can_read(ref): return get_calvinsys().can_read(ref)
def __init__(self, uris, control_uri, attributes=None): super(Node, self).__init__() self.quitting = False self.super_node_class = None # Warn if its not a uri if not isinstance(uris, list): _log.error("Calvin uris must be a list %s" % uris) raise TypeError("Calvin uris must be a list!") # Uris self.uris = uris if attributes: ext_uris = attributes.pop('external_uri', None) if ext_uris is not None: self.uris += ext_uris # Control uri self.control_uri = control_uri self.external_control_uri = attributes.pop('external_control_uri', self.control_uri) \ if attributes else self.control_uri try: self.attributes = AttributeResolver(attributes) except: _log.exception("Attributes not correct, uses empty attribute!") self.attributes = AttributeResolver(None) self.node_name = self.attributes.get_node_name_as_str() # Obtain node id, when using security also handle runtime certificate try: security_dir = _conf.get("security", "security_dir") self.runtime_credentials = RuntimeCredentials(self.node_name, node=self, security_dir=security_dir) self.id = self.runtime_credentials.get_node_id() except Exception as err: _log.debug("No runtime credentials, err={}".format(err)) self.runtime_credentials = None self.id = calvinuuid.uuid("Node") self.certificate_authority = certificate_authority.CertificateAuthority(self) self.authentication = authentication.Authentication(self) self.authorization = authorization.Authorization(self) self.am = actormanager.ActorManager(self) self.rm = replicationmanager.ReplicationManager(self) self.control = calvincontrol.get_calvincontrol() # _scheduler = scheduler.DebugScheduler if _log.getEffectiveLevel() <= logging.DEBUG else scheduler.Scheduler # _scheduler = scheduler.NonPreemptiveScheduler # _scheduler = scheduler.RoundRobinScheduler _scheduler = scheduler.SimpleScheduler # _scheduler = scheduler.BaselineScheduler self.sched = _scheduler(self, self.am) self.async_msg_ids = {} calvinsys = get_calvinsys() calvinsys.init(self) calvinlib = get_calvinlib() calvinlib.init() # Default will multicast and listen on all interfaces # TODO: be able to specify the interfaces # @TODO: Store capabilities self.storage = storage.Storage(self) self.network = CalvinNetwork(self) self.proto = CalvinProto(self, self.network) self.pm = PortManager(self, self.proto) self.app_manager = appmanager.AppManager(self) self.cpu_monitor = CpuMonitor(self.id, self.storage) self.mem_monitor = MemMonitor(self.id, self.storage) self.proxy_handler = ProxyHandler(self) # The initialization that requires the main loop operating is deferred to start function async.DelayedCall(0, self.start)
def can_write(ref): return get_calvinsys().can_write(ref)
def did_migrate(self): self.node_id = get_calvinsys()._node.id self.jumps += 1
def write(ref, data): return get_calvinsys().write(ref, data)
def open(actor, name, **kwargs): return get_calvinsys().open(name, actor, **kwargs)
def can_write(ref): return get_calvinsys().can_write(ref)
def init(self, dump=False): self.dump = dump self.last = None self.node_id = get_calvinsys()._node.id self.jumps = 0 self.index = 0
def init(self, dump=False): self.dump = dump self.last = None self.node_id = get_calvinsys()._node.id self.jumps = 0 self.index = 0
def __init__(self, uris, control_uri, attributes=None): super(Node, self).__init__() self.quitting = False # Warn if its not a uri if not isinstance(uris, list): _log.error("Calvin uris must be a list %s" % uris) raise TypeError("Calvin uris must be a list!") # Uris self.uris = uris if attributes: ext_uris = attributes.pop('external_uri', None) if ext_uris is not None: self.uris += ext_uris # Control uri self.control_uri = control_uri self.external_control_uri = attributes.pop('external_control_uri', self.control_uri) \ if attributes else self.control_uri try: self.attributes = AttributeResolver(attributes) except: _log.exception("Attributes not correct, uses empty attribute!") self.attributes = AttributeResolver(None) self.node_name = self.attributes.get_node_name_as_str() # Obtain node id, when using security also handle runtime certificate try: security_dir = _conf.get("security", "security_dir") self.runtime_credentials = RuntimeCredentials(self.node_name, node=self, security_dir=security_dir) self.id = self.runtime_credentials.get_node_id() except Exception as err: _log.debug("No runtime credentials, err={}".format(err)) self.runtime_credentials = None self.id = calvinuuid.uuid("Node") self.certificate_authority = certificate_authority.CertificateAuthority(self) self.authentication = authentication.Authentication(self) self.authorization = authorization.Authorization(self) self.metering = metering.set_metering(metering.Metering(self)) self.monitor = Event_Monitor() self.am = actormanager.ActorManager(self) self.rm = replicationmanager.ReplicationManager(self) self.control = calvincontrol.get_calvincontrol() _scheduler = scheduler.DebugScheduler if _log.getEffectiveLevel() <= logging.DEBUG else scheduler.Scheduler self.sched = _scheduler(self, self.am, self.monitor) self.async_msg_ids = {} self._calvinsys = CalvinSys(self) calvinsys = get_calvinsys() calvinsys.init(self) calvinlib = get_calvinlib() calvinlib.init(self) # Default will multicast and listen on all interfaces # TODO: be able to specify the interfaces # @TODO: Store capabilities self.storage = storage.Storage(self) self.network = CalvinNetwork(self) self.proto = CalvinProto(self, self.network) self.pm = PortManager(self, self.proto) self.app_manager = appmanager.AppManager(self) # The initialization that requires the main loop operating is deferred to start function async.DelayedCall(0, self.start)
def open(actor, name, **kwargs): return get_calvinsys().open(name, actor, **kwargs)
def close(obj): obj.close() get_calvinsys().remove(obj)
def setup_calvinsys(): import calvin.runtime.north.calvinsys as calvinsys calvinsys.TESTING = True from calvin.runtime.north.calvinsys import get_calvinsys sys = get_calvinsys() sys.init(capabilities={ "io.button": { "module": "mock.MockInput", "attributes": {"data": [1, 0, 1, 0]} }, "io.digitalin": { "module": "mock.MockInput", "attributes": {"data": [1, 0, 1, 0]} }, "io.knob": { "module": "mock.MockInput", "attributes": {"data": [-1, 1, 0, 1]} }, "io.hallswitch": { "module": "mock.MockInput", "attributes": {"data": [False, True, False, True]} }, "io.switch": { "module": "mock.MockInput", "attributes": {"data": [0, 1, 0, 1]} }, "io.tiltswitch": { "module": "mock.MockInput", "attributes": {"data": [1, 0, 1, 0]} }, "io.lightbreaker": { "module": "mock.MockInput", "attributes": {"data": [False, True, False, True]} }, "io.pickupgesture": { "module": "mock.MockInput", "attributes": {"data": [False, True, False, True]} }, "io.buzzer": { "module": "mock.MockOutput", "attributes": {} }, "io.digitalout": { "module": "mock.MockOutput", "attributes": {} }, "io.light": { "module": "mock.MockOutput", "attributes": {} }, "io.stdout": { "module": "mock.MockOutput", "attributes": {} }, "io.pwm": { "module": "mock.MockOutput", "attributes": {} }, "io.servomotor": { "module": "mock.MockOutput", "attributes": {} }, "log.info": { "module": "mock.MockOutput", "attributes": {} }, "log.warning": { "module": "mock.MockOutput", "attributes": {} }, "log.error": { "module": "mock.MockOutput", "attributes": {} }, "web.twitter.post": { "module": "mock.MockOutput", "attributes": {} }, "notify.bell": { "module": "mock.MockOutput", "attributes": {} }, "image.render": { "module": "mock.MockOutput", "attributes": {} }, "web.pushbullet.channel.post": { "module": "mock.MockOutput", "attributes": {} }, "sys.schedule": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "sys.timer.once": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "sys.timer.repeating": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "sys.attribute.public": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "sys.attribute.indexed": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "image.facedetection": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "image.facefinding": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "image.source": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "image.objectdetection": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "image.objectfinding": { "module": "mock.MockInputOutput", "attributes": {'data': ["dummy"]} }, "io.distance": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.temperature": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.pressure": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.accelerometer": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.gyroscope": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.soilmoisture": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.humidity": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.stepcounter": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "io.filesize": { "module": "mock.MockInput", "attributes": {"data": [44]} }, "io.filereader": { "module": "mock.MockInput", "attributes": {"data": ["the quick brown fox jumped over the lazy dog"]} }, "io.filewriter": { "module": "mock.MockOutput", "attributes": {} }, "io.stdin": { "module": "mock.MockInput", "attributes": {"data": ["the quick brown fox jumped over the lazy dog"]} }, "weather": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} }, "weather.local": { "module": "mock.MockInputOutput", "attributes": {"data": ["dummy"]} } }) return sys