agent session enumeration API refactored
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 25 May 2015 19:52:28 +0000 (22:52 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 25 May 2015 19:52:28 +0000 (22:52 +0300)
14 files changed:
android/src/agent/res/values/build_number.xml
android/src/console/res/values/build_number.xml
build/build_number
include/build.h
include/nms_agent.h
include/nms_common.h
src/agent/core/comm.cpp
src/agent/core/nxagentd.cpp
src/agent/core/nxagentd.h
src/agent/libnxagent/bridge.cpp
src/agent/subagents/filemgr/logmonitoring.cpp
src/java/build/set_build_number.cmd
src/java/client/netxms-base/src/main/java/org/netxms/base/BuildNumber.java
src/snmp/libnxsnmp/ber.cpp

index e418471..ca7b19b 100644 (file)
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-       <string name="build_number">8019</string>
+       <string name="build_number">8020</string>
 </resources>
index e418471..ca7b19b 100644 (file)
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-       <string name="build_number">8019</string>
+       <string name="build_number">8020</string>
 </resources>
index 0943f90..274eb1a 100644 (file)
@@ -1 +1 @@
-8019
+8020
index e5475ad..664e49e 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef __build_h
 #define __build_h
-#define NETXMS_VERSION_BUILD 8019
-#define NETXMS_VERSION_BUILD_STRING _T("8019")
+#define NETXMS_VERSION_BUILD 8020
+#define NETXMS_VERSION_BUILD_STRING _T("8020")
 #endif
index d2ac093..266b09b 100644 (file)
@@ -571,7 +571,7 @@ void LIBNXAGENT_EXPORTABLE AgentWriteDebugLog2(int level, const TCHAR *format, v
 void LIBNXAGENT_EXPORTABLE AgentSendTrap(UINT32 dwEvent, const TCHAR *eventName, const char *pszFormat, ...);
 void LIBNXAGENT_EXPORTABLE AgentSendTrap2(UINT32 dwEvent, const TCHAR *eventName, int nCount, TCHAR **ppszArgList);
 
-bool LIBNXAGENT_EXPORTABLE EnumerateSessions(bool (* callback)(AbstractCommSession *, void *), void *data);
+bool LIBNXAGENT_EXPORTABLE AgentEnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data);
 
 bool LIBNXAGENT_EXPORTABLE AgentSendFileToServer(void *session, UINT32 requestId, const TCHAR *file, long offset);
 
index de27b69..df9d10c 100644 (file)
@@ -1222,6 +1222,19 @@ typedef struct tagICMPHDR
 #define INVALID_PIPE_HANDLE (-1)
 #endif
 
+#ifdef __cplusplus
+
+/**
+ * Session enumeration callback return codes
+ */
+enum EnumerationCallbackResult
+{
+   _STOP = 0,
+   _CONTINUE = 1
+};
+
+#endif
+
 /**
  * Memory debug
  */
index a5abbb2..d91859d 100644 (file)
@@ -109,6 +109,31 @@ void UnregisterSession(UINT32 dwIndex)
    MutexUnlock(g_hSessionListAccess);
 }
 
+/**
+ * Enumerates active agent sessions. Callback will be called for each valid session.
+ * Callback must return _STOP to stop enumeration or _CONTINUE to continue.
+ *
+ * @return true if enumeration was stopped by callback
+ */
+bool EnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data)
+{
+   bool result = false;
+   MutexLock(g_hSessionListAccess);
+   for(UINT32 i = 0; i < g_dwMaxSessions; i++)
+   {
+      if (g_pSessionList[i] == NULL)
+         continue;
+
+      if (callback(g_pSessionList[i], data) == _STOP)
+      {
+         result = true;
+         break;
+      }
+   }
+   MutexUnlock(g_hSessionListAccess);
+   return result;
+}
+
 /**
  * TCP/IP Listener
  */ 
index 21407a8..c0cb35b 100644 (file)
@@ -87,7 +87,7 @@ extern TCHAR g_windowsServiceDisplayName[];
 void LIBNXAGENT_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
                                                                                                                 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
                                                                                                                 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
-                                                                                                                bool (* enumerateSessions)(bool (*)(AbstractCommSession *, void *), void*),
+                                                                                                                bool (* enumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void*),
                                                                                                                 bool (* sendFile)(void *, UINT32, const TCHAR *, long),
                                                                                                                 bool (* pushData)(const TCHAR *, const TCHAR *, UINT32, time_t),
                                            CONDITION shutdownCondition, Config *registry,
@@ -654,25 +654,6 @@ static bool SendFileToServer(void *session, UINT32 requestId, const TCHAR *file,
        return ((CommSession *)session)->sendFile(requestId, file, offset);
 }
 
-/**
- * Goes throught sesion list and executs on each object handler while it returns true
- */
-static bool EnumerateSessionsBySubagent(bool (* pHandler)(AbstractCommSession *, void* ), void *data)
-{
-   bool ret = false;
-   MutexLock(g_hSessionListAccess);
-   for(UINT32 i = 0; i < g_dwMaxSessions; i++)
-   {
-      if (!pHandler(g_pSessionList[i], data))
-      {
-         ret = true;
-         break;
-      }
-   }
-   MutexUnlock(g_hSessionListAccess);
-   return ret;
-}
-
 /**
  * Parser server list
  */
@@ -776,7 +757,7 @@ BOOL Initialize()
 
    // Initialize API for subagents
    s_subAgentsStopCondition = ConditionCreate(TRUE);
-   InitSubAgentAPI(WriteSubAgentMsg, SendTrap, SendTrap, EnumerateSessionsBySubagent
+   InitSubAgentAPI(WriteSubAgentMsg, SendTrap, SendTrap, EnumerateSessions, 
       SendFileToServer, PushData, s_subAgentsStopCondition, s_registry, SaveRegistry,
       g_szDataDirectory);
    DebugPrintf(INVALID_INDEX, 1, _T("Subagent API initialized"));
index 31a4ef1..88e3c47 100644 (file)
@@ -518,6 +518,8 @@ bool OpenLocalDatabase();
 void CloseLocalDatabase();
 DB_HANDLE GetLocalDatabaseHandle();
 
+bool EnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void* ), void *data);
+
 #ifdef _WIN32
 
 void InitService();
index 9add2dd..ff6da1c 100644 (file)
@@ -28,7 +28,7 @@
 static void (* s_fpWriteLog)(int, int, const TCHAR *) = NULL;
 static void (* s_fpSendTrap1)(UINT32, const TCHAR *, const char *, va_list) = NULL;
 static void (* s_fpSendTrap2)(UINT32, const TCHAR *, int, TCHAR **) = NULL;
-static bool (* s_fpEnumerateSessions)(bool (*)(AbstractCommSession *, void *), void *) = NULL;
+static bool (* s_fpEnumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void *) = NULL;
 static bool (* s_fpSendFile)(void *, UINT32, const TCHAR *, long) = NULL;
 static bool (* s_fpPushData)(const TCHAR *, const TCHAR *, UINT32, time_t) = NULL;
 static CONDITION s_agentShutdownCondition = INVALID_CONDITION_HANDLE;
@@ -42,7 +42,7 @@ static const TCHAR *s_dataDirectory = NULL;
 void LIBNXAGENT_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
                                                                                                                void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
                                                                                                                void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
-                                                                                                               bool (* enumerateSessions)(bool (*)(AbstractCommSession *, void *), void*),
+                                                                                                               bool (* enumerateSessions)(EnumerationCallbackResult (*)(AbstractCommSession *, void *), void*),
                                                                                                                bool (* sendFile)(void *, UINT32, const TCHAR *, long),
                                                                                                                bool (* pushData)(const TCHAR *, const TCHAR *, UINT32, time_t),
                                           CONDITION shutdownCondition, Config *registry,
@@ -147,16 +147,14 @@ void LIBNXAGENT_EXPORTABLE AgentSendTrap2(UINT32 dwEvent, const TCHAR *eventName
 }
 
 /**
- * Goes throught all sessions and executes
+ * Enumerates active agent sessions. Callback will be called for each valid session.
+ * Callback must return _STOP to stop enumeration or _CONTINUE to continue.
+ *
+ * @return true if enumeration was stopped by callback
  */
-bool LIBNXAGENT_EXPORTABLE EnumerateSessions(bool (* callback)(AbstractCommSession *, void *), void *data)
+bool LIBNXAGENT_EXPORTABLE AgentEnumerateSessions(EnumerationCallbackResult (* callback)(AbstractCommSession *, void *), void *data)
 {
-   if (s_fpEnumerateSessions != NULL)
-   {
-      return s_fpEnumerateSessions(callback, data);
-   }
-   else
-      return false;
+   return (s_fpEnumerateSessions != NULL) ? s_fpEnumerateSessions(callback, data) : false;
 }
 
 /**
index 44c0a1b..ea08323 100644 (file)
@@ -123,7 +123,7 @@ void MonitoredFileList::Unlock()
 /**
  * Data for message sending callback
  */
-struct SendMessageData
+struct SendFileUpdateCallbackData
 {
    InetAddress ip;
    NXCPMessage *pMsg;
@@ -132,14 +132,14 @@ struct SendMessageData
 /**
  * Callback to send message to agent communication session's peer
  */
-static bool SendMessage(AbstractCommSession *session, void *data)
+static EnumerationCallbackResult SendFileUpdateCallback(AbstractCommSession *session, void *data)
 {
-   if (session != NULL && ((SendMessageData *)data)->ip.equals(session->getServerAddress()))
+   if (((SendFileUpdateCallbackData *)data)->ip.equals(session->getServerAddress()))
    {
-      session->sendMessage(((SendMessageData *)data)->pMsg);
-      return false;
+      session->sendMessage(((SendFileUpdateCallbackData *)data)->pMsg);
+      return _STOP;
    }
-   return true;
+   return _CONTINUE;
 };
 
 /**
@@ -200,11 +200,11 @@ THREAD_RESULT THREAD_CALL SendFileUpdatesOverNXCP(void *args)
 #endif
             flData->setOffset(newOffset);
 
-            SendMessageData data;
+            SendFileUpdateCallbackData data;
             data.ip = flData->getServerAddress();
             data.pMsg = pMsg;
 
-            bool sent = EnumerateSessions(SendMessage, &data);
+            bool sent = AgentEnumerateSessions(SendFileUpdateCallback, &data);
 
             if (!sent)
             {
index 1b8ff01..92b2ec5 100644 (file)
@@ -1,5 +1,5 @@
 package org.netxms.base;
 public final class BuildNumber {
-   public static final String TEXT = "8019";
-   public static final int NUMBER = 8019;
+   public static final String TEXT = "8020";
+   public static final int NUMBER = 8020;
 }
index 86274eb..d501038 100644 (file)
@@ -203,7 +203,6 @@ static size_t EncodeContent(UINT32 type, const BYTE *data, size_t dataLength, BY
    UINT32 dwTemp;
    QWORD qwTemp;
    BYTE *pTemp, sign;
-   int i;
    size_t oidLength;
 
    switch(type)
@@ -274,7 +273,7 @@ static size_t EncodeContent(UINT32 type, const BYTE *data, size_t dataLength, BY
             nBytes++;
 
             // Encode other ids
-            for(i = 2; i < oidLength; i++, pdwCurrId++)
+            for(size_t i = 2; i < oidLength; i++, pdwCurrId++)
             {
                dwValue = *pdwCurrId;