1 #include <sys/select.h>
5 #include "opensrf/utils.h"
6 #include "opensrf/osrf_list.h"
7 #include "opensrf/osrf_hash.h"
9 #include "opensrf/string_array.h"
10 #include "opensrf/transport_client.h"
11 #include "opensrf/transport_message.h"
13 #include "opensrf/osrf_message.h"
17 /* a router maintains a list of server classes */
18 struct __osrfRouterStruct {
20 osrfHash* classes; /* our list of server classes */
21 char* domain; /* our login domain */
27 osrfStringArray* trustedClients;
28 osrfStringArray* trustedServers;
30 transport_client* connection;
33 typedef struct __osrfRouterStruct osrfRouter;
36 /* a class maintains a set of server nodes */
37 struct __osrfRouterClassStruct {
38 osrfRouter* router; /* our router handle */
39 osrfHashIterator* itr;
41 transport_client* connection;
43 typedef struct __osrfRouterClassStruct osrfRouterClass;
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;
51 typedef struct __osrfRouterNodeStruct osrfRouterNode;
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
64 osrfRouter* osrfNewRouter( char* domain, char* name, char* resource,
65 char* password, int port, osrfStringArray* trustedClients, osrfStringArray* trustedServers );
68 Connects the given router to the network
70 int osrfRouterConnect( osrfRouter* router );
73 Waits for incoming data to route
74 If this function returns, then the router's connection to the jabber server
77 void osrfRouterRun( osrfRouter* router );
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.
87 osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname );
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
95 int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId );
99 Handles top level router messages
102 int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg );
106 Handles class level requests
109 int osrfRouterClassHandleMessage( osrfRouter* router,
110 osrfRouterClass* rclass, transport_message* msg );
113 Removes a given class from the router, freeing as it goes
115 int osrfRouterRemoveClass( osrfRouter* router, char* classname );
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
124 int osrfRouterClassRemoveNode( osrfRouter* router, char* classname, char* remoteId );
127 Frees a router class object
128 Takes a void* since it is freed by the hash code
130 void osrfRouterClassFree( char* classname, void* rclass );
133 Frees a router node object
134 Takes a void* since it is freed by the list code
136 void osrfRouterNodeFree( char* remoteId, void* node );
142 void osrfRouterFree( osrfRouter* router );
145 Finds the class associated with the given class name in the list of classes
147 osrfRouterClass* osrfRouterFindClass( osrfRouter* router, char* classname );
150 Finds the router node within this class with the given remote id
152 osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, char* remoteId );
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
161 int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set );
166 Utility method for handling incoming requests to the router
167 and making sure the sender is allowed.
169 void osrfRouterHandleIncoming( osrfRouter* router );
172 Utility method for handling incoming requests to a router class,
173 makes sure sender is a trusted client
175 int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRouterClass* class );
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.
182 transport_message* osrfRouterClassHandleBounce(
183 osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg );
188 handles messages that don't have a 'router_command' set. They are assumed to
189 be app request messages
191 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
195 Handles connects, disconnects, etc.
197 int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg );
201 Handles REQUEST messages
203 int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg );
207 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
210 int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
214 int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
216 int osrfRouterHandleAppResponse( osrfRouter* router,
217 transport_message* msg, osrfMessage* omsg, jsonObject* response );
220 int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg );