Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 165 lines (124 sloc) 4.35 KB
#!/usr/bin/env python
# Written by: Vincenzo Maffione <v.maffione@gmail.com>
import argparse
import select
import rina
import sys
import os
def python2():
return sys.version_info[0] <= 2
def str2buf(s):
if python2():
return s
return bytes(s, 'ascii')
def buf2str(b):
if python2():
return b
return b.decode('ascii')
class RinaRRTool:
def __init__(self):
self.cfd = rina.open()
if self.cfd < 0:
raise Exception("Could not open rina ctrl device")
self.client_name = ''
self.server_name = ''
self.dif = ''
self.flow_spec = rina.flow_spec()
rina.flow_spec_unreliable(self.flow_spec)
def __del__(self):
os.close(self.cfd)
def client(self, args):
fd = rina.flow_alloc(self.dif, self.client_name,
self.server_name, self.flow_spec, 0)
if fd < 0:
print("Flow allocation failed")
return fd
if args.stream:
# download a large file from the server
tot = 0
while 1:
data = os.read(fd, 4096)
if not data:
break
tot += len(data)
print('received %d bytes' % (tot,))
else:
# echo transaction
msg = 'Hello guys, this is a test message!'
n = os.write(fd, str2buf(msg))
if n != len(msg):
print("Partial write %s/%s", n, len(msg))
r, w, e = select.select([fd], [], [], 3000)
if len(r) == 0:
print("Timeout")
return -1
buf = os.read(fd, 65535)
print("Response: '%s'" % buf2str(buf))
os.close(fd)
return 0
def server(self, args):
ret = rina.register(self.cfd, self.dif, self.server_name, 0)
if ret:
print('Registration failed')
return ret
while 1:
spec = rina.flow_spec()
fd = rina.flow_accept(self.cfd, None, spec, 0)
if fd < 0:
continue
print('Accepted flow, msg_boundaries %s' % (spec.msg_boundaries,))
if not spec.msg_boundaries:
# server sends a large file to the client
left = 500000000 # about 500 MB
chunk = 31000
buf = bytearray(chunk)
while left:
if left < chunk:
buf = bytearray(left)
n = os.write(fd, buf)
left -= n
else:
# echo transaction
r, w, e = select.select([fd], [], [], 3000)
if len(r) == 0:
print("Timeout")
return -1
buf = os.read(fd, 65535)
print("Request: '%s'" % buf2str(buf))
n = os.write(fd, buf)
if n != len(buf):
print("Partial write %s/%s", n, len(buf))
os.close(fd)
return 0
description = "RINA echo client/server (raw API version)"
epilog = "2015-2016 Vincenzo Maffione <v.maffione@gmail.com>"
argparser = argparse.ArgumentParser(description = description,
epilog = epilog)
argparser.add_argument('-l', '--listen', dest = 'server_mode',
action='store_true',
help = "Run in server mode")
argparser.add_argument('-d', '--dif',
help = "DIF to register to or ask for flow allocation",
type = str)
argparser.add_argument('-a', '--client-name', help = "client name",
type = str, default='rl_rr-data|client')
argparser.add_argument('-z', '--server-name', help = "server name",
type = str, default='rl_rr-data|server')
argparser.add_argument('-S', '--stream', action='store_true',
help = "Run in stream download mode")
args = argparser.parse_args()
try:
rr = RinaRRTool()
rr.client_name = args.client_name
rr.server_name = args.server_name
rr.dif = args.dif
if args.stream:
rr.flow_spec.max_sdu_gap = 0
rr.flow_spec.in_order_delivery = 1
rr.flow_spec.msg_boundaries = 0
if args.server_mode:
rr.server(args)
else:
rr.client(args)
except KeyboardInterrupt:
pass