diff --git a/examples/misc/starttestserver.py b/examples/misc/starttestserver.py new file mode 100644 index 0000000..600c8b3 --- /dev/null +++ b/examples/misc/starttestserver.py @@ -0,0 +1,24 @@ +"""Example file for using a directory test server to test without a TNC or radio.""" + +from packetserver.server.testserver import DirectoryTestServer +import os.path +from shutil import rmtree +import logging + +logging.basicConfig(level=logging.DEBUG) + +server_callsign = "KQ4PEC" +conn_dir = "/tmp/ts_conn_dir" +data_dir = "/tmp/tmp_ps_data" + +if os.path.isdir(conn_dir): + rmtree(conn_dir) + os.mkdir(conn_dir) +else: + os.mkdir(conn_dir) + +if not os.path.isdir(data_dir): + os.mkdir(data_dir) +ts = DirectoryTestServer(server_callsign, connection_directory=os.path.abspath(conn_dir), + data_dir=os.path.abspath(data_dir), zeo=True) +ts.start() \ No newline at end of file diff --git a/examples/misc/testclientserver.py b/examples/misc/testclientserver.py new file mode 100644 index 0000000..ae72186 --- /dev/null +++ b/examples/misc/testclientserver.py @@ -0,0 +1,69 @@ +"""Example file for using a directory test server to test without a TNC or radio.""" + +import msgpack + +from packetserver.common import Request, Response, Message +from packetserver.common.testing import DirectoryTestServerConnection, SimpleDirectoryConnection +from packetserver.server.testserver import DirectoryTestServer +from packetserver.client.testing import TestClient +from packetserver.server.objects import Object +from packetserver.server.messages import Message as Mail +from packetserver.server.messages import Attachment +import time +import logging +import json +import os +import os.path +from shutil import rmtree + +#logging.basicConfig(level=logging.DEBUG) + +server_callsign = "KQ4PEC" +client_callsign = 'KQ4PEC-7' +#client_callsign = "TEST1" +conn_dir = "/tmp/ts_conn_dir" +data_dir = "/tmp/tmp_ps_data" + +if os.path.isdir(conn_dir): + rmtree(conn_dir) + os.mkdir(conn_dir) +else: + os.mkdir(conn_dir) + +if not os.path.isdir(data_dir): + os.mkdir(data_dir) +tc = TestClient(os.path.abspath(conn_dir), client_callsign) +ts = DirectoryTestServer(server_callsign, connection_directory=os.path.abspath(conn_dir), + data_dir=os.path.abspath(data_dir), zeo=True) +ts.start() +tc.start() + +print("creating connection") +conn = tc.connection_for(server_callsign) + +print(conn.remote_callsign) +print(conn.call_to) +print(conn.call_from) +time.sleep(1) +req = Request.blank() + +#req.set_var('fetch_attachments', 1) +req.path = "" +req.method = Request.Method.GET + +#req.method=Request.Method.POST +#attach = [Attachment("test.txt", "Hello sir, I hope that this message finds you well. The other day..")] +#req.payload = Mail("Hi there from a test user!", "KQ4PEC", attachments=attach).to_dict() + + +#req.payload = Object(name="test.txt", data="hello there").to_dict() + +print("sending request") +resp = tc.send_receive_callsign(req, server_callsign) +ts.stop() +print("Waiting for server to stop.") +time.sleep(1) +response = resp +#print(type(response.payload)) +#print(f"Response: {response}: {response.payload}") +print(json.dumps(response.payload, indent=4)) diff --git a/src/packetserver/client/cli/__init__.py b/src/packetserver/client/cli/__init__.py index e956a08..f2f69ae 100644 --- a/src/packetserver/client/cli/__init__.py +++ b/src/packetserver/client/cli/__init__.py @@ -83,7 +83,11 @@ def cli(ctx, conf, server, agwpe, port, callsign, keep_log): storage = ZODB.FileStorage.FileStorage(os.path.join(cfg['cli']['directory'], DEFAULT_DB_FILE)) db = ZODB.DB(storage) - client = Client(ctx.obj['agwpe_server'], ctx.obj['port'], ctx.obj['callsign'], keep_log=ctx.obj['keep_log']) + if 'TEST_SERVER_DIR' in os.environ: + from packetserver.client.testing import TestClient + client = TestClient(os.environ['TEST_SERVER_DIR'], ctx.obj['callsign']) + else: + client = Client(ctx.obj['agwpe_server'], ctx.obj['port'], ctx.obj['callsign'], keep_log=ctx.obj['keep_log']) try: client.start() except Exception as e: diff --git a/src/packetserver/client/testing.py b/src/packetserver/client/testing.py index 2431082..cd5a240 100644 --- a/src/packetserver/client/testing.py +++ b/src/packetserver/client/testing.py @@ -9,6 +9,7 @@ import ax25 from threading import Lock import logging import os.path +import datetime from shutil import rmtree class TestClient(Client): @@ -55,7 +56,12 @@ class TestClient(Client): def receive(self, req: Request, conn: Union[PacketServerConnection,SimpleDirectoryConnection], timeout: int = 300): if type(conn) is SimpleDirectoryConnection: - conn.check_for_data() + time.sleep(1) + cutoff_date = datetime.datetime.now() + datetime.timedelta(seconds=timeout) + while datetime.datetime.now() < cutoff_date: + logging.debug(f"Client {self.callsign} checking for connection conn {conn}") + if conn.check_for_data(): + break return super().receive(req, conn, timeout=timeout) def clear_connections(self): @@ -71,8 +77,9 @@ class TestClient(Client): time.sleep(.5) pass - def start(self): # TODO - pass + def start(self): + self.started = True - def stop(self): # TODO - pass \ No newline at end of file + def stop(self): + self.clear_connections() + self.started = False \ No newline at end of file diff --git a/src/packetserver/common/testing.py b/src/packetserver/common/testing.py index 21d963a..1cbb7c1 100644 --- a/src/packetserver/common/testing.py +++ b/src/packetserver/common/testing.py @@ -252,14 +252,17 @@ class SimpleDirectoryConnection: except msgpack.OutOfData as e: pass - def check_for_data(self): + def check_for_data(self) -> bool: """Monitors connection directory for data.""" if self.closing: self._state = ConnectionState.DISCONNECTED if self.check_closed(): - return + return False if os.path.isfile(self.remote_file_path): data = open(self.remote_file_path, 'rb').read() os.remove(self.remote_file_path) logging.debug(f"[SIMPLE] {self.local_callsign} detected data from {self.remote_callsign}: {data}") self.data.feed(data) + return True + else: + return False diff --git a/src/packetserver/server/requests.py b/src/packetserver/server/requests.py index 39b5fb7..42f2c14 100644 --- a/src/packetserver/server/requests.py +++ b/src/packetserver/server/requests.py @@ -1,5 +1,5 @@ """Module for handling requests as they arrive to connection objects and servers.""" - +import ax25 from msgpack.exceptions import OutOfData from packetserver.common import Message, Request, Response, PacketServerConnection, send_response, send_blank_response from .bulletin import bulletin_root_handler @@ -28,10 +28,11 @@ def handle_root_get(req: Request, conn: PacketServerConnection, jobs_enabled = storage.root.config['jobs_enabled'] logging.debug(f"Root handler retrieved config. {operator} - {motd}") logging.debug("Running user_authorized") + base = ax25.Address(conn.remote_callsign).call if user_authorized(conn, db): - user_message = f"User {conn.remote_callsign} is enabled." + user_message = f"User {base} is enabled." else: - user_message = f"User {conn.remote_callsign} is not enabled." + user_message = f"User {base} is not enabled." logging.debug(f"User authorized: {user_message}") response.payload = { 'operator': operator,