forked from Xilinx/PYNQ_Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new local network pynqp2p library
- Loading branch information
1 parent
edb856b
commit c011146
Showing
5 changed files
with
121 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nohup.out | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
getmac | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters