| … | … | @@ -27,10 +27,6 @@ if _servernode_lib is None: |
| 27 | 27 | from opencog.type_constructors import get_thread_atomspace, FloatValue, VoidValue, Predicate |
| 28 | 28 | from opencog.atomspace import types |
| 29 | 29 | |
| 30 | # Global server state | |
| 31 | _server_handle = None | |
| 32 | _server_running = False | |
| 33 | ||
| 34 | 30 | def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888, |
| 35 | 31 | enable_console=True, enable_web=True, enable_mcp=True): |
| 36 | 32 | """Start the CogServer with the specified configuration. |
| … | … | @@ -47,14 +43,8 @@ def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888, |
| 47 | 43 | CogServerNode: The CogServerNode atom that was created. |
| 48 | 44 | |
| 49 | 45 | Raises: |
| 50 | RuntimeError: If server is already running. | |
| 51 | 46 | ValueError: If invalid ports are specified. |
| 52 | 47 | """ |
| 53 | global _server_handle, _server_running | |
| 54 | ||
| 55 | if _server_running: | |
| 56 | raise RuntimeError("CogServer is already running") | |
| 57 | ||
| 58 | 48 | # Validate ports |
| 59 | 49 | for port, pname in [(console_port, "console"), (web_port, "web"), (mcp_port, "mcp")]: |
| 60 | 50 | if not (1 <= port <= 65535): |
| … | … | @@ -70,42 +60,28 @@ def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888, |
| 70 | 60 | |
| 71 | 61 | # Get the thread atomspace and add the CogServerNode to it |
| 72 | 62 | atomspace = get_thread_atomspace() |
| 73 | _server_handle = atomspace.add_node(types.CogServerNode, name) | |
| 63 | server_handle = atomspace.add_node(types.CogServerNode, name) | |
| 74 | 64 | |
| 75 | 65 | if effective_console != 17001: |
| 76 | 66 | key = Predicate("*-telnet-port-*") |
| 77 | atomspace.set_value(_server_handle, key, FloatValue(float(effective_console))) | |
| 67 | atomspace.set_value(server_handle, key, FloatValue(float(effective_console))) | |
| 78 | 68 | if effective_web != 18080: |
| 79 | 69 | key = Predicate("*-web-port-*") |
| 80 | atomspace.set_value(_server_handle, key, FloatValue(float(effective_web))) | |
| 70 | atomspace.set_value(server_handle, key, FloatValue(float(effective_web))) | |
| 81 | 71 | if effective_mcp != 18888: |
| 82 | 72 | key = Predicate("*-mcp-port-*") |
| 83 | atomspace.set_value(_server_handle, key, FloatValue(float(effective_mcp))) | |
| 73 | atomspace.set_value(server_handle, key, FloatValue(float(effective_mcp))) | |
| 84 | 74 | |
| 85 | 75 | # Start all servers (this also starts the server thread) |
| 86 | atomspace.set_value(_server_handle, Predicate("*-start-*"), VoidValue()) | |
| 76 | atomspace.set_value(server_handle, Predicate("*-start-*"), VoidValue()) | |
| 87 | 77 | |
| 88 | # Mark as initialized | |
| 89 | _server_running = True | |
| 78 | return server_handle | |
| 90 | 79 | |
| 91 | return _server_handle | |
| 80 | def stop_cogserver(server_node): | |
| 81 | """Stop the specified CogServer. | |
| 92 | 82 | |
| 93 | def stop_cogserver(): | |
| 94 | """Stop the running CogServer. | |
| 95 | ||
| 96 | Returns: | |
| 97 | bool: True if server was stopped, False if server was not running. | |
| 83 | Args: | |
| 84 | server_node: The CogServerNode to stop. | |
| 98 | 85 | """ |
| 99 | global _server_handle, _server_running | |
| 100 | ||
| 101 | if not _server_running or _server_handle is None: | |
| 102 | return False | |
| 103 | ||
| 104 | # Stop the server | |
| 105 | 86 | atomspace = get_thread_atomspace() |
| 106 | atomspace.set_value(_server_handle, Predicate("*-stop-*"), VoidValue()) | |
| 107 | ||
| 108 | _server_handle = None | |
| 109 | _server_running = False | |
| 110 | ||
| 111 | return True | |
| 87 | atomspace.set_value(server_node, Predicate("*-stop-*"), VoidValue()) |
| … | … | @@ -46,7 +46,7 @@ class TestCogServer(unittest.TestCase): |
| 46 | 46 | """Ensure server is stopped before each test.""" |
| 47 | 47 | global _current_server |
| 48 | 48 | if _current_server is not None and is_server_running(_current_server): |
| 49 | stop_cogserver() | |
| 49 | stop_cogserver(_current_server) | |
| 50 | 50 | _current_server = None |
| 51 | 51 | # Small delay to ensure clean state |
| 52 | 52 | time.sleep(0.1) |
| … | … | @@ -55,7 +55,7 @@ class TestCogServer(unittest.TestCase): |
| 55 | 55 | """Ensure server is stopped after each test.""" |
| 56 | 56 | global _current_server |
| 57 | 57 | if _current_server is not None and is_server_running(_current_server): |
| 58 | stop_cogserver() | |
| 58 | stop_cogserver(_current_server) | |
| 59 | 59 | _current_server = None |
| 60 | 60 | # Small delay to ensure clean shutdown |
| 61 | 61 | time.sleep(0.1) |
| … | … | @@ -73,8 +73,7 @@ class TestCogServer(unittest.TestCase): |
| 73 | 73 | "Server should be running after start") |
| 74 | 74 | |
| 75 | 75 | # Stop the server |
| 76 | result = stop_cogserver() | |
| 77 | self.assertTrue(result, "stop_cogserver should return True") | |
| 76 | stop_cogserver(_current_server) | |
| 78 | 77 | |
| 79 | 78 | # Check server is stopped |
| 80 | 79 | self.assertFalse(is_server_running(_current_server), |
| … | … | @@ -102,27 +101,10 @@ class TestCogServer(unittest.TestCase): |
| 102 | 101 | time.sleep(0.5) |
| 103 | 102 | |
| 104 | 103 | # Stop the server |
| 105 | stop_cogserver() | |
| 104 | stop_cogserver(_current_server) | |
| 106 | 105 | self.assertFalse(is_server_running(_current_server), |
| 107 | 106 | "Server should be stopped") |
| 108 | 107 | |
| 109 | def test_double_start_error(self): | |
| 110 | """Test that starting server twice raises error.""" | |
| 111 | global _current_server | |
| 112 | ||
| 113 | # Start server | |
| 114 | _current_server = start_cogserver(console_port=17553, enable_web=False, enable_mcp=False) | |
| 115 | self.assertTrue(is_server_running(_current_server), "Server should be running") | |
| 116 | ||
| 117 | # Try to start again - should raise RuntimeError | |
| 118 | with self.assertRaises(RuntimeError) as context: | |
| 119 | start_cogserver() | |
| 120 | ||
| 121 | self.assertIn("already running", str(context.exception)) | |
| 122 | ||
| 123 | # Clean up | |
| 124 | stop_cogserver() | |
| 125 | ||
| 126 | 108 | def test_invalid_port_error(self): |
| 127 | 109 | """Test that invalid port raises error.""" |
| 128 | 110 | # Try invalid port - should raise ValueError |
| … | … | @@ -132,14 +114,6 @@ class TestCogServer(unittest.TestCase): |
| 132 | 114 | self.assertIn("Invalid", str(context.exception)) |
| 133 | 115 | self.assertIn("port", str(context.exception)) |
| 134 | 116 | |
| 135 | def test_stop_when_not_running(self): | |
| 136 | """Test stopping server when not running.""" | |
| 137 | # Try to stop - should return False | |
| 138 | result = stop_cogserver() | |
| 139 | self.assertFalse(result, | |
| 140 | "stop_cogserver should return False when not running") | |
| 141 | ||
| 142 | ||
| 143 | 117 | def test_multiple_start_stop_cycles(self): |
| 144 | 118 | """Test multiple start/stop cycles.""" |
| 145 | 119 | global _current_server |
| … | … | @@ -153,8 +127,7 @@ class TestCogServer(unittest.TestCase): |
| 153 | 127 | f"Server should be running on cycle {i}") |
| 154 | 128 | |
| 155 | 129 | # Stop server |
| 156 | result = stop_cogserver() | |
| 157 | self.assertTrue(result, f"Server should stop on cycle {i}") | |
| 130 | stop_cogserver(_current_server) | |
| 158 | 131 | self.assertFalse(is_server_running(_current_server), |
| 159 | 132 | f"Server should be stopped on cycle {i}") |
| 160 | 133 |