Cleaned up ANSTO code to merge with sinqdev.sics
This is our new RELEASE-4_0 branch which was taken from ansto/93d9a7c Conflicts: .gitignore SICSmain.c asynnet.c confvirtualmot.c counter.c devexec.c drive.c event.h exebuf.c exeman.c histmem.c interface.h motor.c motorlist.c motorsec.c multicounter.c napi.c napi.h napi4.c network.c nwatch.c nxscript.c nxxml.c nxxml.h ofac.c reflist.c scan.c sicshipadaba.c sicsobj.c site_ansto/docs/Copyright.txt site_ansto/instrument/lyrebird/config/tasmad/sicscommon/nxsupport.tcl site_ansto/instrument/lyrebird/config/tasmad/taspub_sics/tasscript.tcl statusfile.c tasdrive.c tasub.c tasub.h tasublib.c tasublib.h
This commit is contained in:
168
conman.c
168
conman.c
@ -73,7 +73,6 @@
|
||||
#include "statusfile.h"
|
||||
#include "sicshipadaba.h"
|
||||
#include "protocol.h"
|
||||
#include "sicsvar.h"
|
||||
/*
|
||||
#define UUDEB 1
|
||||
define UUDEB , for buffer writing for checking encoding */
|
||||
@ -106,7 +105,8 @@ typedef struct {
|
||||
static int iName = 0;
|
||||
static long lastIdent = 0;
|
||||
/*------------- sending connection (prevent double write when listening) ----*/
|
||||
static int sendingConnection = 0;
|
||||
static SConnection *sendingConnection = NULL;
|
||||
static int sendingSockHandle = 0;
|
||||
/*------------- storing connection and context for later use ----*/
|
||||
struct SCStore {
|
||||
SConnection *pCon;
|
||||
@ -115,24 +115,6 @@ struct SCStore {
|
||||
long macroStack;
|
||||
commandContext cc;
|
||||
};
|
||||
|
||||
/*
|
||||
* This will return the value of a SICS int variable called "sicsdebug"
|
||||
* If sicsdebug is not defined it will return 1
|
||||
* TODO Maybe define debugging levels.
|
||||
*
|
||||
* return 0=debug off, 1=debug on or sicsdebug not defined.
|
||||
*/
|
||||
int sicsdebug() {
|
||||
pSicsVariable debug;
|
||||
|
||||
debug = FindVariable(pServ->pSics, "sicsdebug");
|
||||
if (debug) {
|
||||
return debug->iVal;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/*===========================================================================*/
|
||||
static char *ConName(long ident)
|
||||
{
|
||||
@ -165,6 +147,23 @@ void KillFreeConnections()
|
||||
{
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
SConnection *GetSendingConnection(void)
|
||||
{
|
||||
return sendingConnection;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void SetSendingConnection(SConnection *pCon)
|
||||
{
|
||||
sendingConnection = pCon;
|
||||
if (pCon) {
|
||||
sendingSockHandle = pCon->sockHandle;
|
||||
} else {
|
||||
sendingSockHandle = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static SConnection *SCMakeConnection()
|
||||
{
|
||||
@ -605,7 +604,17 @@ int TelnetWriteANET(int sockHandle, char *pBuffer)
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
static int mustWrite(int iOut)
|
||||
{
|
||||
switch(iOut) {
|
||||
case eLog:
|
||||
case eLogError:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
int SCWrite(SConnection * self, char *pBuffer, int iOut)
|
||||
{
|
||||
@ -618,7 +627,7 @@ int SCWrite(SConnection * self, char *pBuffer, int iOut)
|
||||
if (pBuffer == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if(!SCinMacro(self)){
|
||||
if(!SCinMacro(self) || mustWrite(iOut) ){
|
||||
traceCommand(ConID(self),"out:%s", pBuffer);
|
||||
}
|
||||
return self->write(self, pBuffer, iOut);
|
||||
@ -726,14 +735,14 @@ static void testAndWriteCommandLog(SConnection * pCon, char *buffer,
|
||||
{
|
||||
if (SCGetRights(pCon) <= usUser) {
|
||||
if (SCinMacro(pCon) != 1) {
|
||||
sendingConnection = pCon->sockHandle;
|
||||
SetSendingConnection(pCon);
|
||||
WriteToCommandLogId(NULL, pCon->sockHandle, buffer);
|
||||
sendingConnection = 0;
|
||||
SetSendingConnection(NULL);
|
||||
} else {
|
||||
if (iOut == eLog || iOut == eLogError) {
|
||||
sendingConnection = pCon->sockHandle;
|
||||
SetSendingConnection(pCon);
|
||||
WriteToCommandLogId(NULL, pCon->sockHandle, buffer);
|
||||
sendingConnection = 0;
|
||||
SetSendingConnection(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -850,9 +859,6 @@ int SCNormalWrite(SConnection * self, char *buffer, int iOut)
|
||||
|
||||
/* log it for any case */
|
||||
#if 0
|
||||
if ( (sicsdebug() || !SCinMacro(self)) && !(iOut == eInternal
|
||||
|| (buffer[0] == 'O' && buffer[1] == 'K'
|
||||
&& (buffer[2] == '\0' || buffer[2] == '\r' || buffer[2] == '\n'))))
|
||||
SICSLogWrite(buffer, iOut);
|
||||
#else
|
||||
testAndWriteSICSLog(self, buffer, iOut);
|
||||
@ -1079,7 +1085,6 @@ int SCPureSockWrite(SConnection * self, char *buffer, int iOut)
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
special for ClientLog. Do not use elsewhere without check
|
||||
----------------------------------------------------------------------------*/
|
||||
@ -1091,7 +1096,9 @@ int SCLogWrite(SConnection * self, char *buffer, int iOut)
|
||||
if (!VerifyConnection(self)) {
|
||||
return 0;
|
||||
}
|
||||
SetSendingConnection(self);
|
||||
WriteToCommandLogId(NULL, self->sockHandle, buffer);
|
||||
SetSendingConnection(NULL);
|
||||
|
||||
if(self->iProtocolID == 5) {
|
||||
if (strlen(buffer) + 30 > 1024) {
|
||||
@ -1105,6 +1112,18 @@ int SCLogWrite(SConnection * self, char *buffer, int iOut)
|
||||
if(pPtr != pBueffel){
|
||||
free(pPtr);
|
||||
}
|
||||
} else if(self->iProtocolID == 2) {
|
||||
if (strlen(buffer) + 30 > 1024) {
|
||||
pPtr = (char *) malloc((strlen(buffer) + 30) * sizeof(char));
|
||||
memset(pPtr, 0, strlen(buffer) + 20);
|
||||
} else {
|
||||
pPtr = pBueffel;
|
||||
}
|
||||
sprintf(pPtr,"%s@@%s",buffer,pCode[iOut]);
|
||||
testAndWriteSocket(self, pPtr, iOut);
|
||||
if(pPtr != pBueffel){
|
||||
free(pPtr);
|
||||
}
|
||||
} else {
|
||||
testAndWriteSocket(self, buffer, iOut);
|
||||
}
|
||||
@ -1239,6 +1258,8 @@ int SCWriteZipped(SConnection * self, char *pName, void *pData,
|
||||
SCWrite(self, "ERROR: no data to write in SCWriteZiped", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
pBuf = malloc(iDataLen*sizeof(char));
|
||||
memset(pBuf,0,iDataLen*sizeof(char));
|
||||
|
||||
@ -1258,13 +1279,26 @@ int SCWriteZipped(SConnection * self, char *pName, void *pData,
|
||||
compStream.avail_in = iDataLen;
|
||||
compStream.avail_out = iDataLen;
|
||||
iRet = deflate(&compStream, Z_FINISH);
|
||||
if (iRet != Z_STREAM_END) {
|
||||
if (iRet != Z_STREAM_END && iRet != Z_OK) {
|
||||
sprintf(outBuf, "ERROR: zlib error: %d", iRet);
|
||||
SCWrite(self, outBuf, eError);
|
||||
return 0;
|
||||
}
|
||||
compressedLength = compStream.total_out;
|
||||
|
||||
/*
|
||||
If data is large, test if we can do it
|
||||
*/
|
||||
if(compressedLength > 2*1000*1024) {
|
||||
if(!ANETcanWrite(self->sockHandle,pData,compressedLength)){
|
||||
SCWrite(self,"WARNING: skipping excessive data in SCWriteZipped",eLogError);
|
||||
deflateEnd(&compStream);
|
||||
free(pBuf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* write header line */
|
||||
memset(outBuf, 0, 65536);
|
||||
@ -1331,6 +1365,16 @@ int SCWriteBinary(SConnection * self, char *pName, void *pData,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
If data is large, test if we can do it
|
||||
*/
|
||||
if(iDataLen > 2*1000*1024) {
|
||||
if(!ANETcanWrite(self->sockHandle,pData,iDataLen)){
|
||||
SCWrite(self,"WARNING: skipping excessive data in SCWriteBinary",eLogError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* write header line */
|
||||
memset(outBuf, 0, 65536);
|
||||
|
||||
@ -1582,8 +1626,6 @@ int SCPrompt(SConnection * pCon, char *pPrompt, char *pResult, int iLen)
|
||||
|
||||
SCWrite(pCon, pPrompt, eWarning);
|
||||
master = SCfindMaster(pCon);
|
||||
eOld = GetStatus();
|
||||
SetStatus(eInput);
|
||||
CostaUnlock(master->pStack);
|
||||
while (1) {
|
||||
/*
|
||||
@ -1603,14 +1645,57 @@ int SCPrompt(SConnection * pCon, char *pPrompt, char *pResult, int iLen)
|
||||
/* do we have data ? */
|
||||
iRet = CostaPop(master->pStack, &pPtr);
|
||||
if (iRet == 1) {
|
||||
SetStatus(eOld);
|
||||
CostaLock(master->pStack);
|
||||
strlcpy(pResult, pPtr, iLen);
|
||||
WriteToCommandLogId(" prompted>", pCon->sockHandle, pPtr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
SetStatus(eOld);
|
||||
CostaLock(master->pStack);
|
||||
return 0;
|
||||
}
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int SCPromptTMO(SConnection * pCon, char *pPrompt, char *pResult, int iLen, int timeout)
|
||||
{
|
||||
|
||||
int iRet, i;
|
||||
char *pPtr = NULL;
|
||||
char pFrom[50];
|
||||
Status eOld;
|
||||
int oldMode;
|
||||
SConnection *master = NULL;
|
||||
|
||||
if (!VerifyConnection(pCon)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
SCWrite(pCon, pPrompt, eWarning);
|
||||
master = SCfindMaster(pCon);
|
||||
CostaUnlock(master->pStack);
|
||||
for(i = 0; i < timeout; i++) {
|
||||
/*
|
||||
wait a second. We want to wait even in a simulation, otherwise
|
||||
we go into an endless loop. This is why there is the hack with
|
||||
oldMode and pServ->simMode.
|
||||
*/
|
||||
oldMode = pServ->simMode;
|
||||
pServ->simMode = 0;
|
||||
SicsWait(1);
|
||||
pServ->simMode = oldMode;
|
||||
|
||||
/* is there an interrupt pending ? */
|
||||
if (SCGetInterrupt(pCon) != eContinue) {
|
||||
break;
|
||||
}
|
||||
/* do we have data ? */
|
||||
iRet = CostaPop(master->pStack, &pPtr);
|
||||
if (iRet == 1) {
|
||||
CostaLock(master->pStack);
|
||||
strlcpy(pResult, pPtr, iLen);
|
||||
WriteToCommandLogId(" prompted>", pCon->sockHandle, pPtr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
CostaLock(master->pStack);
|
||||
return 0;
|
||||
}
|
||||
@ -1705,7 +1790,7 @@ int SCInvoke(SConnection * self, SicsInterp * pInter, char *pCommand)
|
||||
* case
|
||||
*/
|
||||
if (SCGetWriteFunc(self) != SCNotWrite) {
|
||||
sendingConnection = self->sockHandle;
|
||||
SetSendingConnection(self);
|
||||
if (self->sockHandle >= 0) {
|
||||
if(strstr(pCommand,"Poch") == NULL){
|
||||
WriteToCommandLogCmd(self->sockHandle, pCommand);
|
||||
@ -1713,7 +1798,7 @@ int SCInvoke(SConnection * self, SicsInterp * pInter, char *pCommand)
|
||||
} else {
|
||||
WriteToCommandLog("CRON>>", pCommand);
|
||||
}
|
||||
sendingConnection = 0;
|
||||
SetSendingConnection(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1736,10 +1821,8 @@ int SCInvoke(SConnection * self, SicsInterp * pInter, char *pCommand)
|
||||
return 0;
|
||||
}
|
||||
strlcpy(pCopy->deviceID, pBueffel, SCDEVIDLEN);
|
||||
/* SCAdvanceContext(self,pBueffel); */
|
||||
traceCommand(ConID(self),"in:%s", pCommand);
|
||||
iRet = InterpExecute(pInter, pCopy, pCommand);
|
||||
/* SCPopContext(self); */
|
||||
SCDeleteConnection(pCopy);
|
||||
StatusFileTask(NULL); /* save changed parameters */
|
||||
|
||||
@ -1858,7 +1941,7 @@ int ConfigCon(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
} else if (strcmp(argv[1], "rights") == 0) {
|
||||
if (argc < 4) {
|
||||
snprintf(pBueffel,511, "Insufficient number of args to %s", argv[0]);
|
||||
SCWrite(pCon, pBueffel, eInError);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
i = IsValidUser(argv[2], argv[3]);
|
||||
@ -2366,6 +2449,7 @@ int SCTaskFunction(void *pData)
|
||||
/* response for monit check */
|
||||
if (strstr(pPtr, "How are you") == pPtr) {
|
||||
SCWrite(self, "I am fine", eError);
|
||||
ANETprocess();
|
||||
ANETclose(self->sockHandle);
|
||||
self->iEnd = 1;
|
||||
free(pPtr);
|
||||
@ -2439,11 +2523,11 @@ void SCSignalFunction(void *pData, int iSignal, void *pSigData)
|
||||
} else if (iSignal == SICSBROADCAST) {
|
||||
pPtr = (char *) pSigData;
|
||||
if (pPtr != NULL) {
|
||||
SCWrite(self, pPtr, eWarning);
|
||||
SCPureSockWrite(self, pPtr, eWarning);
|
||||
}
|
||||
} else if (iSignal == COMLOG && self->listening == 1) {
|
||||
pPtr = (char *) pSigData;
|
||||
if (pPtr != NULL && self->sockHandle != sendingConnection) {
|
||||
if (pPtr != NULL && self->sockHandle != sendingSockHandle) {
|
||||
doSockWrite(self, pPtr);
|
||||
}
|
||||
} else if (iSignal == TOKENRELEASE) {
|
||||
|
Reference in New Issue
Block a user