- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
136
asyncprotocol.c
136
asyncprotocol.c
@ -2,7 +2,8 @@
|
||||
#include <asyncprotocol.h>
|
||||
#include <asyncqueue.h>
|
||||
|
||||
int defaultSendCommand(pAsyncProtocol p, pAsyncTxn txn) {
|
||||
int defaultSendCommand(pAsyncProtocol p, pAsyncTxn txn)
|
||||
{
|
||||
int i, iRet;
|
||||
int state;
|
||||
const char *term = "\r\n";
|
||||
@ -10,10 +11,9 @@ int defaultSendCommand(pAsyncProtocol p, pAsyncTxn txn) {
|
||||
term = p->sendTerminator;
|
||||
state = 0;
|
||||
for (i = 0; i < txn->out_len; ++i) {
|
||||
if (txn->out_buf[i] == 0x00) { /* end of transmission */
|
||||
if (txn->out_buf[i] == 0x00) { /* end of transmission */
|
||||
break;
|
||||
}
|
||||
else if (txn->out_buf[i] == term[state]) {
|
||||
} else if (txn->out_buf[i] == term[state]) {
|
||||
++state;
|
||||
continue;
|
||||
}
|
||||
@ -24,11 +24,12 @@ int defaultSendCommand(pAsyncProtocol p, pAsyncTxn txn) {
|
||||
if (iRet <= 0)
|
||||
return iRet;
|
||||
if (term[state] != 0)
|
||||
iRet = AsyncUnitWrite(txn->unit,(void *) term, strlen(term));
|
||||
iRet = AsyncUnitWrite(txn->unit, (void *) term, strlen(term));
|
||||
return iRet;
|
||||
}
|
||||
|
||||
int defaultHandleInput(pAsyncProtocol p, pAsyncTxn txn, int ch) {
|
||||
int defaultHandleInput(pAsyncProtocol p, pAsyncTxn txn, int ch)
|
||||
{
|
||||
const char *term = "\r\n";
|
||||
if (p->replyTerminator)
|
||||
term = p->replyTerminator;
|
||||
@ -47,12 +48,15 @@ int defaultHandleInput(pAsyncProtocol p, pAsyncTxn txn, int ch) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int defaultHandleEvent(pAsyncProtocol p, pAsyncTxn txn, int event) {
|
||||
int defaultHandleEvent(pAsyncProtocol p, pAsyncTxn txn, int event)
|
||||
{
|
||||
/* TODO: what could or should we do to handle the event */
|
||||
return AQU_POP_CMD;
|
||||
}
|
||||
|
||||
int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_len, int rsp_len) {
|
||||
int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char *cmd,
|
||||
int cmd_len, int rsp_len)
|
||||
{
|
||||
int i;
|
||||
int state;
|
||||
const char *term = "\r\n";
|
||||
@ -60,11 +64,10 @@ int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_
|
||||
term = p->sendTerminator;
|
||||
state = 0;
|
||||
for (i = 0; i < cmd_len; ++i) {
|
||||
if (cmd[i] == 0x00) { /* end of transmission */
|
||||
if (cmd[i] == 0x00) { /* end of transmission */
|
||||
cmd_len = i;
|
||||
break;
|
||||
}
|
||||
else if (cmd[i] == term[state]) {
|
||||
} else if (cmd[i] == term[state]) {
|
||||
++state;
|
||||
continue;
|
||||
}
|
||||
@ -74,17 +77,18 @@ int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_
|
||||
/* outgoing command is correctly terminated */
|
||||
txn->out_buf = malloc(cmd_len + 1);
|
||||
if (txn->out_buf == NULL) {
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn", eError);
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
memcpy(txn->out_buf, cmd, cmd_len + 1);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
/* outgoing command is NOT correctly terminated */
|
||||
int tlen = strlen(term);
|
||||
txn->out_buf = malloc(cmd_len + tlen + 1);
|
||||
if (txn->out_buf == NULL) {
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn", eError);
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
memcpy(txn->out_buf, cmd, cmd_len);
|
||||
@ -93,12 +97,13 @@ int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_
|
||||
}
|
||||
txn->out_len = cmd_len;
|
||||
txn->out_idx = 0;
|
||||
if(txn->inp_buf != NULL){
|
||||
if (txn->inp_buf != NULL) {
|
||||
free(txn->inp_buf);
|
||||
}
|
||||
txn->inp_buf = malloc(rsp_len);
|
||||
if (txn->inp_buf == NULL) {
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn", eError);
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::defaultPrepareTxn",
|
||||
eError);
|
||||
free(txn->out_buf);
|
||||
txn->out_buf = NULL;
|
||||
return 0;
|
||||
@ -110,7 +115,7 @@ int defaultPrepareTxn(pAsyncProtocol p, pAsyncTxn txn, const char* cmd, int cmd_
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
|
||||
/*--------------------------------------------------------------------*/
|
||||
static void encodeTerminator(char *result, char *terminator)
|
||||
@ -119,15 +124,16 @@ static void encodeTerminator(char *result, char *terminator)
|
||||
while (*terminator) {
|
||||
*result++ = '0';
|
||||
*result++ = 'x';
|
||||
*result++ = hex[(*terminator >> 4) &0xF];
|
||||
*result++ = hex[(*terminator) &0xF];
|
||||
*result++ = hex[(*terminator >> 4) & 0xF];
|
||||
*result++ = hex[(*terminator) & 0xF];
|
||||
++terminator;
|
||||
}
|
||||
*result = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
static int fromHex(const char* code) {
|
||||
static int fromHex(const char *code)
|
||||
{
|
||||
int icode = -1;
|
||||
int result = -1;
|
||||
if (code[0] == '0' && (code[1] == 'x' || code[1] == 'X')) {
|
||||
@ -154,53 +160,52 @@ static int fromHex(const char* code) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------*/
|
||||
static char *decodeTerminator(char *code)
|
||||
{
|
||||
int count = 0, icode;
|
||||
char *pResult;
|
||||
char* pCh;
|
||||
char* pQt = NULL; /* pointer to quote character if found */
|
||||
char *pCh;
|
||||
char *pQt = NULL; /* pointer to quote character if found */
|
||||
|
||||
if (code == NULL)
|
||||
return NULL;
|
||||
count = strlen(code);
|
||||
pResult = (char *) malloc(count + 1);
|
||||
if (!pResult) {
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::decodeTerminator", eError);
|
||||
SICSLogWrite("Out of memory in AsyncProtocol::decodeTerminator",
|
||||
eError);
|
||||
return NULL;
|
||||
}
|
||||
memset(pResult, 0, count + 1);
|
||||
|
||||
pCh = pResult;
|
||||
if (*code == '\'' || *code == '"') /* check for leading quote */
|
||||
if (*code == '\'' || *code == '"') /* check for leading quote */
|
||||
pQt = code++;
|
||||
|
||||
while (*code) {
|
||||
if (pQt && *code == *pQt) /* check for trailing quote */
|
||||
if (pQt && *code == *pQt) /* check for trailing quote */
|
||||
break;
|
||||
|
||||
if (code[0] == '\\' && code[1] == 'r') { /* CR */
|
||||
if (code[0] == '\\' && code[1] == 'r') { /* CR */
|
||||
*pCh++ = '\r';
|
||||
code += 2;
|
||||
}
|
||||
else if (code[0] == '\\' && code[1] == 'n') { /* LF */
|
||||
} else if (code[0] == '\\' && code[1] == 'n') { /* LF */
|
||||
*pCh++ = '\n';
|
||||
code += 2;
|
||||
}
|
||||
else if ((icode = fromHex(code)) >= 0) { /* Hex: 0xFF */
|
||||
} else if ((icode = fromHex(code)) >= 0) { /* Hex: 0xFF */
|
||||
*pCh++ = icode;
|
||||
code += 4;
|
||||
}
|
||||
else /* literal */
|
||||
} else /* literal */
|
||||
*pCh++ = *code++;
|
||||
}
|
||||
*pCh = '\0';
|
||||
|
||||
return pResult;
|
||||
}
|
||||
int AsyncProtocolNoAction(SConnection *pCon, SicsInterp *pSics,
|
||||
void *pData, int argc, char *argv[])
|
||||
int AsyncProtocolNoAction(SConnection * pCon, SicsInterp * pSics,
|
||||
void *pData, int argc, char *argv[])
|
||||
{
|
||||
char line[132];
|
||||
pAsyncProtocol self = (pAsyncProtocol) pData;
|
||||
@ -209,7 +214,7 @@ int AsyncProtocolNoAction(SConnection *pCon, SicsInterp *pSics,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
|
||||
int AsyncProtocolAction(SConnection * pCon, SicsInterp * pSics,
|
||||
void *pData, int argc, char *argv[])
|
||||
{
|
||||
char line[132];
|
||||
@ -218,16 +223,14 @@ int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
|
||||
/* handle genecic parameters like terminators */
|
||||
if (strcasecmp(argv[1], "sendterminator") == 0) {
|
||||
if (argc > 2) {
|
||||
char* pPtr = decodeTerminator(argv[2]);
|
||||
char *pPtr = decodeTerminator(argv[2]);
|
||||
if (pPtr) {
|
||||
if (self->sendTerminator)
|
||||
free(self->sendTerminator);
|
||||
self->sendTerminator = pPtr;
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
char term[132];
|
||||
char line[1024];
|
||||
encodeTerminator(term, self->sendTerminator);
|
||||
@ -235,19 +238,16 @@ int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
|
||||
SCWrite(pCon, line, eValue);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else if (strcasecmp(argv[1], "replyterminator") == 0) {
|
||||
} else if (strcasecmp(argv[1], "replyterminator") == 0) {
|
||||
if (argc > 2) {
|
||||
char* pPtr = decodeTerminator(argv[2]);
|
||||
char *pPtr = decodeTerminator(argv[2]);
|
||||
if (pPtr) {
|
||||
if (self->replyTerminator)
|
||||
free(self->replyTerminator);
|
||||
self->replyTerminator = pPtr;
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
char term[132];
|
||||
char line[1024];
|
||||
encodeTerminator(term, self->replyTerminator);
|
||||
@ -256,10 +256,9 @@ int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (strcasecmp(argv[1], "list") == 0) {
|
||||
} else if (strcasecmp(argv[1], "list") == 0) {
|
||||
int ac = 2;
|
||||
char* av[3] = { argv[0], 0, 0 };
|
||||
char *av[3] = { argv[0], 0, 0 };
|
||||
av[1] = "sendterminator";
|
||||
AsyncProtocolAction(pCon, pSics, pData, ac, av);
|
||||
av[1] = "replyterminator";
|
||||
@ -267,35 +266,41 @@ int AsyncProtocolAction(SConnection *pCon, SicsInterp *pSics,
|
||||
return 1;
|
||||
}
|
||||
/* handle any other actions here */
|
||||
return AsyncProtocolNoAction(pCon, pSics, pData, argc,argv);
|
||||
return AsyncProtocolNoAction(pCon, pSics, pData, argc, argv);
|
||||
}
|
||||
|
||||
void defaultKillPrivate(pAsyncProtocol p) {
|
||||
void defaultKillPrivate(pAsyncProtocol p)
|
||||
{
|
||||
if (p->privateData) {
|
||||
/* TODO: should we do anything? */
|
||||
free(p->privateData);
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncProtocolKill(void *pData) {
|
||||
void AsyncProtocolKill(void *pData)
|
||||
{
|
||||
pAsyncProtocol self = (pAsyncProtocol) pData;
|
||||
if(self->pDes)
|
||||
if (self->pDes)
|
||||
DeleteDescriptor(self->pDes);
|
||||
if(self->sendTerminator != NULL)
|
||||
if (self->sendTerminator != NULL)
|
||||
free(self->sendTerminator);
|
||||
if(self->replyTerminator != NULL)
|
||||
if (self->replyTerminator != NULL)
|
||||
free(self->replyTerminator);
|
||||
if (self->killPrivate)
|
||||
self->killPrivate(self);
|
||||
}
|
||||
|
||||
pAsyncProtocol AsyncProtocolCreate(SicsInterp *pSics, const char* protocolName,
|
||||
ObjectFunc pFunc, KillFunc pKFunc) {
|
||||
pAsyncProtocol AsyncProtocolCreate(SicsInterp * pSics,
|
||||
const char *protocolName,
|
||||
ObjectFunc pFunc, KillFunc pKFunc)
|
||||
{
|
||||
int iRet;
|
||||
pAsyncProtocol self = NULL;
|
||||
|
||||
/* try to find an existing queue with this name */
|
||||
self = (pAsyncProtocol) FindCommandData(pServ->pSics,(char *)protocolName, "AsyncProtocol");
|
||||
self =
|
||||
(pAsyncProtocol) FindCommandData(pServ->pSics, (char *) protocolName,
|
||||
"AsyncProtocol");
|
||||
if (self != NULL) {
|
||||
return self;
|
||||
}
|
||||
@ -311,8 +316,8 @@ pAsyncProtocol AsyncProtocolCreate(SicsInterp *pSics, const char* protocolName,
|
||||
pFunc = AsyncProtocolNoAction;
|
||||
if (pKFunc == NULL)
|
||||
pKFunc = AsyncProtocolKill;
|
||||
iRet = AddCommand(pSics, (char *)protocolName, pFunc, pKFunc, self);
|
||||
if (!iRet ) {
|
||||
iRet = AddCommand(pSics, (char *) protocolName, pFunc, pKFunc, self);
|
||||
if (!iRet) {
|
||||
SICSLogWrite("AddCommand failed in AsyncProtocolCreate", eError);
|
||||
AsyncProtocolKill(self);
|
||||
return NULL;
|
||||
@ -327,14 +332,17 @@ pAsyncProtocol AsyncProtocolCreate(SicsInterp *pSics, const char* protocolName,
|
||||
return self;
|
||||
}
|
||||
|
||||
int AsyncProtocolFactory(SConnection *pCon, SicsInterp *pSics,
|
||||
void *pData, int argc, char *argv[]) {
|
||||
int AsyncProtocolFactory(SConnection * pCon, SicsInterp * pSics,
|
||||
void *pData, int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
SCWrite(pCon,"ERROR: insufficient arguments to AsyncProtocolFactory", eError);
|
||||
SCWrite(pCon, "ERROR: insufficient arguments to AsyncProtocolFactory",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pAsyncProtocol pNew = AsyncProtocolCreate(pSics, argv[1],
|
||||
AsyncProtocolAction, AsyncProtocolKill);
|
||||
AsyncProtocolAction,
|
||||
AsyncProtocolKill);
|
||||
/* handle any extra arguments here */
|
||||
pNew->privateData = NULL;
|
||||
return 1;
|
||||
|
Reference in New Issue
Block a user