diff options
author | YAMADA Hideki <yamada.hideki@po.ntts.co.jp> | 2014-05-14 19:11:02 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-05-16 05:03:11 +0900 |
commit | eb0055f0ce1bff85ecfbe63ac32cebd03436a97c (patch) | |
tree | a22d553ca035b5165af43e033c42195aa2b8aa22 | |
parent | 7b6f6388741111482aad8d27c39480ac683685e1 (diff) |
Add PacketIn dumper websocket application
Signed-off-by: YAMADA Hideki <yamada.hideki@po.ntts.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/simple_switch_websocket_13.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/ryu/app/simple_switch_websocket_13.py b/ryu/app/simple_switch_websocket_13.py new file mode 100644 index 00000000..fa68fc5a --- /dev/null +++ b/ryu/app/simple_switch_websocket_13.py @@ -0,0 +1,91 @@ +# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation. +# +# 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. + +""" +Usage example + +Run this application: +$ PYTHONPATH=. ./bin/ryu run --verbose ryu.app.simple_switch_websocket_13 + +Install and run websocket client(in other terminal): +$ pip install websocket-client +$ wsdump.py ws://127.0.0.1:8080/simpleswitch/ws +""" + +import json +from webob import Response + +from ryu.app import simple_switch_13 +from ryu.app.wsgi import route, ControllerBase, WSGIApplication +from ryu.controller import ofp_event +from ryu.controller.handler import set_ev_cls +from ryu.lib import hub +from ryu.lib.packet import packet + + +simple_switch_instance_name = 'simple_switch_api_app' +url = '/simpleswitch/ws' + + +class SimpleSwitchWebSocket13(simple_switch_13.SimpleSwitch13): + _CONTEXTS = { + 'wsgi': WSGIApplication, + } + + def __init__(self, *args, **kwargs): + super(SimpleSwitchWebSocket13, self).__init__(*args, **kwargs) + + self.ws_send_queue = hub.Queue() + self.ws_lock = hub.BoundedSemaphore() + + wsgi = kwargs['wsgi'] + wsgi.register( + SimpleSwitchWebSocketController, + data={simple_switch_instance_name: self}, + ) + + @set_ev_cls(ofp_event.EventOFPPacketIn) + def _packet_in_handler(self, ev): + super(SimpleSwitchWebSocket13, self)._packet_in_handler(ev) + + pkt = packet.Packet(ev.msg.data) + self.ws_send_queue.put(str(pkt)) + + +class SimpleSwitchWebSocketController(ControllerBase): + def __init__(self, req, link, data, **config): + super(SimpleSwitchWebSocketController, self).__init__( + req, link, data, **config) + self.simpl_switch_spp = data[simple_switch_instance_name] + + def _websocket_handler(self, ws): + simple_switch = self.simpl_switch_spp + simple_switch.logger.debug('WebSocket connected: %s', ws) + while True: + data = simple_switch.ws_send_queue.get() + ws.send(unicode(json.dumps(data))) + + @route('simpleswitch', url) + def websocket(self, req, **kwargs): + simple_switch = self.simpl_switch_spp + if simple_switch.ws_lock.acquire(blocking=False): + try: + self.websocket_handshake(req, self._websocket_handler) + return + finally: + simple_switch.logger.debug('WebSocket disconnected') + simple_switch.ws_lock.release() + else: + return Response(status=503) |