servers_manager.h
Go to the documentation of this file.
1 /*
2 ** Copyright 2014-2016 The Earlham Institute
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
21 /*
22  * servers_pool.h
23  *
24  * Created on: 12 Jun 2015
25  * Author: tyrrells
26  */
27 
28 #ifndef SERVERS_POOL_H_
29 #define SERVERS_POOL_H_
30 
31 #include "uuid/uuid.h"
32 
33 #include "connection.h"
35 #include "linked_list.h"
36 #include "operation.h"
37 #include "memory_allocations.h"
38 #include "string_utils.h"
39 
40 
48 typedef struct ExternalServer
49 {
51  char *es_name_s;
52 
54  char *es_uri_s;
55 
58 
65  uuid_t es_id;
66 
67 
75 
76 
78 
79 
87 typedef struct ExternalServerNode
88 {
91 
94 
102 
103 
104 /* forward declaration */
105 struct ServersManager;
106 struct GrassrootsServer;
107 
108 
117 typedef ExternalServer *(*ExternalServerDeserialiser) (const unsigned char *data_p);
118 
119 
120 
131 typedef unsigned char *(*ExternalServerSerialiser) (ExternalServer *server_p, uint32 *length_p);
132 
133 
141 typedef struct ServersManager
142 {
145 
146 
148  uuid_t sm_server_id;
149 
156 
157 
172  int (*sm_add_server_fn) (struct ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn);
173 
188  ExternalServer *(*sm_get_server_fn) (struct ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn);
189 
204  ExternalServer *(*sm_remove_server_fn) (struct ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn);
205 
206 
207 
219  LinkedList *(*sm_get_all_servers_fn) (struct ServersManager *manager_p, ExternalServerDeserialiser deserialise_fn);
220 
221 
227  void (*sm_free_servers_manager_fn) (struct ServersManager *manager_p);
228 
229 
237  LinkedList *(*sm_get_all_matching_servers_fn) (struct ServersManager *manager_p, const char * const local_service_name_s);
238 
239 
241 
242 
243 #ifdef __cplusplus
244 extern "C"
245 {
246 #endif
247 
248 
249 GRASSROOTS_SERVICE_MANAGER_API ServersManager *LoadServersManager (const char *servers_manager_s, struct GrassrootsServer *server_p);
250 
251 
252 GRASSROOTS_SERVICE_MANAGER_LOCAL ServersManager *GetServersManagerFromPlugin (struct Plugin * const plugin_p);
253 
254 
261 GRASSROOTS_SERVICE_MANAGER_API uuid_t *GetLocalServerId (struct GrassrootsServer *grassroots_p);
262 
263 
270 GRASSROOTS_SERVICE_MANAGER_API const char *GetLocalServerIdAsString (struct GrassrootsServer *grassroots_p);
271 
272 
286 GRASSROOTS_SERVICE_MANAGER_API void InitServersManager (ServersManager *manager_p,
287  int (*add_server_fn) (ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn),
288  ExternalServer *(*get_server_fn) (ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn),
289  ExternalServer *(*remove_server_fn) (ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn),
290  LinkedList *(*get_all_servers_fn) (struct ServersManager *manager_p, ExternalServerDeserialiser deserialise_fn),
291  void (*free_servers_manager_fn) (struct ServersManager *manager_p));
292 
293 
310 GRASSROOTS_SERVICE_MANAGER_API int AddExternalServerToServersManager (ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn);
311 
312 
328 GRASSROOTS_SERVICE_MANAGER_API ExternalServer *GetExternalServerFromServersManager (ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn);
329 
330 
347 GRASSROOTS_SERVICE_MANAGER_API ExternalServer *RemoveExternalServerFromServersManager (ServersManager *manager_p, const char * const server_uri_s, ExternalServerDeserialiser deserialise_fn);
348 
349 
363 GRASSROOTS_SERVICE_MANAGER_API LinkedList *GetAllExternalServersFromServersManager (ServersManager *manager_p, ExternalServerDeserialiser deserialise_fn);
364 
365 
366 
376 GRASSROOTS_SERVICE_MANAGER_API void FreeServersManager (ServersManager *manager_p);
377 
378 
389 GRASSROOTS_SERVICE_MANAGER_API json_t *AddExternalServerOperationsToJSON (ServersManager *manager_p, Operation op);
390 
391 
403 GRASSROOTS_SERVICE_MANAGER_API ExternalServer *AllocateExternalServer (const char *name_s, const char *uri_s, const char *uuid_s, ConnectionType ct);
404 
405 
412 GRASSROOTS_SERVICE_MANAGER_API void FreeExternalServer (ExternalServer *server_p);
413 
414 
424 GRASSROOTS_SERVICE_MANAGER_API ExternalServerNode *AllocateExternalServerNode (ExternalServer *server_p, MEM_FLAG mf);
425 
426 
433 GRASSROOTS_SERVICE_MANAGER_API void FreeExternalServerNode (ListItem *node_p);
434 
435 
449 GRASSROOTS_SERVICE_MANAGER_API json_t *MakeRemoteJSONCallToExternalServer (ExternalServer *server_p, json_t *request_p);
450 
451 
466 GRASSROOTS_SERVICE_MANAGER_API bool AddExternalServerFromJSON (ServersManager *manager_p, const json_t *json_p);
467 
468 
478 GRASSROOTS_SERVICE_MANAGER_API const char *GetRemotePairedServiceName (const ExternalServer * const src_p, const char * const local_service_name_s);
479 
480 
481 
492 GRASSROOTS_SERVICE_MANAGER_API ExternalServer *CreateExternalServerFromJSON (const json_t *json_p);
493 
494 
506 GRASSROOTS_SERVICE_MANAGER_API unsigned char *SerialiseExternalServerToJSON (ExternalServer * const external_server_p, unsigned int *value_length_p);
507 
508 
517 GRASSROOTS_SERVICE_MANAGER_API ExternalServer *DeserialiseExternalServerFromJSON (const unsigned char *raw_json_data_s);
518 
519 
527 GRASSROOTS_SERVICE_MANAGER_API json_t *GetExternalServerAsJSON (ExternalServer *server_p);
528 
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
534 
535 #endif /* SERVERS_POOL_H_ */
ExternalServer * CreateExternalServerFromJSON(const json_t *json_p)
Create a ExternalServer from a json_t object.
The base class for building a Server-Client connection.
Definition: connection.h:66
ConnectionType
An enumeration listing the different types of Connections.
Definition: connection.h:48
ExternalServer * esn_server_p
The ExternalServer.
Definition: servers_manager.h:93
ExternalServer *(* ExternalServerDeserialiser)(const unsigned char *data_p)
A function to take a raw binary data representation of a previously stored ExternalServer and recreat...
Definition: servers_manager.h:117
void FreeExternalServer(ExternalServer *server_p)
Free an ExternalServer.
ExternalServer * AllocateExternalServer(const char *name_s, const char *uri_s, const char *uuid_s, ConnectionType ct)
Allocate an ExternalServer.
A doubly-linked node that points to its predecessor and successor.
Definition: linked_list.h:43
MEM_FLAG esn_server_mem
The MEM_FLAG indicating the ownership of the ExternalServer from this ExternalServerNode and whether ...
Definition: servers_manager.h:100
MEM_FLAG
An enum specifying the particular status of a piece of dynamically allocated memory for a particular ...
Definition: memory_allocations.h:36
ExternalServer * GetExternalServerFromServersManager(ServersManager *manager_p, const char *const server_uri_s, ExternalServerDeserialiser deserialise_fn)
Find a ExternalServer.
const char * GetLocalServerIdAsString(struct GrassrootsServer *grassroots_p)
Get the current Server uuid as a c-style string.
Operation
The various Operations that a Server can perform.
Definition: operation.h:46
ExternalServer * DeserialiseExternalServerFromJSON(const unsigned char *raw_json_data_s)
Create an ExternalServer from a raw data memory block.
A datatype to allow access to an external Grassroots Server.
Definition: servers_manager.h:48
uuid_t es_id
A unique id to identify the ExternalServer.
Definition: servers_manager.h:65
ListItem esn_node
The base node.
Definition: servers_manager.h:90
char * es_uri_s
The URI for the Server.
Definition: servers_manager.h:54
char sm_server_id_s[UUID_STRING_BUFFER_SIZE]
The UUID of this ExternalServer as a string.
Definition: servers_manager.h:155
A set of string functions.
json_t * GetExternalServerAsJSON(ExternalServer *server_p)
Get the JSON fragment that describes the given ExternalServer.
Connection * es_connection_p
The Connection to the ExternalServer.
Definition: servers_manager.h:57
A doubly-linked list.
int AddExternalServerToServersManager(ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn)
Add an ExternalServer to the ServersManager.
A doubly-linked list that can be traversed in either direction.
Definition: linked_list.h:56
A Plugin is a dynamically-loaded component to provide a piece of functionality.
Definition: plugin.h:151
LinkedList * GetAllExternalServersFromServersManager(ServersManager *manager_p, ExternalServerDeserialiser deserialise_fn)
Get all ExternalServers.
uuid_t sm_server_id
The UUID for this Server.
Definition: servers_manager.h:148
ExternalServerNode * AllocateExternalServerNode(ExternalServer *server_p, MEM_FLAG mf)
Allocate an ExternalServerNode.
bool AddExternalServerFromJSON(ServersManager *manager_p, const json_t *json_p)
Add an ExternalServer from a JSON-based definition.
#define UUID_STRING_BUFFER_SIZE
The number of bytes required to store a c-style string representation of a UUID.
Definition: string_utils.h:42
LinkedList * es_paired_services_p
A List of KeyValuePairNodes where the key is the name of the Service on the local Server and the valu...
Definition: servers_manager.h:74
struct Plugin * sm_plugin_p
The plugin that thjs JobsManager was loaded from.
Definition: servers_manager.h:144
json_t * AddExternalServerOperationsToJSON(ServersManager *manager_p, Operation op)
Get the JSON fragment which will form part of a response to other servers and clients.
uuid_t * GetLocalServerId(struct GrassrootsServer *grassroots_p)
Get the current Server uuid.
unsigned char *(* ExternalServerSerialiser)(ExternalServer *server_p, uint32 *length_p)
A function to generate a raw binary data representation of a ExternalServer allowing it to be recreat...
Definition: servers_manager.h:131
A datatype for storing ExternalServers in a LinkedList.
Definition: servers_manager.h:87
const char * GetRemotePairedServiceName(const ExternalServer *const src_p, const char *const local_service_name_s)
Get the name of the remote paired service corresponding to a given local service name.
void InitServersManager(ServersManager *manager_p, int(*add_server_fn)(ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn), ExternalServer *(*get_server_fn)(ServersManager *manager_p, const char *const server_uri_s, ExternalServerDeserialiser deserialise_fn), ExternalServer *(*remove_server_fn)(ServersManager *manager_p, const char *const server_uri_s, ExternalServerDeserialiser deserialise_fn), LinkedList *(*get_all_servers_fn)(struct ServersManager *manager_p, ExternalServerDeserialiser deserialise_fn), void(*free_servers_manager_fn)(struct ServersManager *manager_p))
Initialise a ServersManager.
Definition: grassroots_server.h:44
void(* sm_free_servers_manager_fn)(struct ServersManager *manager_p)
Free a given ServersManager.
Definition: servers_manager.h:227
ExternalServer * RemoveExternalServerFromServersManager(ServersManager *manager_p, const char *const server_uri_s, ExternalServerDeserialiser deserialise_fn)
Remove a ExternalServer.
unsigned char * SerialiseExternalServerToJSON(ExternalServer *const external_server_p, unsigned int *value_length_p)
Get a raw unsigned char array that allows the ExternalServer to be stored in the ServersManager.
json_t * MakeRemoteJSONCallToExternalServer(ExternalServer *server_p, json_t *request_p)
Call a remote server.
void FreeExternalServerNode(ListItem *node_p)
Free an ExternalServerNode.
char * es_name_s
The user-friendly name of the server.
Definition: servers_manager.h:51
int(* sm_add_server_fn)(struct ServersManager *manager_p, ExternalServer *server_p, ExternalServerSerialiser serialise_fn)
Add an ExternalServer to the ServersManager.
Definition: servers_manager.h:172
A datatype for monitoring ExternalServers.
Definition: servers_manager.h:141
void FreeServersManager(ServersManager *manager_p)
Free a ServersManager.