Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

alp_ipc_internal.h

Go to the documentation of this file.
00001 /********************************************************************
00002 
00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved.
00004 
00005 The contents of this file are subject to the Mozilla Public License Version
00006 1.1 (the "License"); you may not use this file except in compliance with
00007 the License. You may obtain a copy of the License at
00008 http://www.mozilla.org/MPL/
00009 
00010 Software distributed under the License is distributed on an "AS IS" basis,
00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 for the specific language governing rights and limitations under the
00013 License.
00014 
00015 The Original Code is the entire contents of this file.
00016 
00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc.
00018 
00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All
00020 Rights Reserved.
00021 
00022 Contributor(s): none.
00023 
00024 ********************************************************************/
00033 #ifndef ALP_IPC_INTERNAL_H_
00034 #define ALP_IPC_INTERNAL_H_ 1
00035 
00036 #include <pthread.h>
00037 #include <signal.h>
00038 
00039 #include <hiker/ipc.h>
00040 
00041 #include <hiker/config_defines.h>
00042 
00043 /* Uncomment the next line to disable traces at COMPILE TIME */
00044 // #define ALP_TRACE ALP_TRACE_OFF
00045 
00046 #include <hiker/traces.h>
00047 
00048 // simple trace macro
00049 #define TRACE_IPC(str, ...) ALP_TM(TL(ALP_CLASS_IPC, "%s(): " str, __PRETTY_FUNCTION__, __VA_ARGS__))
00050 
00051 #define ALP_IPC_MSG_VERSION 1
00052 
00053 typedef struct _ConnectCBInfo ConnectCBInfo;
00054 struct _ConnectCBInfo {
00055         AlpChannelConnectCB callback;    
00056         gpointer           cbData;
00057 };
00058 
00059 typedef struct _DisconnectCBInfo DisconnectCBInfo;
00060 struct _DisconnectCBInfo {
00061         AlpChannelDisconnectCB callback;    
00062         gpointer           cbData;
00063 };
00064 
00065 typedef struct _ReceiveCBInfo ReceiveCBInfo;
00066 struct _ReceiveCBInfo {
00067         AlpMessageReceiveCB callback;    
00068         gpointer           cbData;
00069 };
00070 
00071 typedef enum _AlpConnectionStatus AlpConnectionStatus;
00072 enum _AlpConnectionStatus {
00073         ALP_IPC_CONNECTION_CREATED,
00074         ALP_IPC_CONNECTION_READY
00075 };
00076 
00077 typedef enum _AlpIPCInternalMessageID AlpIPCInternalMessageID;
00078 // NOTE: THE PUBLIC HEADER FILE DEFINES ALP_IPC_RECEIVE_ANY TO BE -1
00079 // THE INTERNAL MESSAGE IDs NEED TO NOT USE THAT VALUE
00080 enum _AlpIPCInternalMessageID {
00081         ALP_IPC_MSG_UNKNOWN = -2,
00082 };
00083 
00084 typedef struct _AlpMessageHeader AlpMessageHeader;
00085 struct _AlpMessageHeader {
00086         gint      msgVersion;           // 1 for now
00087 
00088         gint      msgID;
00089         gint      sentToken;
00090         gint      responseToken;
00091         GString*  msgFormat;
00092 };
00093 
00094 //
00095 // Opaque structure definitions (typedefs are include/alp/ipc.h)
00096 //
00097 
00098 struct _AlpChannel {
00099 
00100         // use local sockets
00101      GString*   socketName;
00102 
00103      int        socketFD;
00104 
00105         GIOChannel *gioChannel; 
00106 
00107         ConnectCBInfo*    connectCB;
00108         DisconnectCBInfo* disconnectCB;
00109  
00110         GHashTable *connectionTable;
00111 
00112         guint     inSource;
00113 
00114         gpointer  customData;              // pointer to a user-specified block of
00115                                            // data that is to be associated with this AlpChannel
00116 };
00117 
00118 struct _AlpConnection {
00119 
00120         // using local sockets
00121 
00122         int       socketFD;
00123 
00124         GIOChannel* gioChannel;
00125 
00126         AlpConnectionStatus status;
00127   
00128         gboolean  amServer;
00129         AlpConnection* connection;     // only set if amServer is TRUE
00130   
00131         guint     inSource;
00132         guint     outSource;
00133 
00134         gboolean  okToWrite;
00135 
00136         ConnectCBInfo*    connectCB;
00137         DisconnectCBInfo* disconnectCB;
00138         ReceiveCBInfo*    receiveCB;
00139 
00140         // for synchronous message receipt
00141         GMainContext*  receiveContext;
00142         GMainLoop*     receiveMainLoop;
00143 
00144         GSource*       receiveTimeoutSrc;
00145         GSource*       receiveIOWatchSrc;
00146 
00147         guint          responseTokenCounter;
00148 
00149         GArray*  receiveMessageQueue;
00150         pthread_mutex_t receiveMessageQueueMutex;
00151 
00152         gint           pid;
00153 
00154         AlpMessage*    partialMessage;
00155         guint          partialMessageRemainingSize;
00156         gchar*         partialMessageBufPtr;
00157 
00158         GArray*         sentMessageQueue;
00159         pthread_mutex_t sentMessageQueueMutex;
00160 
00161         pthread_mutex_t writeMutex;
00162 
00163         gpointer  customData;              // pointer to a user-specified block of
00164                                            // data that is to be associated with this AlpConnection
00165 
00166         AlpConnectionUsageHint hint;
00167 };
00168 
00169 // messages have three parts, an integer containing the
00170 // size of the buffer that was sent, followed by 
00171 // a GNet-packed message header, followed by the
00172 // GNet-packed message data
00173 struct _AlpMessage {
00174         AlpMessageHeader header;
00175 
00176         gchar*   bufPtr;
00177         guint    bufSize;  // total buffer size including
00178         // the header, message and prepended buffer size
00179 
00180         gchar*   headerPtr; // pointer to the character in the buffer
00181         // where the header data starts
00182         guint    headerSize;
00183   
00184         gchar*   msgPtr;    // pointer to the character in the buffer
00185         // where the message data starts
00186         guint    msgSize;
00187  
00188         guint    remainingSize;
00189         guint    allocBufSize;   // allocated size can be different
00190         // from the used bufSize
00191 
00192         guint    formatIndex;    // used for unpacking
00193         gchar*   currMsgPtr;     // used for packing and unpacking
00194 
00195         guint numBytesSent;
00196 };
00197 
00198 // 
00199 // Internal structure definitions
00200 //
00201 typedef struct _SynchReceiveData SynchReceiveData;
00202 struct _SynchReceiveData {
00203         AlpConnection* connection;
00204         gint           messageID;
00205         gint           sentToken;
00206         guint          timeout;
00207         AlpMessage**   message;
00208 
00209         alp_status_t   status;
00210 };
00211 
00212 #define DEFAULT_CHANNEL_DIRECTORY "/tmp/"
00213 
00214 #define DEFAULT_FORMAT_STRING_LENGTH 32
00215 #define DEFAULT_BUFFER_SIZE          1024
00216 
00217 //
00218 // Internal functions
00219 //
00220 
00221 AlpChannel* 
00222 prv_channel_create();
00223 
00224 void        
00225 prv_channel_destroy(AlpChannel* channel);
00226 
00227 AlpConnection* 
00228 prv_connection_create(AlpConnectionUsageHint hint);
00229 
00230 void           
00231 prv_connection_destroy(AlpConnection* connection);
00232 
00233 AlpMessage*    
00234 prv_message_create();
00235 
00236 void           
00237 prv_message_destroy(AlpMessage* message);
00238 
00239 GIOStatus
00240 prv_get_msg_header(AlpConnection* connection, AlpMessage** msg, GIOChannel* source);
00241 
00242 void
00243 prv_invoke_messsage_callback(AlpConnection* connection, AlpMessage* message);
00244 
00245 gboolean
00246 prv_server_accept_CB(GIOChannel* source, GIOCondition condition, gpointer data);
00247 gboolean
00248 prv_server_received_input_hangup_CB(GIOChannel* source, GIOCondition condition, gpointer data);
00249 
00250 gboolean
00251 prv_client_received_input_hangup_CB(GIOChannel* source, GIOCondition condition, gpointer data);
00252 
00253 gboolean
00254 prv_received_output_hangup_CB(GIOChannel* source, GIOCondition condition, gpointer data);
00255 
00256 gboolean 
00257 prv_connection_table_remove_CB(gpointer key, gpointer value, gpointer userData);
00258 
00259 gboolean
00260 prv_finish_packing(AlpMessage* message);
00261 
00262 gboolean
00263 prv_repack_header(AlpMessage* message);
00264 
00265 gboolean
00266 prv_realloc_message_buffer(AlpMessage* message, guint numBytesNeeded);
00267 
00268 //
00269 // Functions for doing synchronous message receive in a separate thread
00270 //
00271 gboolean
00272 prv_receive_func_timeout_CB(gpointer data);
00273 
00274 gboolean
00275 prv_receive_func_input_hangup_CB(GIOChannel* source, 
00276                                                    GIOCondition condition, 
00277                                                    gpointer data);
00278 
00279 alp_status_t
00280 prv_receive_func(SynchReceiveData* arg);
00281 
00282 alp_status_t
00283 prv_send_with_response(AlpConnection* connection,
00284                                         AlpMessage*    messageToSend,
00285                                         guint          timeout,
00286                                         AlpMessage**   responseMessage);
00287 
00288 alp_status_t
00289 prv_wait_for_synch_receive(AlpConnection* connection, 
00290                                                     gint messageID,
00291                                                     guint timeout, 
00292                                                     AlpMessage** message);
00293 
00294 alp_status_t
00295 prv_queue_received_message(AlpConnection* connection, AlpMessage* message);
00296 
00297 gboolean 
00298 prv_idle_send_queued_messages_CB(gpointer data);
00299 
00300 alp_status_t
00301 prv_send_queued_messages(AlpConnection* connection);
00302 
00303 alp_status_t
00304 prv_check_for_queued_message(AlpConnection* connection,
00305                                             gint msgID,
00306                                             AlpMessage** message);
00307 
00308 alp_status_t
00309 prv_queue_sent_message(AlpConnection* connection, AlpMessage* message);
00310 
00311 void
00312 prv_install_SIGPIPE_handler(struct sigaction* oldHandler);
00313 
00314 void
00315 prv_restore_SIGPIPE_handler(struct sigaction* oldHandler);
00316 
00317 void
00318 prv_install_g_io_out_callback(AlpConnection* connection);
00319 
00320 void
00321 prv_make_dirs(GString* path);
00322 
00323 void
00324 prv_clean_up_old_socket(const gchar* channelName, const GString* socketName);
00325 
00326 void
00327 prv_server_socket_name(const gchar* channelName, GString* socketFileName);
00328 
00329 gboolean
00330 prv_set_up_client_connection(AlpConnection* newConnection);
00331 
00332 gboolean
00333 prv_wait_for_connect_CB(GIOChannel* source, 
00334                                     GIOCondition condition, gpointer data);
00335 
00336 gint
00337 prv_try_to_connect(AlpConnection* connection, 
00338                             const gchar* channelName,
00339                             gboolean isRetry,
00340                             gint timeout,
00341                             alp_status_t* status);
00342 
00343 #endif // #ifndef ALP_IPC_INTERNAL_H_
00344 

Generated on Sat Dec 16 20:29:47 2006 for hiker-0.9 by  doxygen 1.4.4