| … | … | @@ -2,7 +2,7 @@ |
| 2 | 2 | # Install pure Python cogserver module |
| 3 | 3 | # |
| 4 | 4 | INSTALL(FILES |
| 5 | __init__.py | |
| 6 | cogserver.py | |
| 5 | opencog/__init__.py | |
| 6 | opencog/cogserver.py | |
| 7 | 7 | DESTINATION "${PYTHON_DEST}" |
| 8 | 8 | ) |
| … | … | @@ -1,16 +0,0 @@ |
| 1 | from pkgutil import extend_path | |
| 2 | __path__ = extend_path(__path__, __name__) | |
| 3 | ||
| 4 | # Import and expose cogserver functions | |
| 5 | try: | |
| 6 | from .cogserver import ( | |
| 7 | start_cogserver, | |
| 8 | stop_cogserver, | |
| 9 | ) | |
| 10 | __all__ = [ | |
| 11 | 'start_cogserver', | |
| 12 | 'stop_cogserver', | |
| 13 | ] | |
| 14 | except ImportError: | |
| 15 | # Module not fully installed yet | |
| 16 | pass |
| … | … | @@ -1,87 +0,0 @@ |
| 1 | # | |
| 2 | # cogserver.py | |
| 3 | # | |
| 4 | # Pure Python wrapper for starting/stopping the CogServer. | |
| 5 | # This module provides a convenient Python API for controlling | |
| 6 | # the CogServer network server. | |
| 7 | # | |
| 8 | ||
| 9 | import ctypes | |
| 10 | import os | |
| 11 | ||
| 12 | # Load the servernode library to register CogServerNode type | |
| 13 | # This must happen before we can use types.CogServerNode | |
| 14 | _lib_paths = [ | |
| 15 | "/usr/local/lib/opencog/libservernode.so", | |
| 16 | "/usr/lib/opencog/libservernode.so", | |
| 17 | ] | |
| 18 | _servernode_lib = None | |
| 19 | for _path in _lib_paths: | |
| 20 | if os.path.exists(_path): | |
| 21 | _servernode_lib = ctypes.CDLL(_path, mode=ctypes.RTLD_GLOBAL) | |
| 22 | break | |
| 23 | ||
| 24 | if _servernode_lib is None: | |
| 25 | raise ImportError("Could not find libservernode.so - is cogserver installed?") | |
| 26 | ||
| 27 | from opencog.type_constructors import get_thread_atomspace, FloatValue, VoidValue, Predicate | |
| 28 | from opencog.atomspace import types | |
| 29 | ||
| 30 | def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888, | |
| 31 | enable_console=True, enable_web=True, enable_mcp=True): | |
| 32 | """Start the CogServer with the specified configuration. | |
| 33 | ||
| 34 | Args: | |
| 35 | console_port (int, optional): Port for telnet console server. Default: 17001 | |
| 36 | web_port (int, optional): Port for web server. Default: 18080 | |
| 37 | mcp_port (int, optional): Port for MCP server. Default: 18888 | |
| 38 | enable_console (bool, optional): Enable telnet console server. Default: True | |
| 39 | enable_web (bool, optional): Enable web server. Default: True | |
| 40 | enable_mcp (bool, optional): Enable MCP server. Default: True | |
| 41 | ||
| 42 | Returns: | |
| 43 | CogServerNode: The CogServerNode atom that was created. | |
| 44 | ||
| 45 | Raises: | |
| 46 | ValueError: If invalid ports are specified. | |
| 47 | """ | |
| 48 | # Validate ports | |
| 49 | for port, pname in [(console_port, "console"), (web_port, "web"), (mcp_port, "mcp")]: | |
| 50 | if not (1 <= port <= 65535): | |
| 51 | raise ValueError(f"Invalid {pname} port: {port}. Must be between 1 and 65535") | |
| 52 | ||
| 53 | # Compute effective port values (0 disables a server) | |
| 54 | effective_console = console_port if enable_console else 0 | |
| 55 | effective_web = web_port if enable_web else 0 | |
| 56 | effective_mcp = mcp_port if enable_mcp else 0 | |
| 57 | ||
| 58 | # Construct name from port configuration | |
| 59 | name = f"cogserver://{effective_console}:{effective_web}:{effective_mcp}" | |
| 60 | ||
| 61 | # Get the thread atomspace and add the CogServerNode to it | |
| 62 | atomspace = get_thread_atomspace() | |
| 63 | server_handle = atomspace.add_node(types.CogServerNode, name) | |
| 64 | ||
| 65 | if effective_console != 17001: | |
| 66 | key = Predicate("*-telnet-port-*") | |
| 67 | atomspace.set_value(server_handle, key, FloatValue(float(effective_console))) | |
| 68 | if effective_web != 18080: | |
| 69 | key = Predicate("*-web-port-*") | |
| 70 | atomspace.set_value(server_handle, key, FloatValue(float(effective_web))) | |
| 71 | if effective_mcp != 18888: | |
| 72 | key = Predicate("*-mcp-port-*") | |
| 73 | atomspace.set_value(server_handle, key, FloatValue(float(effective_mcp))) | |
| 74 | ||
| 75 | # Start all servers (this also starts the server thread) | |
| 76 | atomspace.set_value(server_handle, Predicate("*-start-*"), VoidValue()) | |
| 77 | ||
| 78 | return server_handle | |
| 79 | ||
| 80 | def stop_cogserver(server_node): | |
| 81 | """Stop the specified CogServer. | |
| 82 | ||
| 83 | Args: | |
| 84 | server_node: The CogServerNode to stop. | |
| 85 | """ | |
| 86 | atomspace = get_thread_atomspace() | |
| 87 | atomspace.set_value(server_node, Predicate("*-stop-*"), VoidValue()) |
| … | … | @@ -0,0 +1,16 @@ |
| 1 | from pkgutil import extend_path | |
| 2 | __path__ = extend_path(__path__, __name__) | |
| 3 | ||
| 4 | # Import and expose cogserver functions | |
| 5 | try: | |
| 6 | from .cogserver import ( | |
| 7 | start_cogserver, | |
| 8 | stop_cogserver, | |
| 9 | ) | |
| 10 | __all__ = [ | |
| 11 | 'start_cogserver', | |
| 12 | 'stop_cogserver', | |
| 13 | ] | |
| 14 | except ImportError: | |
| 15 | # Module not fully installed yet | |
| 16 | pass |
| … | … | @@ -0,0 +1,90 @@ |
| 1 | # | |
| 2 | # cogserver.py | |
| 3 | # | |
| 4 | # Pure Python wrapper for starting/stopping the CogServer. | |
| 5 | # This module provides a convenient Python API for controlling | |
| 6 | # the CogServer network server. | |
| 7 | # | |
| 8 | ||
| 9 | import ctypes | |
| 10 | import os | |
| 11 | ||
| 12 | # Load the servernode library to register CogServerNode type | |
| 13 | # This must happen before we can use types.CogServerNode | |
| 14 | _lib_paths = [ | |
| 15 | # Build directory (for testing without install) | |
| 16 | os.path.join(os.path.dirname(__file__), "../../../../build/opencog/cogserver/atoms/libservernode.so"), | |
| 17 | # Installed locations | |
| 18 | "/usr/local/lib/opencog/libservernode.so", | |
| 19 | "/usr/lib/opencog/libservernode.so", | |
| 20 | ] | |
| 21 | _servernode_lib = None | |
| 22 | for _path in _lib_paths: | |
| 23 | if os.path.exists(_path): | |
| 24 | _servernode_lib = ctypes.CDLL(_path, mode=ctypes.RTLD_GLOBAL) | |
| 25 | break | |
| 26 | ||
| 27 | if _servernode_lib is None: | |
| 28 | raise ImportError("Could not find libservernode.so - is cogserver installed?") | |
| 29 | ||
| 30 | from opencog.type_constructors import get_thread_atomspace, FloatValue, VoidValue, Predicate | |
| 31 | from opencog.atomspace import types | |
| 32 | ||
| 33 | def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888, | |
| 34 | enable_console=True, enable_web=True, enable_mcp=True): | |
| 35 | """Start the CogServer with the specified configuration. | |
| 36 | ||
| 37 | Args: | |
| 38 | console_port (int, optional): Port for telnet console server. Default: 17001 | |
| 39 | web_port (int, optional): Port for web server. Default: 18080 | |
| 40 | mcp_port (int, optional): Port for MCP server. Default: 18888 | |
| 41 | enable_console (bool, optional): Enable telnet console server. Default: True | |
| 42 | enable_web (bool, optional): Enable web server. Default: True | |
| 43 | enable_mcp (bool, optional): Enable MCP server. Default: True | |
| 44 | ||
| 45 | Returns: | |
| 46 | CogServerNode: The CogServerNode atom that was created. | |
| 47 | ||
| 48 | Raises: | |
| 49 | ValueError: If invalid ports are specified. | |
| 50 | """ | |
| 51 | # Validate ports | |
| 52 | for port, pname in [(console_port, "console"), (web_port, "web"), (mcp_port, "mcp")]: | |
| 53 | if not (1 <= port <= 65535): | |
| 54 | raise ValueError(f"Invalid {pname} port: {port}. Must be between 1 and 65535") | |
| 55 | ||
| 56 | # Compute effective port values (0 disables a server) | |
| 57 | effective_console = console_port if enable_console else 0 | |
| 58 | effective_web = web_port if enable_web else 0 | |
| 59 | effective_mcp = mcp_port if enable_mcp else 0 | |
| 60 | ||
| 61 | # Construct name from port configuration | |
| 62 | name = f"cogserver://{effective_console}:{effective_web}:{effective_mcp}" | |
| 63 | ||
| 64 | # Get the thread atomspace and add the CogServerNode to it | |
| 65 | atomspace = get_thread_atomspace() | |
| 66 | server_handle = atomspace.add_node(types.CogServerNode, name) | |
| 67 | ||
| 68 | if effective_console != 17001: | |
| 69 | key = Predicate("*-telnet-port-*") | |
| 70 | atomspace.set_value(server_handle, key, FloatValue(float(effective_console))) | |
| 71 | if effective_web != 18080: | |
| 72 | key = Predicate("*-web-port-*") | |
| 73 | atomspace.set_value(server_handle, key, FloatValue(float(effective_web))) | |
| 74 | if effective_mcp != 18888: | |
| 75 | key = Predicate("*-mcp-port-*") | |
| 76 | atomspace.set_value(server_handle, key, FloatValue(float(effective_mcp))) | |
| 77 | ||
| 78 | # Start all servers (this also starts the server thread) | |
| 79 | atomspace.set_value(server_handle, Predicate("*-start-*"), VoidValue()) | |
| 80 | ||
| 81 | return server_handle | |
| 82 | ||
| 83 | def stop_cogserver(server_node): | |
| 84 | """Stop the specified CogServer. | |
| 85 | ||
| 86 | Args: | |
| 87 | server_node: The CogServerNode to stop. | |
| 88 | """ | |
| 89 | atomspace = get_thread_atomspace() | |
| 90 | atomspace.set_value(server_node, Predicate("*-stop-*"), VoidValue()) |
| … | … | @@ -10,8 +10,8 @@ import time |
| 10 | 10 | import sys |
| 11 | 11 | import os |
| 12 | 12 | |
| 13 | # Add the build directory to Python path if needed | |
| 14 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../build/opencog/cython')) | |
| 13 | # Add the source directory to Python path for the pure Python cogserver module | |
| 14 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../opencog/cogserver/python')) | |
| 15 | 15 | |
| 16 | 16 | try: |
| 17 | 17 | from opencog.atomspace import AtomSpace |