-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-service.php
More file actions
204 lines (180 loc) · 6.13 KB
/
Copy pathclass-service.php
File metadata and controls
204 lines (180 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* OpenSimulator Service Engine
*
* Framework-agnostic service connection management for OpenSimulator instances.
* Handles connections to grid services and simulators including:
* - URI connections for basic information fetching
* - Database connections for direct queries and updates
* - Remote console connections for console access
*
* @package OpenSim Engine
* @version 0.1
*/
class OpenSim_Service {
protected $serviceURI;
protected $credentials;
protected $serviceType;
protected static $consoles = array(); // Ensure each console is initialized only once
protected static $dbs = array(); // Ensure each db is initialized only once
protected $console;
public $db;
public function __construct($serviceURI = null, $credentials = null) {
if ($serviceURI) {
$this->serviceURI = $serviceURI;
$this->credentials = $credentials;
$this->init_console();
$this->init_db();
}
}
/**
* Initialize database connection
*/
public function init_db() {
if ($this->db) {
return $this->db;
}
if (isset(self::$dbs[$this->serviceURI])) {
$this->db = self::$dbs[$this->serviceURI];
return $this->db;
}
$db_creds = $this->credentials['db'] ?? null;
if (empty($db_creds) || empty($db_creds['enabled'])) {
return false;
}
$this->db = new OSPDO($this->serviceURI);
if ($this->is_error($this->db)) {
error_log('Database connection error: ' . $this->get_error_message($this->db));
$this->db = false;
} elseif ($this->db) {
$tables = $this->db->get_results('SHOW TABLES');
if (!$tables || count($tables) === 0) {
$this->db = false;
}
}
if ($this->db) {
self::$dbs[$this->serviceURI] = $this->db;
}
return $this->db;
}
/**
* Initialize console connection
*/
public function init_console() {
if ($this->console) {
return $this->console;
}
if (isset(self::$consoles[$this->serviceURI])) {
$this->console = self::$consoles[$this->serviceURI];
return $this->console;
}
$console_creds = $this->credentials['console'] ?? null;
if (empty($console_creds) || empty($console_creds['enabled'])) {
return false;
}
$rest_args = array(
'uri' => $console_creds['host'] . ':' . $console_creds['port'],
'ConsoleUser' => $console_creds['user'],
'ConsolePass' => $console_creds['pass'],
);
$this->console = false;
$rest = new OpenSim_Rest($rest_args);
if ($this->is_rest_error($rest->error ?? null)) {
error_log(__METHOD__ . ' ' . $this->get_error_message($rest->error));
$response = $rest->error;
} else {
$response = $rest->sendCommand('show info');
if ($this->is_rest_error($response)) {
$response = $this->create_error('console_command_failed', $this->get_error_message($response));
} else {
$this->console = $rest;
}
}
self::$consoles[$this->serviceURI] = $this->console;
return $response;
}
/**
* Check if console is connected
*/
public function console_connected() {
return ($this->console && $this->console !== false);
}
/**
* Check if database is connected
*/
public function db_connected() {
return ($this->db->ready ?? false);
}
/**
* Send command to console and return result
* If command is empty, return console status (true/false)
*
* @param string $command
* @return mixed Error on failure, boolean on status, or response array
*/
public function console($command = null) {
if (empty($this->serviceURI) || empty($this->credentials)) {
error_log(__METHOD__ . ' missing arguments to use console.');
return false;
}
// Initialize console, return false if failed
if (!$this->init_console()) {
error_log(__METHOD__ . ' console initialization failed.');
return false;
}
if ($this->is_error($this->console)) {
error_log('console error: ' . $this->get_error_message($this->console));
return false;
}
// Send command to console
if ($this->console && !empty($command)) {
$response = $this->console->sendCommand($command);
if ($this->is_rest_error($response)) {
$error = $this->create_error('console_command_failed', $this->get_error_message($response));
error_log('console error: ' . $this->get_error_message($error));
return $error;
} else {
return $response;
}
} else {
return ($this->console) ? true : false;
}
}
/**
* Framework-agnostic error checking
* Override in framework-specific implementations
*/
protected function is_error($obj) {
return false;
}
/**
* Framework-agnostic REST error checking
* Override in framework-specific implementations
*/
protected function is_rest_error($obj) {
return function_exists('is_opensim_rest_error') ? is_opensim_rest_error($obj) : false;
}
/**
* Framework-agnostic error message extraction
* Override in framework-specific implementations
*/
protected function get_error_message($error) {
if (is_object($error) && method_exists($error, 'getMessage')) {
return $error->getMessage();
}
return 'Unknown error';
}
/**
* Framework-agnostic error creation
* Override in framework-specific implementations
*/
protected function create_error($code, $message) {
return array('error' => $code, 'message' => $message);
}
/**
* Get service URI
*/
public function get_service_uri() {
return $this->serviceURI;
}
}