diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2014-07-30 16:03:56 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-07-30 17:41:40 +0900 |
commit | 915dd2ca82bd42ed58768cb1139ceaceada42c84 (patch) | |
tree | aa6b94dcf44073035190d9fcfafbcebf12c09b65 | |
parent | 3ead62d37f14e2490668680327f39e88bc69e94c (diff) |
app: add simple bmp station application
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/bmpstation.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/ryu/app/bmpstation.py b/ryu/app/bmpstation.py new file mode 100644 index 00000000..e90abc1f --- /dev/null +++ b/ryu/app/bmpstation.py @@ -0,0 +1,48 @@ +import socket +import logging +logging.basicConfig(level=logging.DEBUG) + +from ryu.base import app_manager + +from ryu.lib import hub +from ryu.lib.hub import StreamServer +from ryu.lib.packet.bmp import * + +SERVER_HOST = '0.0.0.0' +SERVER_PORT = 11019 + + +class BMPStation(app_manager.RyuApp): + def __init__(self): + super(BMPStation, self).__init__() + self.name = 'bmpstation' + + def start(self): + super(BMPStation, self).start() + + return hub.spawn(StreamServer((SERVER_HOST, SERVER_PORT), + self.loop).serve_forever) + + def loop(self, sock, addr): + logging.debug("started bmp loop.") + self.is_active = True + + buf = bytearray() + required_len = BMPMessage._HDR_LEN + + while self.is_active: + buf = sock.recv(BMPMessage._HDR_LEN) + if len(buf) == 0: + self.is_active = False + break + + _, len_, _ = BMPMessage.parse_header(buf) + + body = sock.recv(len_ - BMPMessage._HDR_LEN) + if len(body) == 0: + self.is_active = False + break + + msg, rest = BMPMessage.parser(buf + body) + assert len(rest) == 0 + print msg, '\n' |