Hello Guys,
I'm a bit in trouble with Ryu thread system and i need some help. i did 2
ryuapp but it seems that the first one doesn't yield or yield impeerfectly.
Could you hrlp me to understand what happens....
import logging
from ryu.base import app_manager
from ryu.controller import event as ryu_event
from ryu.ofproto import ofproto_v1_0
from ryu.lib import hub
class TimerEvent(ryu_event.EventBase):
def __init__(self, msg):
super(TimerEvent, self).__init__()
self.msg = msg
class TimerRyuApp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
_EVENTS = [
TimerEvent
]
"""
Syncrepl Consumer interface
"""
def __init__(self,*args,**kwargs):
super(TimerRyuApp,self).__init__(*args,**kwargs)
# self.is_active = False
# self.start()
# We need this for later internal use
def shutdown(self):
self.RunningState = False
def main_timer(self,duration):
while True:
try:
while self.is_active:
self.send_event_to_observers(TimerEvent('Timer Event'))
hub.sleep(duration)
except KeyboardInterrupt:
# User asked to exit
self.shutdown()
pass
except Exception as e:
# Handle any exception
print(e)
pass
hub.sleep(1)
def start(self):
super(TimerRyuApp,self).start()
self.main_thread = hub.spawn(self.main_timer(5))
The second one is based on simple_switch.py
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
An OpenFlow 1.0 L2 learning switch implementation.
"""
import threading
import ryu.app.event_sender4
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
from event_sender4 import TimerEvent
from ryu.ofproto import ofproto_v1_0
from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
from ryu.lib import hub
class SimpleSwitch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
def add_flow(self, datapath, in_port, dst, src, actions):
ofproto = datapath.ofproto
match = datapath.ofproto_parser.OFPMatch(
in_port=in_port,
dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
mod = datapath.ofproto_parser.OFPFlowMod(
datapath=datapath, match=match, cookie=0,
command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
priority=ofproto.OFP_DEFAULT_PRIORITY,
flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
msg = ev.msg
self.logger.info("Config Event: %s", msg)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
pkt = packet.Packet(msg.data)
eth = pkt.get_protocol(ethernet.ethernet)
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = msg.in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
self.add_flow(datapath, msg.in_port, dst, src, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions, data=data)
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
msg = ev.msg
reason = msg.reason
port_no = msg.desc.port_no
ofproto = msg.datapath.ofproto
if reason == ofproto.OFPPR_ADD:
self.logger.info("port added %s", port_no)
elif reason == ofproto.OFPPR_DELETE:
self.logger.info("port deleted %s", port_no)
elif reason == ofproto.OFPPR_MODIFY:
self.logger.info("port modified %s", port_no)
else:
self.logger.info("Illegal port state %s %s", port_no, reason)
@set_ev_cls(TimerEvent,MAIN_DISPATCHER)
def _TimerEventHandler(self, ev):
self.logger.info('Timer Event Received: %s', ev.msg)
Here is the output:
ssh://[email protected]:2023/usr/bin/python3 -u ./ryu-manager simple_switch3
event_sender4
loading app simple_switch3
loading app event_sender4
loading app ryu.controller.ofp_handler
instantiating app simple_switch3 of SimpleSwitch
instantiating app event_sender4 of TimerRyuApp
instantiating app ryu.controller.ofp_handler of OFPHandler
Timer Event Received: Timer Event
Timer Event Received: Timer Event
Timer Event Received: Timer Event
Timer Event Received: Timer Event
Timer Event Received: Timer Event
^CTimer Event Received: Timer Event
Nothing happens in thr ryu contoller ofp handler as if the event_sender app
doent yield....
Could you help?
Yours
Tim
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel