def __init__(self, sessionid=None, cfg={}, server=None, persistent=False, mkdir=True): if sessionid is None: # try to keep this short since it's used to construct # network interface names pid = os.getpid() sessionid = ((pid >> 16) ^ (pid & ((1 << 16) - 1))) sessionid ^= ((id(self) >> 16) ^ (id(self) & ((1 << 16) - 1))) sessionid &= 0xffff self.sessionid = sessionid self.sessiondir = os.path.join( tempfile.gettempdir(), #self.sessiondir = os.path.join('/home/dokeeffe/tmp', "pycore.%s" % self.sessionid) if mkdir: os.mkdir(self.sessiondir) self.name = None self.filename = None self.thumbnail = None self.user = None self.node_count = None self._time = time.time() self.evq = EventLoop() # dict of objects: all nodes and nets self._objs = {} self._objslock = threading.Lock() # dict of configurable objects self._confobjs = {} self._confobjslock = threading.Lock() self._handlers = set() self._handlerslock = threading.Lock() self._state = None self._hooks = {} self._state_hooks = {} # dict of configuration items from /etc/core/core.conf config file self.cfg = cfg self.add_state_hook(coreapi.CORE_EVENT_RUNTIME_STATE, self.runtime_state_hook) self.setstate(state=coreapi.CORE_EVENT_DEFINITION_STATE, info=False, sendevent=False) self.server = server if not persistent: self.addsession(self) self.master = False self.broker = CoreBroker(session=self, verbose=True) self.location = CoreLocation(self) self.mobility = MobilityManager(self) self.services = CoreServices(self) self.emane = emane.Emane(self) self.xen = xenconfig.XenConfigManager(self) self.sdt = Sdt(self) # future parameters set by the GUI may go here self.options = SessionConfig(self) self.metadata = SessionMetaData(self)
def __init__(self, session_id, config=None, mkdir=True): """ Create a Session instance. :param int session_id: session id :param dict config: session configuration :param bool mkdir: flag to determine if a directory should be made """ self.session_id = session_id # define and create session directory when desired self.session_dir = os.path.join(tempfile.gettempdir(), "pycore.%s" % self.session_id) if mkdir: os.mkdir(self.session_dir) self.name = None self.file_name = None self.thumbnail = None self.user = None self.event_loop = EventLoop() # dict of objects: all nodes and nets self.objects = {} self._objects_lock = threading.Lock() # TODO: should the default state be definition? self.state = EventTypes.NONE.value self._state_time = time.time() self._state_file = os.path.join(self.session_dir, "state") self._hooks = {} self._state_hooks = {} self.add_state_hook(state=EventTypes.RUNTIME_STATE.value, hook=self.runtime_state_hook) self.master = False # handlers for broadcasting information self.event_handlers = [] self.exception_handlers = [] self.node_handlers = [] self.link_handlers = [] self.file_handlers = [] self.config_handlers = [] self.shutdown_handlers = [] # session options/metadata self.options = SessionConfig() if not config: config = {} for key, value in config.iteritems(): self.options.set_config(key, value) self.metadata = SessionMetaData() # initialize session feature helpers self.broker = CoreBroker(session=self) self.location = CoreLocation() self.mobility = MobilityManager(session=self) self.services = CoreServices(session=self) self.emane = EmaneManager(session=self) self.sdt = Sdt(session=self)