00001 #ifndef PROTON_DRIVER_H 00002 #define PROTON_DRIVER_H 1 00003 00004 /* 00005 * 00006 * Licensed to the Apache Software Foundation (ASF) under one 00007 * or more contributor license agreements. See the NOTICE file 00008 * distributed with this work for additional information 00009 * regarding copyright ownership. The ASF licenses this file 00010 * to you under the Apache License, Version 2.0 (the 00011 * "License"); you may not use this file except in compliance 00012 * with the License. You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, 00017 * software distributed under the License is distributed on an 00018 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 00019 * KIND, either express or implied. See the License for the 00020 * specific language governing permissions and limitations 00021 * under the License. 00022 * 00023 */ 00024 00025 #include <proton/import_export.h> 00026 #include <proton/error.h> 00027 #include <proton/engine.h> 00028 #include <proton/sasl.h> 00029 #include <proton/ssl.h> 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif 00034 00035 /** @file 00036 * API for the Driver Layer. 00037 * 00038 * The driver library provides a simple implementation of a driver for 00039 * the proton engine. A driver is responsible for providing input, 00040 * output, and tick events to the bottom half of the engine API. See 00041 * ::pn_transport_input, ::pn_transport_output, and 00042 * ::pn_transport_tick. The driver also provides an interface for the 00043 * application to access the top half of the API when the state of the 00044 * engine may have changed due to I/O or timing events. Additionally 00045 * the driver incorporates the SASL engine as well in order to provide 00046 * a complete network stack: AMQP over SASL over TCP. 00047 * 00048 */ 00049 00050 typedef struct pn_driver_t pn_driver_t; 00051 typedef struct pn_listener_t pn_listener_t; 00052 typedef struct pn_connector_t pn_connector_t; 00053 00054 typedef enum { 00055 PN_CONNECTOR_WRITABLE, 00056 PN_CONNECTOR_READABLE 00057 } pn_activate_criteria_t; 00058 00059 /** Construct a driver 00060 * 00061 * Call pn_driver_free() to release the driver object. 00062 * @return new driver object, NULL if error 00063 */ 00064 PN_EXTERN pn_driver_t *pn_driver(void); 00065 00066 /** Return the most recent error code. 00067 * 00068 * @param[in] d the driver 00069 * 00070 * @return the most recent error text for d 00071 */ 00072 PN_EXTERN int pn_driver_errno(pn_driver_t *d); 00073 00074 /** Return the most recent error text for d. 00075 * 00076 * @param[in] d the driver 00077 * 00078 * @return the most recent error text for d 00079 */ 00080 PN_EXTERN const char *pn_driver_error(pn_driver_t *d); 00081 00082 /** Set the tracing level for the given driver. 00083 * 00084 * @param[in] driver the driver to trace 00085 * @param[in] trace the trace level to use. 00086 * @todo pn_trace_t needs documentation 00087 */ 00088 PN_EXTERN void pn_driver_trace(pn_driver_t *driver, pn_trace_t trace); 00089 00090 /** Force pn_driver_wait() to return 00091 * 00092 * @param[in] driver the driver to wake up 00093 * 00094 * @return zero on success, an error code on failure 00095 */ 00096 PN_EXTERN int pn_driver_wakeup(pn_driver_t *driver); 00097 00098 /** Wait for an active connector or listener 00099 * 00100 * @param[in] driver the driver to wait on 00101 * @param[in] timeout maximum time in milliseconds to wait, -1 means 00102 * infinite wait 00103 * 00104 * @return zero on success, an error code on failure 00105 */ 00106 PN_EXTERN int pn_driver_wait(pn_driver_t *driver, int timeout); 00107 00108 /** Get the next listener with pending data in the driver. 00109 * 00110 * @param[in] driver the driver 00111 * @return NULL if no active listener available 00112 */ 00113 PN_EXTERN pn_listener_t *pn_driver_listener(pn_driver_t *driver); 00114 00115 /** Get the next active connector in the driver. 00116 * 00117 * Returns the next connector with pending inbound data, available 00118 * capacity for outbound data, or pending tick. 00119 * 00120 * @param[in] driver the driver 00121 * @return NULL if no active connector available 00122 */ 00123 PN_EXTERN pn_connector_t *pn_driver_connector(pn_driver_t *driver); 00124 00125 /** Free the driver allocated via pn_driver, and all associated 00126 * listeners and connectors. 00127 * 00128 * @param[in] driver the driver to free, no longer valid on 00129 * return 00130 */ 00131 PN_EXTERN void pn_driver_free(pn_driver_t *driver); 00132 00133 00134 /** pn_listener - the server API **/ 00135 00136 /** Construct a listener for the given address. 00137 * 00138 * @param[in] driver driver that will 'own' this listener 00139 * @param[in] host local host address to listen on 00140 * @param[in] port local port to listen on 00141 * @param[in] context application-supplied, can be accessed via 00142 * pn_listener_context() 00143 * @return a new listener on the given host:port, NULL if error 00144 */ 00145 PN_EXTERN pn_listener_t *pn_listener(pn_driver_t *driver, const char *host, 00146 const char *port, void* context); 00147 00148 /** Access the head listener for a driver. 00149 * 00150 * @param[in] driver the driver whose head listener will be returned 00151 * 00152 * @return the head listener for driver or NULL if there is none 00153 */ 00154 PN_EXTERN pn_listener_t *pn_listener_head(pn_driver_t *driver); 00155 00156 /** Access the next listener. 00157 * 00158 * @param[in] listener the listener whose next listener will be 00159 * returned 00160 * 00161 * @return the next listener 00162 */ 00163 PN_EXTERN pn_listener_t *pn_listener_next(pn_listener_t *listener); 00164 00165 /** 00166 * @todo pn_listener_trace needs documentation 00167 */ 00168 PN_EXTERN void pn_listener_trace(pn_listener_t *listener, pn_trace_t trace); 00169 00170 /** Accept a connection that is pending on the listener. 00171 * 00172 * @param[in] listener the listener to accept the connection on 00173 * @return a new connector for the remote, or NULL on error 00174 */ 00175 PN_EXTERN pn_connector_t *pn_listener_accept(pn_listener_t *listener); 00176 00177 /** Access the application context that is associated with the listener. 00178 * 00179 * @param[in] listener the listener whose context is to be returned 00180 * @return the application context that was passed to pn_listener() or 00181 * pn_listener_fd() 00182 */ 00183 PN_EXTERN void *pn_listener_context(pn_listener_t *listener); 00184 00185 PN_EXTERN void pn_listener_set_context(pn_listener_t *listener, void *context); 00186 00187 /** Close the socket used by the listener. 00188 * 00189 * @param[in] listener the listener whose socket will be closed. 00190 */ 00191 PN_EXTERN void pn_listener_close(pn_listener_t *listener); 00192 00193 /** Frees the given listener. 00194 * 00195 * Assumes the listener's socket has been closed prior to call. 00196 * 00197 * @param[in] listener the listener object to free, no longer valid 00198 * on return 00199 */ 00200 PN_EXTERN void pn_listener_free(pn_listener_t *listener); 00201 00202 00203 00204 00205 /** pn_connector - the client API **/ 00206 00207 /** Construct a connector to the given remote address. 00208 * 00209 * @param[in] driver owner of this connection. 00210 * @param[in] host remote host to connect to. 00211 * @param[in] port remote port to connect to. 00212 * @param[in] context application supplied, can be accessed via 00213 * pn_connector_context() @return a new connector 00214 * to the given remote, or NULL on error. 00215 */ 00216 PN_EXTERN pn_connector_t *pn_connector(pn_driver_t *driver, const char *host, 00217 const char *port, void* context); 00218 00219 /** Access the head connector for a driver. 00220 * 00221 * @param[in] driver the driver whose head connector will be returned 00222 * 00223 * @return the head connector for driver or NULL if there is none 00224 */ 00225 PN_EXTERN pn_connector_t *pn_connector_head(pn_driver_t *driver); 00226 00227 /** Access the next connector. 00228 * 00229 * @param[in] connector the connector whose next connector will be 00230 * returned 00231 * 00232 * @return the next connector 00233 */ 00234 PN_EXTERN pn_connector_t *pn_connector_next(pn_connector_t *connector); 00235 00236 /** Set the tracing level for the given connector. 00237 * 00238 * @param[in] connector the connector to trace 00239 * @param[in] trace the trace level to use. 00240 */ 00241 PN_EXTERN void pn_connector_trace(pn_connector_t *connector, pn_trace_t trace); 00242 00243 /** Service the given connector. 00244 * 00245 * Handle any inbound data, outbound data, or timing events pending on 00246 * the connector. 00247 * 00248 * @param[in] connector the connector to process. 00249 */ 00250 PN_EXTERN void pn_connector_process(pn_connector_t *connector); 00251 00252 /** Access the listener which opened this connector. 00253 * 00254 * @param[in] connector connector whose listener will be returned. 00255 * @return the listener which created this connector, or NULL if the 00256 * connector has no listener (e.g. an outbound client 00257 * connection) 00258 */ 00259 PN_EXTERN pn_listener_t *pn_connector_listener(pn_connector_t *connector); 00260 00261 /** Access the Authentication and Security context of the connector. 00262 * 00263 * @param[in] connector connector whose security context will be 00264 * returned 00265 * @return the Authentication and Security context for the connector, 00266 * or NULL if none 00267 */ 00268 PN_EXTERN pn_sasl_t *pn_connector_sasl(pn_connector_t *connector); 00269 00270 /** Access the AMQP Connection associated with the connector. 00271 * 00272 * @param[in] connector the connector whose connection will be 00273 * returned 00274 * @return the connection context for the connector, or NULL if none 00275 */ 00276 PN_EXTERN pn_connection_t *pn_connector_connection(pn_connector_t *connector); 00277 00278 /** Assign the AMQP Connection associated with the connector. 00279 * 00280 * @param[in] connector the connector whose connection will be set. 00281 * @param[in] connection the connection to associate with the 00282 * connector 00283 */ 00284 PN_EXTERN void pn_connector_set_connection(pn_connector_t *connector, pn_connection_t *connection); 00285 00286 /** Access the application context that is associated with the 00287 * connector. 00288 * 00289 * @param[in] connector the connector whose context is to be returned. 00290 00291 * @return the application context that was passed to pn_connector() 00292 * or pn_connector_fd() 00293 */ 00294 PN_EXTERN void *pn_connector_context(pn_connector_t *connector); 00295 00296 /** Assign a new application context to the connector. 00297 * 00298 * @param[in] connector the connector which will hold the context. 00299 * @param[in] context new application context to associate with the 00300 * connector 00301 */ 00302 PN_EXTERN void pn_connector_set_context(pn_connector_t *connector, void *context); 00303 00304 /** Access the transport used by this connector. 00305 * 00306 * @param[in] connector connector whose transport will be returned 00307 * @return the transport, or NULL if none 00308 */ 00309 PN_EXTERN pn_transport_t *pn_connector_transport(pn_connector_t *connector); 00310 00311 /** Close the socket used by the connector. 00312 * 00313 * @param[in] connector the connector whose socket will be closed 00314 */ 00315 PN_EXTERN void pn_connector_close(pn_connector_t *connector); 00316 00317 /** Determine if the connector is closed. 00318 * 00319 * @return True if closed, otherwise false 00320 */ 00321 PN_EXTERN bool pn_connector_closed(pn_connector_t *connector); 00322 00323 /** Destructor for the given connector. 00324 * 00325 * Assumes the connector's socket has been closed prior to call. 00326 * 00327 * @param[in] connector the connector object to free. No longer 00328 * valid on return 00329 */ 00330 PN_EXTERN void pn_connector_free(pn_connector_t *connector); 00331 00332 /** Activate a connector when a criteria is met 00333 * 00334 * Set a criteria for a connector (i.e. it's transport is writable) that, once met, 00335 * the connector shall be placed in the driver's work queue. 00336 * 00337 * @param[in] connector The connector object to activate 00338 * @param[in] criteria The criteria that must be met prior to activating the connector 00339 */ 00340 PN_EXTERN void pn_connector_activate(pn_connector_t *connector, pn_activate_criteria_t criteria); 00341 00342 /** Return the activation status of the connector for a criteria 00343 * 00344 * Return the activation status (i.e. readable, writable) for the connector. This function 00345 * has the side-effect of canceling the activation of the criteria. 00346 * 00347 * Please note that this function must not be used for normal AMQP connectors. It is only 00348 * used for connectors created so the driver can track non-AMQP file descriptors. Such 00349 * connectors are never passed into pn_connector_process. 00350 * 00351 * @param[in] connector The connector object to activate 00352 * @param[in] criteria The criteria to test. "Is this the reason the connector appeared 00353 * in the work list?" 00354 * @return true iff the criteria is activated on the connector. 00355 */ 00356 PN_EXTERN bool pn_connector_activated(pn_connector_t *connector, pn_activate_criteria_t criteria); 00357 00358 00359 #ifdef __cplusplus 00360 } 00361 #endif 00362 00363 #endif /* driver.h */