Skip to content

Commit

Permalink
Added new local network pynqp2p library
Browse files Browse the repository at this point in the history
  • Loading branch information
rzampino-xilinx committed Aug 4, 2021
1 parent edb856b commit c011146
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 35 deletions.
2 changes: 2 additions & 0 deletions bootcamp_sessions/pynq-p2p/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nohup.out
__pycache__
111 changes: 79 additions & 32 deletions bootcamp_sessions/pynq-p2p/pynqp2p/__init__.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,97 @@
from multiprocessing import Process
from pathlib import Path
import os
import shutil
import requests
import getmac
import pathlib

TEMP_FILE = '/tmp/pynqp2p_running'
LOG_FOLDER = '/tmp/pynqp2p_logs'

def register(ip, key):
ip_filename = pathlib.PurePath(pathlib.Path.home(), 'pynqp2p-ip')
key_filename = pathlib.PurePath(pathlib.Path.home(), 'pynqp2p-key')
def launch():
# check for temporary file
# if temp file exists, return error

with open(ip_filename, 'w+') as ip_file:
ip_file.write(ip)

with open(key_filename, 'w+') as key_file:
key_file.write(key)
temp_file = Path(TEMP_FILE)
if temp_file.is_file():
raise RuntimeError('pynqp2p already running')

# remove log folder
try:
shutil.rmtree(LOG_FOLDER)
except FileNotFoundError:
pass

def get_ip():
ip_filename = pathlib.PurePath(pathlib.Path.home(), 'pynqp2p-ip')

with open(ip_filename, 'r') as ip_file:
return ip_file.read()
# launch background listener
current_folder = Path(__file__).parent.resolve()

server_script = Path(current_folder, "server.py")

def get_key():
key_filename = pathlib.PurePath(pathlib.Path.home(), 'pynqp2p-key')

with open(key_filename, 'r') as key_file:
return key_file.read()
os.system(f'nohup python3 {server_script} &')

temp_file.touch()

def reset():
# kill background listener
os.system(f'pkill -9 -f server.py')

def get_id():
return getmac.get_mac_address(interface='eth0')
# delete temporary files
try:
os.remove(TEMP_FILE)
except:
pass

try:
shutil.rmtree(LOG_FOLDER)
except FileNotFoundError:
pass

def ping():
r = requests.post(f'http://{get_ip()}/ping', data = {'key': get_key(), 'id': get_id()})
return r.text

def send(send_id, message):
r = requests.post(f'http://{get_ip()}/send', data = {'key': get_key(), 'id': send_id,
'message': message})
return r.text
def send(target_ip, message):
# post message to target
r = requests.post(f'http://{target_ip}:9001', json = {'data': message})


def receive():
r = requests.get(f'http://{get_ip()}/receive', data = {'key': get_key(), 'id': get_id()})
return r.text
# get sorted list of message files
logs = sorted(os.listdir(LOG_FOLDER))
# if no files, error
if len(logs) == 0:
raise RuntimeError('No messages are waiting to be recieved')

# get oldest log file
current_log_file = Path(LOG_FOLDER, logs[0])
current_log = open(current_log_file)

# get file contents
message = current_log.read()

# delet file
os.remove(current_log_file)

return message

def receive_all():
r = requests.get(f'http://{get_ip()}/receive_all', data = {'key': get_key(), 'id': get_id()})
return r.text.split('\n')[:-1]
# get sorted list of all mesagge log files
logs = sorted(os.listdir(LOG_FOLDER))
if len(logs) == 0:
raise RuntimeError('No messages are waiting to be recieved')

messages = []

# read all files and store in array
for log in logs:
current_log_file = Path(LOG_FOLDER, log)
current_log = open(current_log_file)
messages.append(current_log.read())
os.remove(current_log_file)

return messages

def clear():
# delet all logfiles in folder
for log in os.listdir(LOG_FOLDER):
try:
os.remove(log)
except:
pass
38 changes: 38 additions & 0 deletions bootcamp_sessions/pynq-p2p/pynqp2p/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from pathlib import Path

LOG_PATH = '/tmp/pynqp2p_logs'
os.mkdir(LOG_PATH)
message_counter = 0

class MyHandler(BaseHTTPRequestHandler):
def do_POST(self):
cont_length = int(self.headers['Content-Length']) # <--- Gets the size of data
body = self.rfile.read(cont_length).decode('UTF-8') # <--- Gets the data itself
body_dict = json.loads(body)

global message_counter

log_file = Path(LOG_PATH, f'{message_counter}.log')
message_counter += 1

log = open(log_file, 'w')

log.write(body_dict["data"])
log.flush()
log.close()

self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()


def run_webserver(server_class=HTTPServer, handler_class=MyHandler):
server_address = ('', 9001)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()


run_webserver()
1 change: 0 additions & 1 deletion bootcamp_sessions/pynq-p2p/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
getmac
requests
4 changes: 2 additions & 2 deletions bootcamp_sessions/pynq-p2p/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

setuptools.setup(
name="pynqp2p",
version="0.0.1",
version="0.2.0",
author="Xilinx Inc.",
description="Client side functions for pynqp2p",
description="Board to Board Messaging for PYNQ",
packages=setuptools.find_packages(),
python_requires='>=3.6',
)

0 comments on commit c011146

Please sign in to comment.