Beispiel #1
0
    def addNeuShare(self, name, task, tags=()):
        '''
        Add a shared object to the neuron

        Example:

            task = ('synapse.cortex.openurl', ('ram:///',), {})

            neu.addNeuShare('hehe', task, tags=('foo.bar.baz',))

        '''
        item = s_dyndeps.runDynTask(task)
        self.share(name, item, tags=tags)

        jsinfo = dict(name=name, task=task, tags=tags)
        self.core.addTufoList(self.neur, 'shared', json.dumps(jsinfo))
Beispiel #2
0
    def addNeuShare(self, name, task, tags=()):
        '''
        Add a shared object to the neuron

        Example:

            task = ('synapse.cortex.openurl', ('ram:///',), {})

            neu.addNeuShare('hehe', task, tags=('foo.bar.baz',))

        '''
        item = s_dyndeps.runDynTask(task)
        self.share(name,item,tags=tags)

        jsinfo = dict(name=name, task=task, tags=tags)
        self.core.addTufoList(self.neur,'shared', json.dumps(jsinfo) )
Beispiel #3
0
def subtask(job):
    jid = job[0]

    slot = job[1].get('slot')

    meld = job[1].get('meld')
    if meld != None:
        s_mindmeld.loadMindMeld(meld)

    hive = slot[1].get('hive')

    queen = s_telepath.openurl( job[1].get('queen') )

    s_threads.put('syn.queen',queen)

    try:
        dyntask = job[1].get('dyntask')
        ret = s_dyndeps.runDynTask(dyntask)

        queen.tell(hive, 'job:done', jid=jid, ret=ret)

    except Exception as e:
        queen.tell(hive, 'job:done', jid=jid, **excinfo(e))
Beispiel #4
0
def subtask(job):
    jid = job[0]

    slot = job[1].get('slot')

    meld = job[1].get('meld')
    if meld is not None:
        s_mindmeld.loadMindMeld(meld)

    hive = slot[1].get('hive')

    queen = s_telepath.openurl(job[1].get('queen'))

    s_scope.set('syn.queen', queen)

    try:
        dyntask = job[1].get('dyntask')
        ret = s_dyndeps.runDynTask(dyntask)

        queen.tell(hive, 'job:done', jid=jid, ret=ret)

    except Exception as e:
        queen.tell(hive, 'job:done', jid=jid, **s_common.excinfo(e))
Beispiel #5
0
 def test_dyndeps_dyntask(self):
     task = ('synapse.tests.test_dyndeps.Foo', (), {})
     foo = s_dyndeps.runDynTask(task)
     self.assertEqual( foo.bar(), 'baz' )
Beispiel #6
0
 def test_dyndeps_dyntask(self):
     task = ('synapse.tests.test_dyndeps.Foo', (), {})
     foo = s_dyndeps.runDynTask(task)
     self.assertEqual(foo.bar(), 'baz')
Beispiel #7
0
    def __init__(self, core=None, pool=None):

        s_daemon.Daemon.__init__(self, core=core, pool=pool)

        self.sched = s_sched.Sched()

        self.core.addTufoForm('neuron')

        #self.core.addTufoProp('neuron','name')
        #self.core.addTufoProp('neuron','super',ptype='int',defval=0)
        #self.core.addTufoProp('neuron','usepki', ptype='bool', defval=0)

        self.neur = self.core.formTufoByProp('neuron', 'self')
        self.iden = self.neur[0]

        self.peers = {}  # <peer>:<sock>
        self.routes = {}  # <dest>:[ (dist,peer), ... ]

        #self.mesh = {}
        #self.peers = {}

        #self.mesh['certs'] = {}
        #self.mesh['links'] = {}

        #self.mesh['peers'] = { self.iden:self.neur }

        #self.sockbyfrom = s_cache.Cache(maxtime=120)
        #self.sockbyfrom.setOnMiss( self._getFakeSock )

        #self.links = collections.defaultdict(set)
        #
        #self.linkpaths = s_cache.Cache(maxtime=30)
        #self.linkpaths.setOnMiss( self._getLinkPath )

        self.setMesgFunc('peer:syn', self._onPeerSynMesg)
        self.setMesgFunc('peer:synack', self._onPeerSynAckMesg)
        self.setMesgFunc('peer:fin',
                         self._onPeerFinMesg)  # gracefully shut down

        self.setMesgFunc('peer:data', self._onPeerDataMesg)
        self.setMesgFunc('peer:route',
                         self._onPeerLinkMesg)  # route change information

        #self.setMesgFunc('peer:link:init', self._onPeerLinkInitMesg )

        #self.setMesgFunc('neu:peer:chal', self._onNeuPeerChal )
        #self.setMesgFunc('neu:peer:resp', self._onNeuPeerResp )

        #self.setMesgFunc('neu:data', self._onNeuDataMesg )
        #self.setMesgFunc('neu:storm', self._onNeuStormMesg )

        self.on('neu:link:init', self._onNeuLinkInit)
        self.on('neu:link:fini', self._onNeuLinkFini)

        self.share('neuron', self)

        self.hasopt = {}

        # fire any persistent neuron listeners
        for url in self.core.getTufoList(self.neur, 'listen'):
            try:
                self.listen(url)
                self.hasopt[('listen', url)] = True
            except Exception as e:
                logger.error('neu listen: %s', e)

        # spin up any persistent neuron connections
        for url in self.core.getTufoList(self.neur, 'connect'):
            try:
                self.connect(url)
                self.hasopt[('connect', url)] = True
            except Exception as e:
                logger.error('neu connect: %s', e)

        # load any persistent shared objects
        for jsval in self.core.getTufoList(self.neur, 'shared'):

            try:
                info = json.loads(v)

                name = info.get('name')
                task = info.get('task')
                tags = info.get('tags', ())

                item = s_dyndeps.runDynTask(task)

                self.share(name, item, tags=tags)

            except Exception as e:
                logger.error('neu share: %s', e)
Beispiel #8
0
    def __init__(self, core=None, pool=None):

        s_daemon.Daemon.__init__(self, core=core, pool=pool)

        self.sched = s_sched.Sched()
        self.model = self.core.genDataModel()

        self.model.addTufoForm('neuron')

        self.model.addTufoProp('neuron','name')
        self.model.addTufoProp('neuron','super',ptype='int',defval=0)
        self.model.addTufoProp('neuron','usepki', ptype='bool', defval=0)

        self.neur = self.core.formTufoByProp('neuron','self')
        self.iden = self.neur[0]

        self.mesh = {}
        self.peers = {}

        self.mesh['certs'] = {}
        self.mesh['links'] = {}

        self.mesh['peers'] = { self.iden:self.neur }

        self.sockbyfrom = s_cache.Cache(maxtime=120)
        self.sockbyfrom.setOnMiss( self._getFakeSock )

        self.links = collections.defaultdict(set)

        self.linkpaths = s_cache.Cache(maxtime=30)
        self.linkpaths.setOnMiss( self._getLinkPath )

        self.setMesgFunc('peer:syn', self._onPeerSynMesg )
        self.setMesgFunc('peer:synack', self._onPeerSynAckMesg )
        self.setMesgFunc('peer:link:init', self._onPeerLinkInitMesg )

        #self.setMesgFunc('neu:peer:chal', self._onNeuPeerChal )
        #self.setMesgFunc('neu:peer:resp', self._onNeuPeerResp )

        self.setMesgFunc('neu:data', self._onNeuDataMesg )
        self.setMesgFunc('neu:storm', self._onNeuStormMesg )

        self.on('neu:link:init', self._onNeuLinkInit)
        self.on('neu:link:fini', self._onNeuLinkFini)

        self.share('neuron',self)

        self.hasopt = {}

        # fire any persistent neuron listeners
        for url in self.core.getTufoList(self.neur, 'listen'):
            try:
                self.listen(url)
                self.hasopt[ ('listen',url) ] = True
            except Exception as e:
                logger.error('neu listen: %s', e)

        # spin up any persistent neuron connections
        for url in self.core.getTufoList(self.neur, 'connect'):
            try:
                self.connect(url)
                self.hasopt[ ('connect',url) ] = True
            except Exception as e:
                logger.error('neu connect: %s', e)

        # load any persistent shared objects
        for jsval in self.core.getTufoList(self.neur, 'shared'):

            try:
                info = json.loads(v)

                name = info.get('name')
                task = info.get('task')
                tags = info.get('tags',())

                item = s_dyndeps.runDynTask(task)

                self.share(name,item,tags=tags)

            except Exception as e:
                logger.error('neu share: %s', e)