
00001 /*********************************************************************************************************** 00002 Callbacks for converse messages. Sameer Paranjpye 2/24/2000. 00003 This module defines an interface for registering callbacks for converse messages. 00004 This is meant to be a machine level interface through which additional message processing capabilities can 00005 be added to converse. The idea is that if a message needs additional processing when it is sent or received 00006 such as encryption/decryption, maintaining quiescence counters etc., then a callback is registered that provides 00007 this processing capability. Its only current application is keeping quiescence counts. 00008 00009 For now I'm assuming that callbacks are only registered in pairs, for every send callback there is a receice 00010 callback. But this can be easily changed by making CMsgRegisterCallback non-static. 00011 00012 CMsgCallbacksInit - Initializes the callback mechanism. 00013 00014 CMsgRegisterCallbackPair - Registers a message callback pair, one at the send side one at the recv side. 00015 00016 CMsgInvokeCallbacks - Invokes registered callbacks 00017 00018 ************************************************************************************************************/ 00019 00020 00021 #include "converse.h" 00022 #define CALLBACKSETSIZE 5 00023 #define MAXCALLBACKSETS 5 00024 #define SENDCALLBACK 0 00025 #define RECVCALLBACK 1 00026 00027 typedef struct CMsgCallback { 00028 CMsgProcFn fn; 00029 } CMSGCALLBACK; 00030 00031 struct CMsgCallbackQ { 00032 CMSGCALLBACK **cbQ; 00033 int size; 00034 }; 00035 00036 struct CMsgCallbackQ cMsgSendCbQ; 00037 struct CMsgCallbackQ cMsgRecvCbQ; 00038 00039 void CMsgCallbacksInit(void) 00040 { 00041 cMsgSendCbQ.cbQ = (CMSGCALLBACK **) malloc(MAXCALLBACKSETS* 00042 sizeof(CMSGCALLBACK*)); 00043 cMsgSendCbQ.size = 0; 00044 cMsgRecvCbQ.cbQ = (CMSGCALLBACK **) malloc(MAXCALLBACKSETS* 00045 sizeof(CMSGCALLBACK*)); 00046 cMsgRecvCbQ.size = 0; 00047 } 00048 00049 static int CMsgRegisterCallback(CMsgProcFn fnp, int type) 00050 { 00051 int setNum, setIdx; 00052 struct CMsgCallbackQ* Q; 00053 00054 if (type < 2) 00055 Q = (type)? (&cMsgRecvCbQ) : (&cMsgSendCbQ); 00056 else 00057 { 00058 CmiPrintf("Unknown callback type, cannot register"); 00059 return -1; 00060 } 00061 00062 setNum = Q->size/CALLBACKSETSIZE; 00063 setIdx = Q->size%CALLBACKSETSIZE; 00064 00065 if (setNum >= MAXCALLBACKSETS) { 00066 CmiPrintf("Too many message callbacks, cannot register\n"); 00067 return -1; 00068 } 00069 00070 if (!setIdx) 00071 Q->cbQ[setNum] = (CMSGCALLBACK *) malloc(CALLBACKSETSIZE*sizeof(CMSGCALLBACK)); 00072 00073 (Q->cbQ[setNum])[setIdx].fn = fnp; 00074 Q->size++; 00075 00076 return 0; 00077 } 00078 00079 int CMsgRegisterCallbackPair(CMsgProcFn sendFn, CMsgProcFn recvFn) 00080 { 00081 if(CMsgRegisterCallback(sendFn, SENDCALLBACK) < 0) 00082 return -1; 00083 if(CMsgRegisterCallback(recvFn, RECVCALLBACK) < 0) 00084 return -1; 00085 return 0; 00086 } 00087 00088 void CMsgInvokeCallbacks(int type, char *msg) 00089 { 00090 int i; 00091 struct CMsgCallbackQ* Q; 00092 00093 if (type < 2) 00094 Q = (type)? (&cMsgRecvCbQ) : (&cMsgSendCbQ); 00095 else 00096 { 00097 CmiPrintf("Unknown callback type, cannot invoke"); 00098 return; 00099 } 00100 00101 for(i=0; i < Q->size; i++) 00102 ((Q->cbQ[i/CALLBACKSETSIZE])[i%CALLBACKSETSIZE].fn)(msg); 00103 }
1.5.5