1 #include <sys/select.h>
5 #include "opensrf/utils.h"
6 #include "opensrf/log.h"
7 #include "opensrf/osrf_list.h"
8 #include "opensrf/osrf_hash.h"
10 #include "opensrf/string_array.h"
11 #include "opensrf/transport_client.h"
12 #include "opensrf/transport_message.h"
14 #include "opensrf/osrf_message.h"
18 /* a router maintains a list of server classes */
19 struct __osrfRouterStruct {
21 osrfHash* classes; /* our list of server classes */
22 char* domain; /* our login domain */
28 osrfStringArray* trustedClients;
29 osrfStringArray* trustedServers;
31 transport_client* connection;
34 typedef struct __osrfRouterStruct osrfRouter;
37 /* a class maintains a set of server nodes */
38 struct __osrfRouterClassStruct {
39 osrfRouter* router; /* our router handle */
40 osrfHashIterator* itr;
42 transport_client* connection;
44 typedef struct __osrfRouterClassStruct osrfRouterClass;
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;
52 typedef struct __osrfRouterNodeStruct osrfRouterNode;
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
65 osrfRouter* osrfNewRouter( char* domain, char* name, char* resource,
66 char* password, int port, osrfStringArray* trustedClients, osrfStringArray* trustedServers );
69 Connects the given router to the network
71 int osrfRouterConnect( osrfRouter* router );
74 Waits for incoming data to route
75 If this function returns, then the router's connection to the jabber server
78 void osrfRouterRun( osrfRouter* router );
82 Allocates and adds a new router class handler to the router's list of handlers.
83 Also connects the class handler to the network at <routername>@domain/<classname>
84 @param router The current router instance
85 @param classname The name of the class this node handles.
86 @return 0 on success, -1 on connection error.
88 osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname );
91 Adds a new server node to the given class.
92 @param rclass The Router class to add the node to
93 @param remoteId The remote login of this node
94 @return 0 on success, -1 on generic error
96 int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId );
100 Handles top level router messages
103 int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg );
107 Handles class level requests
110 int osrfRouterClassHandleMessage( osrfRouter* router,
111 osrfRouterClass* rclass, transport_message* msg );
114 Removes a given class from the router, freeing as it goes
116 int osrfRouterRemoveClass( osrfRouter* router, char* classname );
119 Removes the given node from the class. Also, if this is that last node in the set,
120 removes the class from the router
121 @return 0 on successful removal with no class removal
122 @return 1 on successful remove with class removal
123 @return -1 error on removal
125 int osrfRouterClassRemoveNode( osrfRouter* router, char* classname, char* remoteId );
128 Frees a router class object
129 Takes a void* since it is freed by the hash code
131 void osrfRouterClassFree( char* classname, void* rclass );
134 Frees a router node object
135 Takes a void* since it is freed by the list code
137 void osrfRouterNodeFree( char* remoteId, void* node );
143 void osrfRouterFree( osrfRouter* router );
146 Finds the class associated with the given class name in the list of classes
148 osrfRouterClass* osrfRouterFindClass( osrfRouter* router, char* classname );
151 Finds the router node within this class with the given remote id
153 osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, char* remoteId );
157 Clears and populates the provided fd_set* with file descriptors
158 from the router's top level connection as well as each of the
159 router class connections
160 @return The largest file descriptor found in the filling process
162 int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set );
167 Utility method for handling incoming requests to the router
168 and making sure the sender is allowed.
170 void osrfRouterHandleIncoming( osrfRouter* router );
173 Utility method for handling incoming requests to a router class,
174 makes sure sender is a trusted client
176 int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRouterClass* class );
178 /* handles case where router node is not longer reachable. copies over the
179 data from the last sent message and returns a newly crafted suitable for treating
180 as a newly inconing message. Removes the dead node and If there are no more
181 nodes to send the new message to, returns NULL.
183 transport_message* osrfRouterClassHandleBounce(
184 osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg );
189 handles messages that don't have a 'router_command' set. They are assumed to
190 be app request messages
192 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
196 Handles connects, disconnects, etc.
198 int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg );
202 Handles REQUEST messages
204 int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg );
208 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
211 int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
215 int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
217 int osrfRouterHandleAppResponse( osrfRouter* router,
218 transport_message* msg, osrfMessage* omsg, jsonObject* response );
221 int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg );