Example #1
0
	def _set_pling(self):
		timeout = unixtime(self.end) - unixtime(now(self.force))
		if timeout <= 0.1:
			timeout = 0.1
		if self._plinger:
			self._plinger.cancel()
		self._plinger = callLater(self.force, timeout, self._pling)
Example #2
0
	def maybe_up(self, resync = False):
		if self.running not in ("off","error"):
			return
		if self.last is not None:
			self.running = "next"
			self.next = time_delta(self.interval, now=self.last)
			self.waiter = callLater(False, self.next, self.do_pre)
Example #3
0
	def up(self, resync=False):
		if self.running not in ("off","error"):
			if not resync:
				raise AlreadyRunningError(self)
		if resync:
			self.do_sync()
		else:
			self.running = "next"
			self.next = time_delta(self.interval, now=self.last)
			self.waiter = callLater(False, self.next, self.do_pre)
Example #4
0
	def do_post(self):
		self.slotter = None
		if self.running != "during" or self.waiter is not None:
			log(ERROR,"timeslot error post",self.running,*self.name)
			return

		self.running = "next"
		simple_event("timeslot","end",*self.name)
		self.next = time_delta(self.interval, now=self.next)-dt.timedelta(0,self.duration)
		self.waiter = callLater(False, self.next, self.do_pre)
Example #5
0
	def do_pre(self):
		self.waiter = None
		if self.running != "next" or self.slotter is not None:
			log(ERROR,"timeslot error pre",self.running,*self.name)
			return

		if self.next is None:
			self.next = now()
		self.last = self.next

		self.running = "during"
		simple_event("timeslot","begin",*self.name)
		self.next += dt.timedelta(0,self.duration)
		self.slotter = callLater(False,self.next, self.do_post)
Example #6
0
	def set_value(self,val=None):
		if val is None: val = self._value
		assert 0<=val<=1, u"Value is '%s', not in 0…1" % (val,)

		do = self.t_on if self.state else self.t_off
		self.t_off,self.t_on = self.new_value(val)
		dn = self.t_on if self.state else self.t_off

		self._value = val

		if do != dn:
			if self.timer is not None:
				self.timer.cancel()
			if dn is not None:
				self.next = (self.last if self.last is not None else now()) + dt.timedelta(0,dn)
				self.timer = callLater(False,self.next,self.do_timed_switch)
Example #7
0
	def do_switch(self):
		"""Click"""
		if self.state:
			self.state = 0
			tn = self.t_off
		else:
			self.state = 1
			tn = self.t_on

		process_event(Event(self.ctx,"pcm","set",self.names[self.state],*self.name))
		try:
			self.last = self.next
			if tn is not None:
				self.next = self.last + dt.timedelta(0,tn)
				self.timer = callLater(False,self.next,self.do_timed_switch)
			else:
				self.next = None
		except Exception as e:
			fix_exception(e)
			process_failure(e)
			simple_event(self.ctx,"pcm","error",*self.name)
Example #8
0
	
def looper():
	while True:
		print "L"
		sleep(1)

def ready():
	c=Context()
	for f in sys.argv[1:]:
		read_config(c,f)
		parse(f, interpreter, ctx=c)
	
	#c.logger=parse_logger
	if os.isatty(0):
		i = InteractiveInterpreter
	else:
		i = Interpreter
	print """Ready. Type «help» if you don't know what to do."""
	try:
		parse(sys.stdin, interpreter=i, ctx=c)
	except Exception as e:
		fix_exception(e)
		reporter(e)
	shut_down()

from gevent import spawn,sleep
#spawn(looper)
callLater(False,0.1,ready)
mainloop()

Example #9
0
	def _start_timer(self):
		if self.timer is not None:
			self.timer = callLater(True,self.timeout, self.no_data)
Example #10
0
	def do_restart(self):
		if not self.stopped:
			callLater(True,5,self.do_start)
Example #11
0
	def _start(self):
		if self._timer:
			self._timer.cancel()
		self.started = now()
		self._timer = callLater(False,self.end,self._timeout)
Example #12
0
	def do_sync(self):
		self.down()
		self.running = "during"
		self.next = now()+dt.timedelta(0,self.duration/2)
		self.slotter = callLater(False,self.next, self.do_post)
Example #13
0
    def _handler(self):
        """\
			This is the receiver's main loop.

			Processing of incoming and outgoing data is serialized so that
			there will be no problems with concurrency.
			"""

        def doReOpen():
            m = MsgReOpen()
            if self.q is not None:
                self.q.put(m, block=False)

        state = "open" if self.state == "connected" else "closed" if self.channel is None else "connecting"
        log("conn", TRACE, "setstate init %s" % (state,))
        self.connect_timeout = self.initial_connect_timeout
        self.attempts = 0

        if not self.ondemand and state != "open":
            doReOpen()

        while True:
            msg = self.q.get()

            if isinstance(msg, MsgSender):
                self.senders[msg.prio].append(msg)
            elif isinstance(msg, MsgReceiver):
                if msg.blocking:
                    self.receivers[msg.prio].insert(0, msg)
                else:
                    self.receivers[msg.prio].append(msg)
            elif isinstance(msg, MsgIncoming):
                self._incoming(msg)
            elif isinstance(msg, MsgOpenMarker):
                log("conn", TRACE, "setstate %s %s" % (state, "connected"))
                state = "connected"

                self.connect_timeout = self.initial_connect_timeout
                self.attempts = 0

            elif isinstance(msg, MsgReOpen):
                if self.channel is None:
                    log("conn", TRACE, "setstate %s %s" % (state, "want"))
                    state = "want"

            elif isinstance(msg, MsgClosed):
                if self.channel is not None:
                    if state != "waiting" and state != "connecting":
                        log("conn", TRACE, "setstate2 %s %s" % (state, "closed"))
                        state = "closed"
                    self._teardown("ReOpen", external=False)
                if state == "closed" or state == "connecting":
                    log("conn", TRACE, "setstate %s %s: wait %.3f" % (state, "waiting", self.connect_timeout))
                    state = "waiting"
                    callLater(True, self.connect_timeout, doReOpen)
                    self._up_timeout()

            elif isinstance(msg, MsgError):
                self._error(msg.error)
            else:
                raise UnknownMessageType(msg)

            if self.ondemand and not self.n_outq:
                continue
            if state == "want" or state == "closed" and self.ondemand and self.n_outq:
                log("conn", TRACE, "setstate %s %s" % (state, "connecting"))
                state = "connecting"
                self._setup()
            if self.state != "connected":
                continue

            log("msg", TRACE, "states at run", self.state, state)
            done = False  # marker for "don't send any more stuff"

            for mq in self.receivers:
                if done:
                    break
                for m in mq:
                    if m.blocking:
                        log("msg", TRACE, "blocked by", str(m))
                        done = True
            if done:
                continue

            for mq in self.senders:
                if done:
                    break
                while len(mq):
                    if done:
                        break
                    if self.channel is None:
                        break
                    if self.max_open >= self.is_open:
                        break

                    msg = mq.pop(0)
                    log("msg", TRACE, "send", str(msg))
                    try:
                        r = msg.send(self.channel)
                    except Exception as ex:
                        fix_exception(ex)
                        r = ex
                    else:
                        self.last_sent = msg
                        self.last_sent_at = now()
                        self.n_sent_now += 1
                    log("msg", TRACE, "send result", r)
                    if r is RECV_AGAIN:
                        if msg.blocking:
                            self.receivers[msg.prio].insert(0, msg)
                        else:
                            self.receivers[msg.prio].append(msg)
                    elif r is SEND_AGAIN:
                        if msg.blocking:
                            self.senders[msg.prio].insert(0, msg)
                        else:
                            self.senders[msg.prio].append(msg)
                    elif isinstance(r, SEND_LATER):
                        raise NotImplementedError("Queueing doesn't work yet")
                    elif isinstance(r, MSG_ERROR):
                        try:
                            raise r
                        except Exception as r:
                            fix_exception(r)
                            process_failure(r)
                    elif isinstance(r, Exception):
                        reraise(r)
                    else:
                        msg.done()

                    if msg.blocking:
                        done = True
                        break

                        # while setting up, only process PRIO_CONNECT messages
                if state != "connected":
                    log(TRACE, "NotConn", self.senders)
                    break
Example #14
0
 def arm(self, sending):
     if self.timer is None:
         self.timer = callLater(True, self.reply_timeout, self.send_me)
Example #15
0
 def _set_timeout(self):
     if self.timeout is not None:
         self._timer = callLater(True, self.timeout, self._timeout)