PSI update
r1464 | ffr | 2007-02-12 12:20:21 +1100 (Mon, 12 Feb 2007) | 2 lines
This commit is contained in:
committed by
Douglas Clowes
parent
634f2023b1
commit
3168325921
83
protocol.c
83
protocol.c
@@ -16,7 +16,7 @@
|
||||
#include <dynstring.h>
|
||||
#include "commandlog.h"
|
||||
#include "protocol.h"
|
||||
#include "json.h"
|
||||
#include <json/json.h>
|
||||
|
||||
#define MAXMSG 1024
|
||||
#define INIT_STR_SIZE 256
|
||||
@@ -87,8 +87,6 @@ pProtocol CreateProtocol(void);
|
||||
static int ProtocolOptions(SConnection* pCon, pProtocol pPro);
|
||||
static int ProtocolHelp(SConnection* pCon, Protocol* pPro);
|
||||
static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName);
|
||||
static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
||||
int *pIndex);
|
||||
static int ProtocolList(SConnection* pCon, Protocol* pPro);
|
||||
int ProtocolAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]);
|
||||
@@ -294,9 +292,10 @@ static int ProtocolSet(SConnection* pCon, Protocol* pPro, char *pProName)
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
||||
int *pIndex)
|
||||
int ProtocolGet(SConnection* pCon, void* pData, char *pProName, int len)
|
||||
{
|
||||
int Index;
|
||||
Protocol *pPro = (Protocol *)pData;
|
||||
if(!SCVerifyConnection(pCon))
|
||||
{
|
||||
return 0;
|
||||
@@ -309,24 +308,27 @@ static int ProtocolGet(SConnection* pCon, Protocol* pPro, char *pProName,
|
||||
pPro->isDefaultSet = 1;
|
||||
pCon->iProtocolID = 0;
|
||||
}
|
||||
|
||||
*pIndex = (int)malloc(sizeof(int));
|
||||
*pIndex = pCon->iProtocolID;
|
||||
strncpy(pProName, pPro->pProList[pCon->iProtocolID], len);
|
||||
return 1;
|
||||
#if 0
|
||||
Index = pCon->iProtocolID;
|
||||
|
||||
/* check list of protocols for valid name */
|
||||
switch(*pIndex)
|
||||
switch(Index)
|
||||
{
|
||||
case 0: /* default = psi_sics */
|
||||
case 1: /* normal (connection start default) */
|
||||
case 2: /* outcodes */
|
||||
case 3: /* sycamore */
|
||||
pProName = strdup(pPro->pProList[*pIndex]);
|
||||
case 4: /* json */
|
||||
pProName = pPro->pProList[Index];
|
||||
return 1;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
@@ -632,42 +634,43 @@ struct json_object *mkJSON_Object(SConnection *pCon, char *pBuffer, int iOut)
|
||||
json_object_object_add(msg_json, "flag", json_object_new_string(pCode[iOut]));
|
||||
break;
|
||||
}
|
||||
if (iOut == eHdb) {
|
||||
if (iOut == eHdbValue || iOut == eHdbEvent) {
|
||||
tmp_json = json_tokener_parse(pBuffer);
|
||||
if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; }
|
||||
if (is_error(tmp_json)) { linenum = __LINE__; goto reporterr; }
|
||||
} else {
|
||||
/* Strip \r and \n */
|
||||
for (pBufferFrom=pBufferTo=pBuffer; ; pBufferFrom++) {
|
||||
if (*pBufferFrom == '\r' || *pBufferFrom == '\n')
|
||||
continue;
|
||||
*pBufferTo = *pBufferFrom;
|
||||
pBufferTo = pBufferFrom;
|
||||
if (*pBufferTo == '\0')
|
||||
break;
|
||||
pBufferTo++;
|
||||
}
|
||||
tmp_json = json_object_new_string(pBuffer);
|
||||
if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; }
|
||||
if (is_error(tmp_json)) { linenum = __LINE__; goto reporterr; }
|
||||
}
|
||||
json_object_object_add(msg_json, "data", tmp_json);
|
||||
return msg_json;
|
||||
|
||||
reporterr:
|
||||
SCSetWriteFunc(pCon,SCNormalWrite);
|
||||
snprintf(pError, 256,"%s:%d Error making json object", __FILE__, linenum);
|
||||
snprintf(pError, 256,"{\"ERROR\": \"%s:%d Error making json object\"}", __FILE__, linenum);
|
||||
SCWrite(pCon,pError,eError);
|
||||
SCSetWriteFunc(pCon,SCWriteJSON_String);
|
||||
cleanup:
|
||||
if (tmp_json != NULL)
|
||||
if (tmp_json != NULL && !is_error(tmp_json))
|
||||
json_object_put(tmp_json);
|
||||
if (msg_json != NULL)
|
||||
if (msg_json != NULL && !is_error(msg_json))
|
||||
json_object_put(msg_json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int SCWriteJSON_String(SConnection *pCon, char *pBuffer, int iOut)
|
||||
{
|
||||
struct json_object *my_object=NULL;
|
||||
char pBueffel[MAXMSG];
|
||||
int iRet;
|
||||
struct json_object *my_object=NULL, *tmp_json=NULL;
|
||||
char pBueffel[MAXMSG], errBuff[MAXMSG];
|
||||
int iRet, errLen = MAXMSG;
|
||||
|
||||
if (strlen(pBuffer) == 0)
|
||||
return 1;
|
||||
@@ -688,21 +691,48 @@ int SCWriteJSON_String(SConnection *pCon, char *pBuffer, int iOut)
|
||||
/* write to commandlog if user or manager privilege */
|
||||
if(SCGetRights(pCon) <= usUser)
|
||||
{
|
||||
if(pCon->iMacro != 1)
|
||||
{
|
||||
sprintf(pBueffel,"To sock %d :",iRet);
|
||||
WriteToCommandLog(pBueffel,pBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(iOut == eError || iOut == eWarning)
|
||||
{
|
||||
sprintf(pBueffel,"To sock %d :",iRet);
|
||||
WriteToCommandLog(pBueffel,pBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(SCinMacro(pCon))
|
||||
{
|
||||
InterpWrite(pServ->pSics,pBuffer);
|
||||
/* print it to client if error message */
|
||||
if((iOut== eError) || (iOut == eWarning) )
|
||||
{
|
||||
tmp_json = json_object_new_string(pBuffer);
|
||||
iRet = SCDoSockWrite(pCon,json_object_to_json_string(tmp_json));
|
||||
}
|
||||
} else {
|
||||
my_object = mkJSON_Object(pCon, pBuffer, iOut);
|
||||
iRet = SCDoSockWrite(pCon,json_object_to_json_string(my_object));
|
||||
SCWriteToLogFiles(pCon,pBuffer);
|
||||
if ((my_object = mkJSON_Object(pCon, pBuffer, iOut)) == NULL) {
|
||||
snprintf(errBuff, errLen, "failed to make JSON object from, %s", pBuffer);
|
||||
tmp_json = json_object_new_string(errBuff);
|
||||
my_object = json_object_new_object();
|
||||
json_object_object_add(my_object, "ERROR", tmp_json);
|
||||
SCDoSockWrite(pCon,json_object_to_json_string(my_object));
|
||||
iRet = 0;
|
||||
} else {
|
||||
iRet = SCDoSockWrite(pCon,json_object_to_json_string(my_object));
|
||||
SCWriteToLogFiles(pCon,pBuffer);
|
||||
}
|
||||
}
|
||||
if (my_object != NULL)
|
||||
if (tmp_json != NULL && !is_error(tmp_json))
|
||||
json_object_put(tmp_json);
|
||||
if (my_object != NULL && !is_error(my_object))
|
||||
json_object_put(my_object);
|
||||
return 1;
|
||||
return iRet;
|
||||
}
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Protocol API */
|
||||
@@ -731,6 +761,7 @@ char * GetProtocolName(SConnection* pCon)
|
||||
case 1: /* normal (connection start default) */
|
||||
case 2: /* outcodes */
|
||||
case 3: /* sycamore */
|
||||
case 4: /* json */
|
||||
return strdup(pPro->pProList[pCon->iProtocolID]);
|
||||
break;
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user