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( const char* domain, const char* name, const char* resource,
66 const char* password, int port, osrfStringArray* trustedClients,
67 osrfStringArray* trustedServers );
70 Connects the given router to the network
72 int osrfRouterConnect( osrfRouter* router );
75 Waits for incoming data to route
76 If this function returns, then the router's connection to the jabber server
79 void osrfRouterRun( osrfRouter* router );
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.
89 osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname );
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
97 int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId );
101 Handles top level router messages
104 int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg );
108 Handles class level requests
111 int osrfRouterClassHandleMessage( osrfRouter* router,
112 osrfRouterClass* rclass, transport_message* msg );
115 Removes a given class from the router, freeing as it goes
117 int osrfRouterRemoveClass( osrfRouter* router, const char* classname );
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
126 int osrfRouterClassRemoveNode( osrfRouter* router, const char* classname,
127 const char* remoteId );
130 Frees a router class object
131 Takes a void* since it is freed by the hash code
133 void osrfRouterClassFree( char* classname, void* rclass );
136 Frees a router node object
137 Takes a void* since it is freed by the list code
139 void osrfRouterNodeFree( char* remoteId, void* node );
145 void osrfRouterFree( osrfRouter* router );
148 Finds the class associated with the given class name in the list of classes
150 osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname );
153 Finds the router node within this class with the given remote id
155 osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, const char* remoteId );
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
164 int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set );
169 Utility method for handling incoming requests to the router
170 and making sure the sender is allowed.
172 void osrfRouterHandleIncoming( osrfRouter* router );
175 Utility method for handling incoming requests to a router class,
176 makes sure sender is a trusted client
178 int osrfRouterClassHandleIncoming( osrfRouter* router,
179 const char* classname, osrfRouterClass* class );
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.
186 transport_message* osrfRouterClassHandleBounce( osrfRouter* router,
187 const char* classname, osrfRouterClass* rclass, transport_message* msg );
192 handles messages that don't have a 'router_command' set. They are assumed to
193 be app request messages
195 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
199 Handles connects, disconnects, etc.
201 int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg );
205 Handles REQUEST messages
207 int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg );
211 int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg );
214 int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
218 int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg );
220 int osrfRouterHandleAppResponse( osrfRouter* router,
221 transport_message* msg, osrfMessage* omsg, const jsonObject* response );
224 int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg );