- Installed json protocol
SKIPPED: psi/libpsi.a psi/tecs/libtecsl.a
This commit is contained in:
145
sicshipadaba.c
145
sicshipadaba.c
@@ -23,6 +23,7 @@
|
||||
#include "sicspoll.h"
|
||||
#include <lld.h>
|
||||
#include <stptok.h>
|
||||
#include "protocol.h"
|
||||
|
||||
/*== there can be only hipadaba in SICS, some globals to care for that == */
|
||||
static pHdb root = NULL;
|
||||
@@ -147,6 +148,53 @@ typedef struct {
|
||||
SConnection *pCon;
|
||||
commandContext context;
|
||||
}HdbCBInfo;
|
||||
|
||||
static int isJSON(SConnection *pCon) {
|
||||
char proName[128];
|
||||
void *pData;
|
||||
|
||||
if(SCinMacro(pCon)){
|
||||
return 0;
|
||||
}
|
||||
pData = FindCommandData(pServ->pSics, "protocol","Protocol");
|
||||
ProtocolGet(pCon, pData, proName, 128);
|
||||
if (strcmp(proName, "json") == 0)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int formatNameValue(int jsonSet, char *name, char *value, pDynString result) {
|
||||
if (name == NULL) {
|
||||
if (jsonSet) {
|
||||
} else {
|
||||
}
|
||||
} else if (value == NULL) {
|
||||
if (jsonSet) {
|
||||
DynStringInsert(result,"\": ", 0);
|
||||
DynStringInsert(result,name,0);
|
||||
DynStringInsert(result,"{\"", 0);
|
||||
DynStringConcat(result,"}");
|
||||
} else {
|
||||
DynStringInsert(result," =",0);
|
||||
DynStringInsert(result,name,0);
|
||||
}
|
||||
} else {
|
||||
if (jsonSet) {
|
||||
DynStringCopy(result,"{\"");
|
||||
DynStringConcat(result,name);
|
||||
DynStringConcat(result,"\": ");
|
||||
DynStringConcat(result,value);
|
||||
DynStringConcat(result,"}");
|
||||
} else {
|
||||
DynStringCopy(result,name);
|
||||
DynStringConcat(result," = ");
|
||||
DynStringConcat(result,value);
|
||||
}
|
||||
}
|
||||
return jsonSet;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------------------*/
|
||||
static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||
hdbValue v){
|
||||
@@ -154,10 +202,16 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||
pDynString printedData = NULL;
|
||||
pDynString result = NULL;
|
||||
char *pPath = NULL;
|
||||
int protocol = 0, outCode;
|
||||
|
||||
cbInfo = (HdbCBInfo *)userData;
|
||||
pPath = GetHipadabaPath(node);
|
||||
result = CreateDynString(128,128);
|
||||
if ((protocol = isJSON(cbInfo->pCon)) == 1)
|
||||
outCode = eHdbEvent;
|
||||
else
|
||||
outCode = eEvent;
|
||||
|
||||
if(v.arrayLength < 100){
|
||||
printedData = formatValue(v);
|
||||
if(pPath == NULL || printedData == NULL || result == NULL){
|
||||
@@ -169,17 +223,14 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
DynStringCopy(result,pPath);
|
||||
DynStringConcat(result," = ");
|
||||
DynStringConcat(result,GetCharArray(printedData));
|
||||
formatNameValue(protocol, pPath, GetCharArray(printedData), result);
|
||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
||||
eEvent,cbInfo->context);
|
||||
outCode,cbInfo->context);
|
||||
DeleteDynString(printedData);
|
||||
} else {
|
||||
DynStringCopy(result,"!!datachange!! = ");
|
||||
DynStringConcat(result,pPath);
|
||||
formatNameValue(protocol,"!!datachange!!", pPath, result);
|
||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
||||
eEvent,cbInfo->context);
|
||||
outCode,cbInfo->context);
|
||||
}
|
||||
free(pPath);
|
||||
DeleteDynString(result);
|
||||
@@ -203,12 +254,21 @@ static int TreeChangeCallback(void *userData, void *callData, pHdb node,
|
||||
hdbValue v){
|
||||
char *path = NULL;
|
||||
char buffer[1024];
|
||||
pDynString result = NULL;
|
||||
int protocol = 0, outCode;
|
||||
|
||||
result = CreateDynString(128,128);
|
||||
HdbCBInfo *cbInfo = (HdbCBInfo *)userData;
|
||||
|
||||
if(cbInfo != NULL && cbInfo->pCon != NULL){
|
||||
path = GetHipadabaPath(node);
|
||||
snprintf(buffer,1023,"treechange = %s", path);
|
||||
SCWriteInContext(cbInfo->pCon,buffer,eEvent,cbInfo->context);
|
||||
if ((protocol = isJSON(cbInfo->pCon)) == 1)
|
||||
outCode = eHdbEvent;
|
||||
else
|
||||
outCode = eEvent;
|
||||
formatNameValue(protocol, "treechange", path, result);
|
||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),outCode,cbInfo->context);
|
||||
DeleteDynString(result);
|
||||
free(path);
|
||||
}
|
||||
return 1;
|
||||
@@ -1668,6 +1728,7 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
pDynString parData = NULL;
|
||||
char error[512], oriPath[512];;
|
||||
int i, status;
|
||||
int protocol = 0, outCode;
|
||||
|
||||
if(argc < 2) {
|
||||
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
||||
@@ -1686,9 +1747,13 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
||||
return 0;
|
||||
}
|
||||
DynStringInsert(parData," =",0);
|
||||
DynStringInsert(parData,oriPath,0);
|
||||
SCWrite(pCon,GetCharArray(parData),eValue);
|
||||
if ((protocol = isJSON(pCon)) == 1)
|
||||
outCode = eHdbEvent;
|
||||
else
|
||||
outCode = eEvent;
|
||||
|
||||
formatNameValue(protocol, oriPath, NULL, parData);
|
||||
SCWrite(pCon,GetCharArray(parData),outCode);
|
||||
DeleteDynString(parData);
|
||||
ReleaseHdbValue(&newValue);
|
||||
|
||||
@@ -1811,7 +1876,7 @@ static int HdbNodeVal(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
||||
return 0;
|
||||
}
|
||||
SCWrite(pCon,GetCharArray(parData),eValue);
|
||||
SCWrite(pCon,GetCharArray(parData),eHdbValue);
|
||||
DeleteDynString(parData);
|
||||
ReleaseHdbValue(&newValue);
|
||||
|
||||
@@ -1850,6 +1915,47 @@ static pDynString formatPlainList(pHdb node){
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static pDynString formatJSONList(pHdb node){
|
||||
pHdb current;
|
||||
pDynString result = NULL;
|
||||
pDynString data = NULL;
|
||||
|
||||
if (node->child == NULL) return NULL;
|
||||
result = CreateDynString(128,128);
|
||||
if(result == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(node->child->value.dataType == HIPNONE)
|
||||
DynStringCopy(result,"[");
|
||||
else
|
||||
DynStringCopy(result,"{");
|
||||
|
||||
current = node->child;
|
||||
while(current != NULL){
|
||||
DynStringConcat(result,"\"");
|
||||
DynStringConcat(result,current->name);
|
||||
DynStringConcat(result,"\"");
|
||||
if(current->value.dataType != HIPNONE){
|
||||
data = formatValue(current->value);
|
||||
if(data != NULL){
|
||||
DynStringConcat(result,": ");
|
||||
DynStringConcat(result,GetCharArray(data));
|
||||
DeleteDynString(data);
|
||||
}
|
||||
}
|
||||
if (current->next != NULL) DynStringConcat(result,", ");
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if(node->child->value.dataType == HIPNONE)
|
||||
DynStringConcat(result,"]");
|
||||
else
|
||||
DynStringConcat(result,"}");
|
||||
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static pDynString formatListWithVal(pHdb node){
|
||||
pHdb current;
|
||||
pDynString result = NULL;
|
||||
@@ -1952,6 +2058,7 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
pHdb node = NULL;
|
||||
int pathArg = 1;
|
||||
pDynString listData = NULL;
|
||||
int protocol = 0, outCode;
|
||||
|
||||
if(argc < 2) {
|
||||
SCWrite(pCon,"ERROR: need path to node to print",eError);
|
||||
@@ -1979,14 +2086,24 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
} else if(strcmp(argv[1],"-cli") == 0){
|
||||
listData = formatClientList(node);
|
||||
} else {
|
||||
if ((protocol = isJSON(pCon)) == 1) {
|
||||
listData = formatJSONList(node);
|
||||
outCode = eHdbEvent;
|
||||
} else {
|
||||
listData = formatPlainList(node);
|
||||
outCode = eEvent;
|
||||
}
|
||||
}
|
||||
if(listData == NULL){
|
||||
SCWrite(pCon,"ERROR: failed to format list",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
SCWrite(pCon,GetCharArray(listData),eValue);
|
||||
if( (strcmp(argv[1],"-val") == 0) || (strcmp(argv[1],"-cli") == 0) ){
|
||||
SCWrite(pCon,GetCharArray(listData),eValue);
|
||||
} else {
|
||||
SCWrite(pCon,GetCharArray(listData),outCode);
|
||||
}
|
||||
DeleteDynString(listData);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user