- All component locks moved to memory
authorVictor Kirhenshtein <victor@netxms.org>
Sat, 6 Nov 2004 13:24:00 +0000 (13:24 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Sat, 6 Nov 2004 13:24:00 +0000 (13:24 +0000)
- Table "locks" removed
- Database locking flag moved to "config" table
- Added CSCP commands for SNMP trap management

include/nms_cscp.h
include/nxclapi.h
sql/schema.in
sql/setup.in
src/server/core/config.cpp
src/server/core/locks.cpp
src/server/core/main.cpp
src/server/core/nms_core.h
src/server/core/nms_locks.h
src/server/core/session.cpp

index d2ef0b0..985d717 100644 (file)
@@ -257,6 +257,12 @@ typedef struct
 #define CMD_WAKEUP_NODE             0x005C
 #define CMD_DELETE_EVENT_TEMPLATE   0x005D
 #define CMD_GENERATE_EVENT_ID       0x005E
+#define CMD_LOCK_TRAP_CFG           0x005F
+#define CMD_UNLOCK_TRAP_CFG         0x0060
+#define CMD_CREATE_TRAP             0x0061
+#define CMD_MODIFY_TRAP             0x0062
+#define CMD_DELETE_TRAP             0x0063
+#define CMD_LOAD_TRAP_LIST          0x0064
 
 
 //
index 39a4600..8f2b1e7 100644 (file)
@@ -274,8 +274,9 @@ typedef unsigned long HREQUEST;
 #define SYSTEM_ACCESS_EDIT_EVENT_DB       0x0020
 #define SYSTEM_ACCESS_EPP                 0x0040
 #define SYSTEM_ACCESS_MANAGE_ACTIONS      0x0080
+#define SYSTEM_ACCESS_CONFIGURE_TRAPS     0x0100
 
-#define SYSTEM_ACCESS_FULL                0x00FF
+#define SYSTEM_ACCESS_FULL                0x01FF
 
 
 //
index 37a7935..69fc5e3 100644 (file)
@@ -13,21 +13,6 @@ CREATE TABLE config
 );
 
 
-/*
-** This table contains information about currently locked tables
-** or subsystems
-*/
-
-CREATE TABLE locks
-(
-       component_id integer not null,
-       component_name varchar(63),
-       lock_status integer not null,
-       owner_info varchar(255),  // Information about current lock owner, may be empty
-       PRIMARY KEY(component_id)
-);
-
-
 /*
 ** Users
 */
index cb43ccb..66a2ed5 100644 (file)
@@ -8,6 +8,10 @@ INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
        VALUES ('DBFormatVersion',DB_FORMAT_VERSION,0,1);
 INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
        VALUES ('DBSyntax',DB_SYNTAX,0,1);
+INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
+       VALUES ('DBLockStatus','UNLOCKED',0,1);
+INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
+       VALUES ('DBLockInfo','',0,0);
 INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
        VALUES ('SyncInterval','60',1,1);
 INSERT INTO config (var_name,var_value,is_visible,need_server_restart)
@@ -75,22 +79,6 @@ INSERT INTO user_groups (id,name,access,flags,description)
    VALUES (-2147483648, 'Everyone', 16, 0, 'Built-in everyone group');
 
 
-/*
-** Component lock table
-*/
-
-INSERT INTO locks (component_id,component_name,lock_status,owner_info)
-   VALUES (CID_NETXMS_INSTANCE,'NetXMS Server Instance',-1,'');
-INSERT INTO locks (component_id,component_name,lock_status,owner_info)
-   VALUES (CID_EPP,'Event Processing Policy',-1,'');
-INSERT INTO locks (component_id,component_name,lock_status,owner_info)
-   VALUES (CID_USER_DB,'User Database',-1,'');
-INSERT INTO locks (component_id,component_name,lock_status,owner_info)
-   VALUES (CID_EVENT_DB,'Event Configuration Database',-1,'');
-INSERT INTO locks (component_id,component_name,lock_status,owner_info)
-   VALUES (CID_ACTION_DB,'Action Configuration Database',-1,'');
-
-
 /*
 ** Default event groups
 */
index d171239..b6fe334 100644 (file)
@@ -264,8 +264,7 @@ BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefa
    char szQuery[256];
    BOOL bSuccess = FALSE;
 
-   strncpy(szBuffer, szDefault, iBufSize - 1);
-   szBuffer[iBufSize - 1] = 0;
+   strncpy(szBuffer, szDefault, iBufSize);
    if (strlen(szVar) > 127)
       return FALSE;
 
index 0f70467..ecb62ca 100644 (file)
 #include "nms_core.h"
 
 
+//
+// Constants
+//
+
+#define MAX_OWNER_INFO     256
+#define NUMBER_OF_LOCKS    5
+
+
+//
+// Lock structure
+//
+
+struct LOCK_INFO
+{
+   DWORD dwLockStatus;
+   TCHAR *pszName;
+   TCHAR szOwnerInfo[MAX_OWNER_INFO];
+};
+
+
 //
 // Static data
 //
 
-static MUTEX m_hMutexLockerAccess;
+static MUTEX m_hMutexLockerAccess = NULL;
+static LOCK_INFO m_locks[NUMBER_OF_LOCKS] =
+{
+   { UNLOCKED, _T("Event Processing Policy"), _T("") },
+   { UNLOCKED, _T("User Database"), _T("") },
+   { UNLOCKED, _T("Event Configuration Database"), _T("") },
+   { UNLOCKED, _T("Action Configuration Database"), _T("") },
+   { UNLOCKED, _T("SNMP Trap Configuration"), _T("") }
+};
 
 
 //
@@ -37,51 +65,47 @@ static MUTEX m_hMutexLockerAccess;
 
 BOOL InitLocks(DWORD *pdwIpAddr, char *pszInfo)
 {
-   DB_RESULT hResult;
    BOOL bSuccess = FALSE;
-   DWORD dwStatus;
+   char szBuffer[256];
 
    *pdwIpAddr = UNLOCKED;
    pszInfo[0] = 0;
 
-   hResult = DBSelect(g_hCoreDB, "SELECT lock_status,owner_info FROM locks WHERE component_id=0");
-   if (hResult != NULL)
+   // Check current database lock status
+   ConfigReadStr("DBLockStatus", szBuffer, 256, "ERROR");
+   if (!strcmp(szBuffer, "UNLOCKED"))
    {
-      if (DBGetNumRows(hResult) > 0)
-      {
-         dwStatus = DBGetFieldULong(hResult, 0, 0);
-         if (dwStatus != UNLOCKED)
-         {
-            // Database is locked by someone else, fetch owner info
-            *pdwIpAddr = dwStatus;
-            strcpy(pszInfo, DBGetField(hResult, 0, 1));
-         }
-         else
-         {
-            char szQuery[1024], szSysInfo[512];
-
-            // Lock database
-            GetSysInfoStr(szSysInfo);
-            sprintf(szQuery, "UPDATE locks SET lock_status=%ld,owner_info='%s' WHERE component_id=0",
-                    GetLocalIpAddr(), szSysInfo);
-            DBQuery(g_hCoreDB, szQuery);
-            bSuccess = TRUE;
-         }
-      }
-      DBFreeResult(hResult);
+      IpToStr(GetLocalIpAddr(), szBuffer);
+      ConfigWriteStr("DBLockStatus", szBuffer, FALSE);
+      GetSysInfoStr(szBuffer);
+      ConfigWriteStr("DBLockInfo", szBuffer, TRUE);
+      m_hMutexLockerAccess = MutexCreate();
+      bSuccess = TRUE;
    }
-
-   // Clear all locks and create mutex if we was successfully locked the database
-   if (bSuccess)
+   else
    {
-      DBQuery(g_hCoreDB, "UPDATE locks SET lock_status=-1,owner_info='' WHERE COMPONENT_id<>0");
-      m_hMutexLockerAccess = MutexCreate();
+      if (strcmp(szBuffer, "ERROR"))
+      {
+         *pdwIpAddr = ntohl(inet_addr(szBuffer));
+         ConfigReadStr("DBLockInfo", pszInfo, 256, "<error>");
+      }
    }
 
    return bSuccess;
 }
 
 
+//
+// Unlock database
+//
+
+void UnlockDB(void)
+{
+   ConfigWriteStr("DBLockStatus", "UNLOCKED", FALSE);
+   ConfigWriteStr("DBLockInfo", "", FALSE);
+}
+
+
 //
 // Lock component
 // Function will try to lock specified component. On success, will return TRUE.
@@ -92,8 +116,7 @@ BOOL InitLocks(DWORD *pdwIpAddr, char *pszInfo)
 BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo, 
                    DWORD *pdwCurrentOwner, char *pszCurrentOwnerInfo)
 {
-   char szQuery[256], szBuffer[256];
-   DB_RESULT hResult;
+   char szBuffer[256];
    BOOL bSuccess = FALSE;
    DWORD dwTemp;
 
@@ -102,36 +125,26 @@ BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo,
    if (pszCurrentOwnerInfo == NULL)
       pszCurrentOwnerInfo = szBuffer;
 
-   DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Attempting to lock component %d by %d (%s)",
-             dwId, dwLockBy, pszOwnerInfo != NULL ? pszOwnerInfo : "NULL");
+   if (dwId >= NUMBER_OF_LOCKS)
+   {
+      *pdwCurrentOwner = UNLOCKED;
+      strcpy(pszCurrentOwnerInfo, "Unknown component");
+      return FALSE;
+   }
+
+   DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Attempting to lock component \"%s\" by %d (%s)",
+             m_locks[dwId].pszName, dwLockBy, pszOwnerInfo != NULL ? pszOwnerInfo : "NULL");
    MutexLock(m_hMutexLockerAccess, INFINITE);
-   sprintf(szQuery, "SELECT lock_status,owner_info FROM locks WHERE component_id=%ld", dwId);
-   hResult = DBSelect(g_hCoreDB, szQuery);
-   if (hResult != NULL)
+   if (m_locks[dwId].dwLockStatus == UNLOCKED)
    {
-      if (DBGetNumRows(hResult) > 0)
-      {
-         *pdwCurrentOwner = DBGetFieldULong(hResult, 0, 0);
-         strcpy(pszCurrentOwnerInfo, DBGetField(hResult, 0, 1));
-         DBFreeResult(hResult);
-         if (*pdwCurrentOwner == UNLOCKED)
-         {
-            sprintf(szQuery, "UPDATE locks SET lock_status=%d,owner_info='%s' WHERE component_id=%ld",
-                    dwLockBy, pszOwnerInfo != NULL ? pszOwnerInfo : "", dwId);
-            DBQuery(g_hCoreDB, szQuery);
-            bSuccess = TRUE;
-         }
-      }
-      else
-      {
-         *pdwCurrentOwner = UNLOCKED;
-         strcpy(pszCurrentOwnerInfo, "Unknown component");
-      }
+      m_locks[dwId].dwLockStatus = dwLockBy;
+      strncpy(m_locks[dwId].szOwnerInfo, pszOwnerInfo, MAX_OWNER_INFO);
+      bSuccess = TRUE;
    }
    else
    {
-      *pdwCurrentOwner = UNLOCKED;
-      strcpy(pszCurrentOwnerInfo, "SQL query failed");
+      *pdwCurrentOwner = m_locks[dwId].dwLockStatus;
+      strcpy(pszCurrentOwnerInfo, m_locks[dwId].szOwnerInfo);
    }
    MutexUnlock(m_hMutexLockerAccess);
    return bSuccess;
@@ -144,13 +157,11 @@ BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo,
 
 void UnlockComponent(DWORD dwId)
 {
-   char szQuery[256];
-
    MutexLock(m_hMutexLockerAccess, INFINITE);
-   sprintf(szQuery, "UPDATE locks SET lock_status=-1,owner_info='' WHERE component_id=%ld", dwId);
-   DBQuery(g_hCoreDB, szQuery);
+   m_locks[dwId].dwLockStatus = UNLOCKED;
+   m_locks[dwId].szOwnerInfo[0] = 0;
    MutexUnlock(m_hMutexLockerAccess);
-   DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Component %d unlocked", dwId);
+   DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Component \"%s\" unlocked", m_locks[dwId].pszName);
 }
 
 
@@ -160,12 +171,15 @@ void UnlockComponent(DWORD dwId)
 
 void RemoveAllSessionLocks(DWORD dwSessionId)
 {
-   char szQuery[256];
+   DWORD i;
 
    MutexLock(m_hMutexLockerAccess, INFINITE);
-   sprintf(szQuery, "UPDATE locks SET lock_status=-1,owner_info='' "
-                    "WHERE (component_id <> 0) AND (lock_status = %d)", dwSessionId);
-   DBQuery(g_hCoreDB, szQuery);
+   for(i = 0; i < NUMBER_OF_LOCKS; i++)
+      if (m_locks[i].dwLockStatus == dwSessionId)
+      {
+         m_locks[i].dwLockStatus = UNLOCKED;
+         m_locks[i].szOwnerInfo[0] = 0;
+      }
    MutexUnlock(m_hMutexLockerAccess);
    DbgPrintf(AF_DEBUG_LOCKS, "*Locks* All locks for session %d removed", dwSessionId);
 }
index 55154ab..6ac5e3d 100644 (file)
@@ -348,7 +348,7 @@ void Shutdown(void)
    DbgPrintf(AF_DEBUG_MISC, "Database writer stopped");
 
    // Remove database lock
-   DBQuery(g_hCoreDB, "UPDATE locks SET lock_status=-1,owner_info='' WHERE component_id=0");
+   UnlockDB();
 
    // Disconnect from database and unload driver
    if (g_hCoreDB != 0)
@@ -602,7 +602,7 @@ int main(int argc, char *argv[])
          // Remove database lock
          if (g_dwFlags & AF_DB_LOCKED)
          {
-            DBQuery(g_hCoreDB, "UPDATE locks SET lock_status=-1,owner_info='' WHERE component_id=0");
+            UnlockDB();
             DBDisconnect(g_hCoreDB);
          }
          return 3;
@@ -639,7 +639,7 @@ int main(int argc, char *argv[])
       // Remove database lock
       if (g_dwFlags & AF_DB_LOCKED)
       {
-         DBQuery(g_hCoreDB, "UPDATE locks SET lock_status=-1,owner_info='' WHERE component_id=0");
+         UnlockDB();
          DBDisconnect(g_hCoreDB);
       }
       return 3;
index 8c43305..80ada32 100644 (file)
@@ -96,8 +96,6 @@
 
 #define MAX_LINE_SIZE         4096
 
-#define UNLOCKED           ((DWORD)0xFFFFFFFF)
-
 #define GROUP_FLAG_BIT     ((DWORD)0x80000000)
 
 typedef void * HSNMPSESSION;
@@ -290,6 +288,11 @@ private:
    void ForcedNodePoll(CSCPMessage *pRequest);
    void OnTrap(CSCPMessage *pRequest);
    void OnWakeUpNode(CSCPMessage *pRequest);
+   void CreateTrap(CSCPMessage *pRequest);
+   void UpdateTrap(CSCPMessage *pRequest);
+   void DeleteTrap(CSCPMessage *pRequest);
+   void LockTrapCfg(DWORD dwRqId, BOOL bLock);
+   void SendAllTraps(DWORD dwRqId);
 
 public:
    ClientSession(SOCKET hSocket, DWORD dwHostAddr);
index 9bf5df7..0b893b4 100644 (file)
 #define _nms_locks_h_
 
 
+#define UNLOCKED           ((DWORD)0xFFFFFFFF)
+
+
 //
 // Component identifiers used for locking
 //
 
-#define CID_NETXMS_INSTANCE   0
-#define CID_EPP               1
-#define CID_USER_DB           2
-#define CID_EVENT_DB          3
-#define CID_ACTION_DB         4
+#define CID_EPP               0
+#define CID_USER_DB           1
+#define CID_EVENT_DB          2
+#define CID_ACTION_DB         3
+#define CID_TRAP_CFG          4
 
 
 //
@@ -45,6 +48,7 @@ BOOL InitLocks(DWORD *pdwIpAddr, char *pszInfo);
 BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo, DWORD *pdwCurrentOwner, char *pszCurrentOwnerInfo);
 void UnlockComponent(DWORD dwId);
 void RemoveAllSessionLocks(DWORD dwSessionId);
+void UnlockDB(void);
 
 #endif
 
index 1bcf88d..72354ae 100644 (file)
@@ -577,6 +577,24 @@ void ClientSession::ProcessingThread(void)
          case CMD_WAKEUP_NODE:
             OnWakeUpNode(pMsg);
             break;
+         case CMD_LOCK_TRAP_CFG:
+            LockTrapCfg(pMsg->GetId(), TRUE);
+            break;
+         case CMD_UNLOCK_TRAP_CFG:
+            LockTrapCfg(pMsg->GetId(), FALSE);
+            break;
+         case CMD_CREATE_TRAP:
+            CreateTrap(pMsg);
+            break;
+         case CMD_MODIFY_TRAP:
+            UpdateTrap(pMsg);
+            break;
+         case CMD_DELETE_TRAP:
+            DeleteTrap(pMsg);
+            break;
+         case CMD_LOAD_TRAP_LIST:
+            SendAllTraps(pMsg->GetId());
+            break;
          default:
             {
                CSCPMessage responce;
@@ -3078,3 +3096,48 @@ void ClientSession::OnWakeUpNode(CSCPMessage *pRequest)
    // Send responce
    SendMessage(&msg);
 }
+
+
+//
+// Create new trap configuration record
+//
+
+void ClientSession::CreateTrap(CSCPMessage *pRequest)
+{
+}
+
+
+//
+// Update trap configuration record
+//
+
+void ClientSession::UpdateTrap(CSCPMessage *pRequest)
+{
+}
+
+
+//
+// Delete trap configuration record
+//
+
+void ClientSession::DeleteTrap(CSCPMessage *pRequest)
+{
+}
+
+
+//
+// Lock/unlock trap configuration
+//
+
+void ClientSession::LockTrapCfg(DWORD dwRqId, BOOL bLock)
+{
+}
+
+
+//
+// Send all trap configuration records to client
+//
+
+void ClientSession::SendAllTraps(DWORD dwRqId)
+{
+}