]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/router/osrf_router.h
added list and hash code based on libJudy
[OpenSRF.git] / src / router / osrf_router.h
1 #include <sys/select.h>
2 #include <signal.h>
3 #include <stdio.h>
4
5 #include "opensrf/utils.h"
6 #include "opensrf/osrf_list.h"
7 #include "opensrf/osrf_hash.h"
8
9 #include "opensrf/string_array.h"
10 #include "opensrf/transport_client.h"
11 #include "opensrf/transport_message.h"
12
13 #include "opensrf/osrf_message.h"
14
15
16
17 /* a router maintains a list of server classes */
18 struct __osrfRouterStruct {
19
20         osrfHash* classes;      /* our list of server classes */
21         char* domain;                   /* our login domain */
22         char* name;
23         char* resource;
24         char* password;
25         int port;
26
27         osrfStringArray* trustedClients;
28         osrfStringArray* trustedServers;
29
30         transport_client* connection;
31 };
32
33 typedef struct __osrfRouterStruct osrfRouter;
34
35
36 /* a class maintains a set of server nodes */
37 struct __osrfRouterClassStruct {
38         osrfRouter* router; /* our router handle */
39         osrfHashIterator* itr;
40         osrfHash* nodes;
41         transport_client* connection;
42 };
43 typedef struct __osrfRouterClassStruct osrfRouterClass;
44
45 /* represents a link to a single server's inbound connection */
46 struct __osrfRouterNodeStruct {
47         char* remoteId; /* send message to me via this login */
48         int count;                      /* how many message have been sent to this node */
49         transport_message* lastMessage;
50 };
51 typedef struct __osrfRouterNodeStruct osrfRouterNode;
52
53 /**
54   Allocates a new router.  
55   @param domain The jabber domain to connect to
56   @param name The login name for the router
57   @param resource The login resource for the router
58   @param password The login password for the new router
59   @param port The port to connect to the jabber server on
60   @param trustedClients The array of client domains that we allow to send requests through us
61   @param trustedServers The array of server domains that we allow to register, etc. with ust.
62   @return The allocated router or NULL on memory error
63   */
64 osrfRouter* osrfNewRouter( char* domain, char* name, char* resource, 
65         char* password, int port, osrfStringArray* trustedClients, osrfStringArray* trustedServers );
66
67 /**
68   Connects the given router to the network
69   */
70 int osrfRouterConnect( osrfRouter* router );
71
72 /**
73   Waits for incoming data to route
74   If this function returns, then the router's connection to the jabber server
75   has failed.
76   */
77 void osrfRouterRun( osrfRouter* router );
78
79
80 /**
81   Allocates and adds a new router class handler to the router's list of handlers.
82   Also connects the class handler to the network at <routername>@domain/<classname>
83   @param router The current router instance
84   @param classname The name of the class this node handles.
85   @return 0 on success, -1 on connection error.
86   */
87 osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname );
88
89 /**
90   Adds a new server node to the given class.
91   @param rclass The Router class to add the node to
92   @param remoteId The remote login of this node
93   @return 0 on success, -1 on generic error
94   */
95 int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId );
96
97
98 /**
99   Handles top level router messages
100   @return 0 on success
101   */
102 int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg );
103
104
105 /**
106   Handles class level requests
107   @return 0 on success
108   */
109 int osrfRouterClassHandleMessage( osrfRouter* router, 
110                 osrfRouterClass* rclass, transport_message* msg );
111
112 /**
113   Removes a given class from the router, freeing as it goes
114   */
115 int osrfRouterRemoveClass( osrfRouter* router, char* classname );
116
117 /**
118   Removes the given node from the class.  Also, if this is that last node in the set,
119   removes the class from the router 
120   @return 0 on successful removal with no class removal
121   @return 1 on successful remove with class removal
122   @return -1 error on removal
123  */
124 int osrfRouterClassRemoveNode( osrfRouter* router, char* classname, char* remoteId );
125
126 /**
127   Frees a router class object
128   Takes a void* since it is freed by the hash code
129   */
130 void osrfRouterClassFree( char* classname, void* rclass );
131
132 /**
133   Frees a router node object 
134   Takes a void* since it is freed by the list code
135   */
136 void osrfRouterNodeFree( char* remoteId, void* node );
137
138
139 /**
140   Frees a router
141   */
142 void osrfRouterFree( osrfRouter* router );
143
144 /**
145   Finds the class associated with the given class name in the list of classes
146   */
147 osrfRouterClass* osrfRouterFindClass( osrfRouter* router, char* classname );
148
149 /**
150   Finds the router node within this class with the given remote id 
151   */
152 osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, char* remoteId );
153
154
155 /**
156   Clears and populates the provided fd_set* with file descriptors
157   from the router's top level connection as well as each of the
158   router class connections
159   @return The largest file descriptor found in the filling process
160   */
161 int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set );
162
163
164
165 /**
166   Utility method for handling incoming requests to the router
167   and making sure the sender is allowed.
168   */
169 void osrfRouterHandleIncoming( osrfRouter* router );
170
171 /**
172         Utility method for handling incoming requests to a router class,
173         makes sure sender is a trusted client
174         */
175 int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname,  osrfRouterClass* class );
176
177 /* handles case where router node is not longer reachable.  copies over the
178         data from the last sent message and returns a newly crafted suitable for treating
179         as a newly inconing message.  Removes the dead node and If there are no more
180         nodes to send the new message to, returns NULL.
181         */
182 transport_message* osrfRouterClassHandleBounce(
183                 osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg );
184
185
186
187 /**
188   handles messages that don't have a 'router_command' set.  They are assumed to
189   be app request messages 
190   */
191 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
192
193
194 /**
195   Handles connects, disconnects, etc.
196   */
197 int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg );
198
199
200 /**
201   Handles REQUEST messages 
202   */
203 int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg );
204
205
206
207 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
208
209
210 int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
211
212
213
214 int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
215
216 int osrfRouterHandleAppResponse( osrfRouter* router, 
217                 transport_message* msg, osrfMessage* omsg, jsonObject* response );
218
219
220 int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
221