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