Adjustments for handling binary protocols
r2267 | dcl | 2008-01-15 12:17:58 +1100 (Tue, 15 Jan 2008) | 2 lines
This commit is contained in:
@@ -49,7 +49,7 @@ struct __async_protocol {
|
|||||||
pObjectDescriptor pDes;
|
pObjectDescriptor pDes;
|
||||||
char* protocolName;
|
char* protocolName;
|
||||||
char *sendTerminator;
|
char *sendTerminator;
|
||||||
char *replyTerminator;
|
char *replyTerminator[10];
|
||||||
void* privateData;
|
void* privateData;
|
||||||
int (* sendCommand)(pAsyncProtocol p, pAsyncTxn txn);
|
int (* sendCommand)(pAsyncProtocol p, pAsyncTxn txn);
|
||||||
int (* handleInput)(pAsyncProtocol p, pAsyncTxn txn, int ch);
|
int (* handleInput)(pAsyncProtocol p, pAsyncTxn txn, int ch);
|
||||||
|
|||||||
141
asyncqueue.c
141
asyncqueue.c
@@ -15,11 +15,13 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <sics.h>
|
#include <sics.h>
|
||||||
#include <rs232controller.h>
|
#include <rs232controller.h>
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "asyncqueue.h"
|
#include "asyncqueue.h"
|
||||||
#include "nwatch.h"
|
#include "nwatch.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct __AsyncQueue AsyncQueue, *pAsyncQueue;
|
typedef struct __AsyncQueue AsyncQueue, *pAsyncQueue;
|
||||||
|
|
||||||
@@ -49,6 +51,7 @@ struct __AsyncQueue {
|
|||||||
int iDelay; /* intercommand delay in milliseconds */
|
int iDelay; /* intercommand delay in milliseconds */
|
||||||
int timeout;
|
int timeout;
|
||||||
int retries;
|
int retries;
|
||||||
|
bool translate; /* translate binary output with escaped chars */
|
||||||
struct timeval tvLastCmd; /* time of completion of last command */
|
struct timeval tvLastCmd; /* time of completion of last command */
|
||||||
int unit_count; /* number of units connected */
|
int unit_count; /* number of units connected */
|
||||||
pAsyncUnit units; /* head of unit chain */
|
pAsyncUnit units; /* head of unit chain */
|
||||||
@@ -399,19 +402,23 @@ pAsyncTxn AsyncUnitPrepareTxn(pAsyncUnit unit,
|
|||||||
myTxn = (pAsyncTxn) malloc(sizeof(AsyncTxn));
|
myTxn = (pAsyncTxn) malloc(sizeof(AsyncTxn));
|
||||||
if (myTxn == NULL) {
|
if (myTxn == NULL) {
|
||||||
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
memset(myTxn, 0, sizeof(AsyncTxn));
|
memset(myTxn, 0, sizeof(AsyncTxn));
|
||||||
if (unit->queue->protocol->prepareTxn) {
|
if (unit->queue->protocol->prepareTxn) {
|
||||||
int iRet;
|
int iRet;
|
||||||
iRet = unit->queue->protocol->prepareTxn(unit->queue->protocol, myTxn, command, cmd_len, rsp_len);
|
iRet = unit->queue->protocol->prepareTxn(unit->queue->protocol, myTxn, command, cmd_len, rsp_len);
|
||||||
|
if (iRet == 0) {
|
||||||
|
free(myTxn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
myTxn->out_buf = (char*) malloc(cmd_len + 5);
|
myTxn->out_buf = (char*) malloc(cmd_len + 5);
|
||||||
if (myTxn->out_buf == NULL) {
|
if (myTxn->out_buf == NULL) {
|
||||||
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
||||||
free(myTxn);
|
free(myTxn);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
memcpy(myTxn->out_buf, command, cmd_len);
|
memcpy(myTxn->out_buf, command, cmd_len);
|
||||||
myTxn->out_len = cmd_len;
|
myTxn->out_len = cmd_len;
|
||||||
@@ -431,7 +438,7 @@ pAsyncTxn AsyncUnitPrepareTxn(pAsyncUnit unit,
|
|||||||
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
SICSLogWrite("ERROR: Out of memory in AsyncUnitPrepareTxn", eError);
|
||||||
free(myTxn->out_buf);
|
free(myTxn->out_buf);
|
||||||
free(myTxn);
|
free(myTxn);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
memset(myTxn->inp_buf, 0, rsp_len + 1);
|
memset(myTxn->inp_buf, 0, rsp_len + 1);
|
||||||
}
|
}
|
||||||
@@ -459,6 +466,7 @@ int AsyncUnitSendTxn(pAsyncUnit unit,
|
|||||||
typedef struct txn_s {
|
typedef struct txn_s {
|
||||||
char* transReply;
|
char* transReply;
|
||||||
int transWait;
|
int transWait;
|
||||||
|
int respLen;
|
||||||
} TXN, *pTXN;
|
} TXN, *pTXN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -469,33 +477,37 @@ static int TransCallback(pAsyncTxn pCmd) {
|
|||||||
int resp_len = pCmd->inp_idx;
|
int resp_len = pCmd->inp_idx;
|
||||||
pTXN self = (pTXN) pCmd->cntx;
|
pTXN self = (pTXN) pCmd->cntx;
|
||||||
|
|
||||||
if (pCmd->txn_status == ATX_TIMEOUT) {
|
self->respLen = resp_len;
|
||||||
|
if (resp_len > 0) {
|
||||||
memcpy(self->transReply, resp, resp_len);
|
memcpy(self->transReply, resp, resp_len);
|
||||||
self->transReply[resp_len] = '\0';
|
self->transReply[resp_len] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
self->transReply[0] = '\0';
|
self->transReply[0] = '\0';
|
||||||
|
|
||||||
|
if (pCmd->txn_status == ATX_TIMEOUT)
|
||||||
self->transWait = -1;
|
self->transWait = -1;
|
||||||
}
|
else
|
||||||
else {
|
|
||||||
memcpy(self->transReply, resp, resp_len);
|
|
||||||
self->transReply[resp_len] = '\0';
|
|
||||||
self->transWait = 0;
|
self->transWait = 0;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AsyncUnitTransact(pAsyncUnit unit,
|
int AsyncUnitTransact(pAsyncUnit unit,
|
||||||
const char* command, int cmd_len,
|
const char* command, int cmd_len,
|
||||||
char* response, int rsp_len)
|
char* response, int *rsp_len)
|
||||||
{
|
{
|
||||||
TXN txn;
|
TXN txn;
|
||||||
assert(unit);
|
assert(unit);
|
||||||
txn.transReply = response;
|
txn.transReply = response;
|
||||||
|
txn.respLen = *rsp_len;
|
||||||
txn.transWait = 1;
|
txn.transWait = 1;
|
||||||
AsyncUnitSendTxn(unit,
|
AsyncUnitSendTxn(unit,
|
||||||
command, cmd_len,
|
command, cmd_len,
|
||||||
TransCallback, &txn, rsp_len);
|
TransCallback, &txn, *rsp_len);
|
||||||
while (txn.transWait == 1)
|
while (txn.transWait == 1)
|
||||||
TaskYield(pServ->pTasker);
|
TaskYield(pServ->pTasker);
|
||||||
|
*rsp_len = txn.respLen;
|
||||||
if (txn.transWait < 0)
|
if (txn.transWait < 0)
|
||||||
return txn.transWait;
|
return txn.transWait;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -601,20 +613,72 @@ int AsyncQueueAction(SConnection *pCon, SicsInterp *pSics,
|
|||||||
char cmd[10240];
|
char cmd[10240];
|
||||||
char rsp[10240];
|
char rsp[10240];
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
int i, j;
|
int i, j, len;
|
||||||
cmd[0] = '\0';
|
cmd[0] = '\0';
|
||||||
for (i = 2; i < argc; ++i) {
|
for (i = 2; i < argc; ++i) {
|
||||||
j = snprintf(&cmd[idx], 10240 - idx, "%s%s",
|
if (idx >= 10240)
|
||||||
(i > 2) ? " " : "",
|
|
||||||
argv[i]);
|
|
||||||
if (j < 0)
|
|
||||||
break;
|
break;
|
||||||
idx += j;
|
if (i > 2)
|
||||||
|
cmd[idx++] = ' ';
|
||||||
|
len = strlen(argv[i]);
|
||||||
|
for (j = 0; j < len; ++j) {
|
||||||
|
if (idx >= 10240)
|
||||||
|
break;
|
||||||
|
if (argv[i][j] == '\\') {
|
||||||
|
++j;
|
||||||
|
if (argv[i][j] == '\\')
|
||||||
|
cmd[idx++] = '\\';
|
||||||
|
else if (argv[i][j] == 'r')
|
||||||
|
cmd[idx++] = '\r';
|
||||||
|
else if (argv[i][j] == 'n')
|
||||||
|
cmd[idx++] = '\n';
|
||||||
|
else if (isdigit(argv[i][j])) {
|
||||||
|
char* nptr = &argv[i][j];
|
||||||
|
char* endptr;
|
||||||
|
long k = strtol(nptr, &endptr, 0);
|
||||||
|
cmd[idx++] = k;
|
||||||
|
j += (endptr - nptr);
|
||||||
|
--j; /* prepare for loop increment */
|
||||||
|
} else
|
||||||
|
cmd[idx++] = argv[i][j];
|
||||||
|
} else
|
||||||
|
cmd[idx++] = argv[i][j];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
cmd[idx] = '\0';
|
||||||
memset(&myUnit, 0, sizeof(AsyncUnit));
|
memset(&myUnit, 0, sizeof(AsyncUnit));
|
||||||
myUnit.queue = self;
|
myUnit.queue = self;
|
||||||
AsyncUnitTransact(&myUnit, cmd, idx, rsp, 10240);
|
len = 10240;
|
||||||
SCWrite(pCon, rsp, eValue);
|
(void) AsyncUnitTransact(&myUnit, cmd, idx, rsp, &len);
|
||||||
|
/* escape control characters in response */
|
||||||
|
j = 0;
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
if (j >= 10230)
|
||||||
|
break;
|
||||||
|
if (self->translate) {
|
||||||
|
if (rsp[i] < 32 || rsp[i] >= 127) {
|
||||||
|
if (rsp[i] == '\r') {
|
||||||
|
cmd[j++] = '\r';
|
||||||
|
}
|
||||||
|
else if (rsp[i] == '\n') {
|
||||||
|
cmd[j++] = '\n';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
j += snprintf(&cmd[j], 6, "\\0x%02x", rsp[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rsp[i] == '\\') {
|
||||||
|
cmd[j++] = '\\';
|
||||||
|
cmd[j++] = '\\';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd[j++] = rsp[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cmd[j++] = rsp[i];
|
||||||
|
}
|
||||||
|
cmd[j++] = '\0';
|
||||||
|
SCWrite(pCon, cmd, eValue);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (strcasecmp(argv[1], "reconnect") == 0) {
|
if (strcasecmp(argv[1], "reconnect") == 0) {
|
||||||
@@ -632,7 +696,7 @@ int AsyncQueueAction(SConnection *pCon, SicsInterp *pSics,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (delay < 0 || delay > 30000) {
|
if (delay < 0 || delay > 300000) {
|
||||||
snprintf(line, 132, "Value out of range: %d", delay);
|
snprintf(line, 132, "Value out of range: %d", delay);
|
||||||
SCWrite(pCon, line, eError);
|
SCWrite(pCon, line, eError);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -702,6 +766,37 @@ int AsyncQueueAction(SConnection *pCon, SicsInterp *pSics,
|
|||||||
}
|
}
|
||||||
return OKOK;
|
return OKOK;
|
||||||
}
|
}
|
||||||
|
if (strcasecmp(argv[1], "translate") == 0) {
|
||||||
|
if (argc > 2) {
|
||||||
|
int value;
|
||||||
|
int iRet;
|
||||||
|
iRet = sscanf(argv[2], "%d", &value);
|
||||||
|
if (iRet != 1) {
|
||||||
|
snprintf(line, 132, "Invalid argument: %s", argv[2]);
|
||||||
|
SCWrite(pCon, line, eError);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (value == 0) {
|
||||||
|
self->translate = false;
|
||||||
|
return OKOK;
|
||||||
|
}
|
||||||
|
else if (value == 1) {
|
||||||
|
self->timeout = true;
|
||||||
|
return OKOK;
|
||||||
|
}
|
||||||
|
snprintf(line, 132, "Invalid argument: %s", argv[2]);
|
||||||
|
SCWrite(pCon, line, eError);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
snprintf(line, 132, "%s.timeout = %d", argv[0], self->timeout);
|
||||||
|
SCWrite(pCon, line, eStatus);
|
||||||
|
return OKOK;
|
||||||
|
}
|
||||||
|
return OKOK;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
snprintf(line, 132, "%s does not understand %s", argv[0], argv[1]);
|
snprintf(line, 132, "%s does not understand %s", argv[0], argv[1]);
|
||||||
SCWrite(pCon, line, eError);
|
SCWrite(pCon, line, eError);
|
||||||
@@ -718,7 +813,7 @@ static pAsyncQueue AQ_Create(const char* host, const char* port)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* try the AsyncQueue with this name */
|
/* try the AsyncQueue with this name */
|
||||||
self = (pAsyncQueue) FindCommandData(pServ->pSics, host, "AsyncQueue");
|
self = (pAsyncQueue) FindCommandData(pServ->pSics, (char*) host, "AsyncQueue");
|
||||||
|
|
||||||
/* try host and port */
|
/* try host and port */
|
||||||
if (self == NULL && port) {
|
if (self == NULL && port) {
|
||||||
@@ -731,7 +826,7 @@ static pAsyncQueue AQ_Create(const char* host, const char* port)
|
|||||||
}
|
}
|
||||||
if (port_no > 0) {
|
if (port_no > 0) {
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in sa;
|
||||||
if (CreateSocketAdress(&sa, host, port_no)) {
|
if (CreateSocketAdress(&sa, (char *)host, port_no)) {
|
||||||
/* look for queue with same address */
|
/* look for queue with same address */
|
||||||
for (i = 0; i < queue_index; ++i)
|
for (i = 0; i < queue_index; ++i)
|
||||||
if (queue_array[i]->pSock->adresse.sin_port == sa.sin_port
|
if (queue_array[i]->pSock->adresse.sin_port == sa.sin_port
|
||||||
@@ -741,7 +836,7 @@ static pAsyncQueue AQ_Create(const char* host, const char* port)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (self == NULL) {
|
if (self == NULL) {
|
||||||
channel = NETConnectWithFlags(host, port_no, 0);
|
channel = NETConnectWithFlags((char *)host, port_no, 0);
|
||||||
/* TODO handle asynchronous connection */
|
/* TODO handle asynchronous connection */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user