Esempio n. 1
0
File: core.py Progetto: voidcc/PCTRL
 def callDelayed (_self, _seconds, _func, *args, **kw):
     """
     Calls the function at a later time.
     This is just a wrapper around a recoco timer.
     """
     t = recoco.Timer(_seconds, _func, args=args, kw=kw, scheduler = _self.scheduler)
     return t
Esempio n. 2
0
    def enable_flow_stats_retrieval(self, interval_secs):
        """
		Starts a timer that periodically requests the switch to send flow statistics.
		The reply will be received through a FlowStatsReceived event.
		"""

        self._stats_req_timer = recoco.Timer(
            interval_secs,
            lambda: self._connection.send(
                of.ofp_stats_request(body=of.ofp_flow_stats_request())),
            recurring=True)
Esempio n. 3
0
def launch(bar=False):
    """
    The default launcher just logs its arguments
    """
    log.warn("Bar: %s (%s)", bar, type(bar))

    core.addListenerByName("UpEvent", _go_up)
    core.openflow_discovery.addListenerByName("LinkEvent", _handle_LinkEvent)
    core.openflow.addListenerByName("FlowStatsReceived", _handle_flowstats)
    core.openflow.addListenerByName("PortStatsReceived", _handle_portstats)

    recoco.Timer(7, _request_stats, recurring=True)
Esempio n. 4
0
 def callDelayed(_self, _seconds, _func, *args, **kw):
     """
 :param _seconds: Paramatro para a função de recoco.
 :param _func: Paramatro para a função de recoco.
 :param args: Paramatro para a função de recoco.
 :param kw: Paramatro para a função de recoco.
 :return: temporizador de recoco
 """
     """
 Calls the function at a later time.
 This is just a wrapper around a recoco timer.
 """
     t = recoco.Timer(_seconds,
                      _func,
                      args=args,
                      kw=kw,
                      scheduler=_self.scheduler)
     return t
Esempio n. 5
0
    def state(self, state):
        old = self._state

        self.log.debug("Transition: %s -> %s", old, state)

        def killtimer(name):
            name += '_timer'
            a = getattr(self, name)
            if a is not None:
                a.cancel()
            setattr(self, name, None)

        def set_state(s, debug=None, warn=None, info=None):
            def state_setter():
                if debug: self.log.debug(debug)
                if warn: self.log.debug(warn)
                if info: self.log.debug(info)
                self.state = s

            return state_setter

        if old == self.INIT:
            killtimer('discover')
        elif old == self.SELECTING:
            killtimer('offer')
        elif old == self.REQUESTING:
            killtimer('request')
            self.requested = None

        # Make sure we're seeing packets if needed...

        def get_flow(broadcast=False):
            fm = of.ofp_flow_mod()
            if broadcast:
                fm.match.dl_dst = pkt.ETHER_BROADCAST
            else:
                fm.match.dl_dst = self.port_eth
            fm.match.in_port = self.portno
            fm.match.dl_type = pkt.ethernet.IP_TYPE
            fm.match.nw_proto = pkt.ipv4.UDP_PROTOCOL
            fm.match.tp_src = pkt.dhcp.SERVER_PORT
            fm.match.tp_dst = pkt.dhcp.CLIENT_PORT
            fm.priority += 1
            return fm

        if state not in (self.IDLE, self.ERROR, self.BOUND):
            if self._packet_listener is None:
                self._packet_listener = core.openflow.addListenerByName(
                    'PacketIn', self._handle_PacketIn)
                if self.install_flows:
                    fm = get_flow(False)
                    fm.actions.append(
                        of.ofp_action_output(port=of.OFPP_CONTROLLER))
                    self._con.send(fm)
                    fm = get_flow(True)
                    fm.actions.append(
                        of.ofp_action_output(port=of.OFPP_CONTROLLER))
                    self._con.send(fm)
        else:
            if self._packet_listener is not None:
                core.openflow.removeListener(self._packet_listener)
                self._packet_listener = None
                if self.install_flows:
                    fm = get_flow(False)
                    fm.command = of.OFPFC_DELETE_STRICT
                    self._con.send(fm)
                    fm = get_flow(True)
                    fm.command = of.OFPFC_DELETE_STRICT
                    self._con.send(fm)

        self._state = state

        if state == self.INIT:
            assert old in (self.NEW, self.INIT)
            # We transition INIT->INIT when discovery times out
            if old == self.NEW:
                # In this case, we want to set a total timeout
                killtimer('total')
                self.total_timer = recoco.Timer(self.total_timeout,
                                                self._do_total_timeout)
                self._start = time.time()
            self._discover()
            self.discover_timer = recoco.Timer(self.discover_timeout,
                                               set_state(self.INIT))
        elif state == self.SELECTING:
            assert old == self.INIT
            self.offer_timer = recoco.Timer(self.offer_timeout,
                                            self._do_accept)
        elif state == self.REQUESTING:
            assert old == self.SELECTING
            assert self.requested
            self._request()
            self.request_timer = recoco.Timer(
                self.request_timeout, set_state(self.INIT, info='Timeout'))
        elif state == self.BOUND:
            killtimer('total')
            ev = DHCPLeased(self.bound)
            self.log.info("Got %s/%s -> %s", self.bound.address,
                          self.bound.subnet_mask,
                          ','.join(str(g) for g in self.bound.routers))

            self.raiseEventNoErrors(ev)
            #TODO: Handle expiring leases

        elif state == self.ERROR:
            #TODO: Error info
            self.raiseEventNoErrors(DHCPClientError())
Esempio n. 6
0
  def state (self, state):
    old = self._state

    self.log.debug("Transition: %s -> %s", old, state)

    def killtimer (name):
      name += '_timer'
      a = getattr(self, name)
      if a is not None:
        a.cancel()
      setattr(self, name, None)

    def set_state (s, debug = None, warn = None, info = None):
      def state_setter ():
        if debug: self.log.debug(debug)
        if warn: self.log.debug(warn)
        if info: self.log.debug(info)
        self.state = s
      return state_setter


    if old == self.INIT:
      killtimer('discover')
    elif old == self.SELECTING:
      killtimer('offer')
    elif old == self.REQUESTING:
      killtimer('request')
      self.requested = None

    self._state_transition(old, state)

    self._state = state

    if state == self.INIT:
      assert old in (self.NEW,self.INIT)
      # We transition INIT->INIT when discovery times out
      if old == self.NEW:
        # In this case, we want to set a total timeout
        killtimer('total')
        self.total_timer = recoco.Timer(self.total_timeout,
                                        self._do_total_timeout)
        self._start = time.time()
      self._discover()
      self.discover_timer = recoco.Timer(self.discover_timeout,
                                         set_state(self.INIT))
    elif state == self.SELECTING:
      assert old == self.INIT
      self.offer_timer = recoco.Timer(self.offer_timeout,
                                      self._do_accept)
    elif state == self.REQUESTING:
      assert old == self.SELECTING
      assert self.requested
      self._request()
      self.request_timer = recoco.Timer(self.request_timeout,
                                        set_state(self.INIT,info='Timeout'))
    elif state == self.BOUND:
      killtimer('total')
      ev = DHCPLeased(self.bound)
      routers = ','.join(str(g) for g in self.bound.routers)
      if not routers: routers = "(No routers)"
      self.log.info("Got %s/%s -> %s",
                    self.bound.address, self.bound.subnet_mask, routers)

      self.raiseEventNoErrors(ev)
      #TODO: Handle expiring leases

    elif state == self.ERROR:
      #TODO: Error info
      self.raiseEventNoErrors(DHCPClientError())