Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pyscion as sci
import pyscion as scion
import sys
import time

Expand All @@ -7,27 +7,18 @@


def main():
sci.init()
print('Local Address is {}'.format(sci.local_address()))
scion.init()
print('Local Address is {}'.format(scion.local_address()))
for dest_addr, nbytes in parse_tasks_from_stdin():
destination = "{}:12345".format(dest_addr) # send to port 12345
print(' === TASK to destination {} : {}MB ==='.format(destination, int(nbytes/1024/1024)))
paths = really_get_paths(destination)
print('Got %d paths' % len(paths))
with sci.connect(destination, paths[0]) as fd:
paths = scion.get_paths(destination)
print('Got {} paths'.format(len(paths)))
with scion.connect(destination, paths[0]) as fd:
for i in range(int(nbytes / 1000)+1):
fd.write(MTU*b'a')


def really_get_paths(destination):
# getting paths is async, so let's just retry until we get some
while True:
try:
return sci.paths(destination)
except sci.SCIONException:
time.sleep(0.1)


def parse_tasks_from_stdin():
def parse_line(line):
dest, nbytes = line.split()
Expand Down
6 changes: 5 additions & 1 deletion example/pyscion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- set_log_level(level)
- init()
- addr = local_address()
- paths = paths(destination)
- paths = get_paths(destination)
- fd = connect(destination, path)
- fd.close()
- fd.write(buffer)
Expand Down Expand Up @@ -60,3 +60,7 @@ def paths(destination):

def listen(port):
return connect(None, None)


def get_paths(destination, loop_till_have_paths=True):
return paths(destination)
2 changes: 1 addition & 1 deletion go/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def main():
print('Local Address is %s' % local_address())

destination = '17-ffaa:1:a,[127.0.0.1]:12345'
p = paths(destination)
p = get_paths(destination, loop_till_have_paths=False)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we pass False here?

print('Got %d paths:' % len(p))
for i in range(len(p)):
print('[%d] %s' % (i, str(p)))
Expand Down
51 changes: 49 additions & 2 deletions go/pyscion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
"""Rudimentary Python API for SCION: wraps a simplified Go API.

To use this, you need to build scion_api.go, which will give you a .so that
this file provides bindings for. Place both the .so and this file into the same
directory, somewhere in your Python path (e.g. into .).

The API consists of:
- set_log_level(level)
- init()
- addr = local_address()
- paths = get_paths(destination)
- fd = connect(destination, path)
- fd.close()
- fd.write(buffer)
- fd = listen(port)
- addr, n = fd.read(buffer)
"""

__all__ = ['SCIONException', 'HostInfo', 'FwdPathMeta', 'Interface', 'Path', 'connect',
'set_log_level', 'init', 'local_address', 'paths', 'listen']
'set_log_level', 'init', 'local_address', 'get_paths', 'listen']
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forgot about that, thanks!


from ctypes import *
from collections import namedtuple
Expand Down Expand Up @@ -164,7 +182,7 @@ def local_address():
_lib.Paths.restype = c_char_p
_lib.FreePathsMemory.argtypes = [POINTER(_PathReplyEntry), c_size_t]
_lib.FreePathsMemory.restype = c_char_p
def paths(destination):
def _call_paths(destination):
paths_n = c_size_t()
paths = (POINTER(_PathReplyEntry))()
err = _lib.Paths(byref(paths_n), byref(paths), _str_to_cstr(destination))
Expand Down Expand Up @@ -229,3 +247,32 @@ def listen(port):
conn = connect(None, None)
conn.fd = int(fd.value)
return conn


def get_paths(destination, loop_till_have_paths=True):
"""Returns a list of SCION Paths.

Warning: This function will change! However, using it with only the
destination (and leaving all other arguments default) will have the same
semantics, i.e. will block until it can return a non-empty list of paths to
the destination.

Getting paths in SCION can be async/non-blocking: if you pass
loop_till_have_paths=False, this function will return immediately with
whatever paths are in the cache right now. If this is the first time you're
looking up this destination, the cache may be empty (and this will return
[]).

If loop_till_have_paths=True, this function will retry until it gets
at least one path. (Note that if the destination is unreachable, this will
loop forever!)

Ideally, this function would be able to distinguish between "no paths in
cache" and "unreachable". That is a TODO.
"""
while True:
try:
return _call_paths(destination)
except SCIONException as e:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we delete the sleep here?

if not loop_till_have_paths:
raise e