Fixed bug with error 913 in windows while follow file, added functionalyti to cancel...
authorzev <zev@radensolutions.com>
Thu, 20 Feb 2014 10:32:53 +0000 (12:32 +0200)
committerzev <zev@radensolutions.com>
Thu, 20 Feb 2014 10:32:53 +0000 (12:32 +0200)
src/agent/core/logmonitoring.cpp
src/agent/core/nxagentd.h
src/agent/core/session.cpp
src/java/netxms-client/src/main/java/org/netxms/client/NXCSession.java
src/java/netxms-client/src/main/java/org/netxms/client/TailFile.java [new file with mode: 0644]
src/java/netxms-eclipse/ObjectTools/src/org/netxms/ui/eclipse/objecttools/ObjectToolsDynamicMenu.java
src/java/netxms-eclipse/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/FileViewer.java
src/server/core/agent.cpp
src/server/core/download_job.cpp
src/server/core/session.cpp

index 67edb10..da416eb 100644 (file)
@@ -163,7 +163,7 @@ THREAD_RESULT THREAD_CALL SendFileUpdatesOverNXCP(void *args)
             pMsg = new CSCPMessage();
             pMsg->SetCode(CMD_FILE_MONITORING);
             pMsg->SetId(0);
-            pMsg->SetVariable(VID_FILE_NAME, flData->pszFile, MAX_PATH);
+            pMsg->SetVariable(VID_FILE_NAME, flData->fileId, MAX_PATH);
 
             lseek(hFile, flData->offset, SEEK_SET);
             readBytes = (BYTE*)malloc(readSize);
@@ -196,13 +196,14 @@ THREAD_RESULT THREAD_CALL SendFileUpdatesOverNXCP(void *args)
       }
 
       ThreadSleep(threadSleepTime);
-      if(!g_monitorFileList.checkFileMonitored(flData->pszFile))
+      if(!g_monitorFileList.checkFileMonitored(flData->fileId))
       {
          follow = false;
       }
    }
-   delete flData->pszFile;
-   delete flData;
+   delete_and_null(flData->pszFile);
+   delete_and_null(flData->fileId);
+   delete_and_null(flData);
    close(hFile);
    return THREAD_OK;
 };
index 42ca3ec..87d5469 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
 ** NetXMS multiplatform core agent
 ** Copyright (C) 2003-2012 Victor Kirhenshtein
 **
 //
 // Request types for H_FileTime
 //
+
 #define FILETIME_ATIME           1
 #define FILETIME_MTIME           2
 #define FILETIME_CTIME           3
@@ -210,7 +210,7 @@ typedef struct
    int iType;
    union
    {
-      TCHAR *pszCmdLine;      // to TCHAR 
+      TCHAR *pszCmdLine;      // to TCHAR
       struct __subagentAction
       {
          LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *);
@@ -410,7 +410,7 @@ BOOL InitSubAgent(HMODULE hModule, const TCHAR *pszModuleName,
                   BOOL (* SubAgentInit)(NETXMS_SUBAGENT_INFO **, Config *),
                   const TCHAR *pszEntryPoint);
 BOOL ProcessCmdBySubAgent(UINT32 dwCommand, CSCPMessage *pRequest, CSCPMessage *pResponse, void *session);
-BOOL AddAction(const TCHAR *pszName, int iType, const TCHAR *pArg, 
+BOOL AddAction(const TCHAR *pszName, int iType, const TCHAR *pArg,
                LONG (*fpHandler)(const TCHAR *, StringList *, const TCHAR *),
                const TCHAR *pszSubAgent, const TCHAR *pszDescription);
 BOOL AddActionFromConfig(TCHAR *pszLine, BOOL bShellExec);
@@ -475,6 +475,7 @@ struct MONITORED_FILE
 struct FollowData
 {
    const TCHAR *pszFile;
+   const TCHAR *fileId;
    long offset;
        void *cbArg;
 };
@@ -522,19 +523,19 @@ TCHAR *GetPdhErrorText(UINT32 dwError, TCHAR *pszBuffer, int iBufSize);
 
 extern UINT32 g_dwFlags;
 extern TCHAR g_szLogFile[];
-extern TCHAR g_szSharedSecret[]; 
-extern TCHAR g_szConfigFile[];  
-extern TCHAR g_szFileStore[];     
-extern TCHAR g_szConfigServer[]; 
-extern TCHAR g_szRegistrar[];  
-extern TCHAR g_szListenAddress[]; 
+extern TCHAR g_szSharedSecret[];
+extern TCHAR g_szConfigFile[];
+extern TCHAR g_szFileStore[];
+extern TCHAR g_szConfigServer[];
+extern TCHAR g_szRegistrar[];
+extern TCHAR g_szListenAddress[];
 extern TCHAR g_szConfigIncludeDir[];
 extern TCHAR g_masterAgent[];
 extern WORD g_wListenPort;
 extern SERVER_INFO g_pServerList[];
 extern UINT32 g_dwServerCount;
 extern time_t g_tmAgentStartTime;
-extern TCHAR g_szPlatformSuffix[]; 
+extern TCHAR g_szPlatformSuffix[];
 extern UINT32 g_dwStartupDelay;
 extern UINT32 g_dwIdleTimeout;
 extern UINT32 g_dwMaxSessions;
index c1cf957..d389bb5 100644 (file)
@@ -719,17 +719,21 @@ void CommSession::getLocalFile(CSCPMessage *pRequest, CSCPMessage *pMsg)
        if (m_bMasterServer)
        {
                TCHAR fileName[MAX_PATH];
+               TCHAR fileNameCode[MAX_PATH];
                pRequest->GetVariableStr(VID_FILE_NAME, fileName, MAX_PATH);
+               pRequest->GetVariableStr(VID_NAME, fileNameCode, MAX_PATH);
                DebugPrintf(m_dwIndex, 5, _T("CommSession::getLocalFile(): request for file \"%s\", follow = %s"),
                   fileName, pRequest->GetVariableShort(VID_FILE_FOLLOW) ? _T("true") : _T("false"));
                bool result = sendFile(pRequest->GetId(), fileName, pRequest->GetVariableLong(VID_FILE_OFFSET), pRequest->GetVariableLong(VID_FILE_SIZE_LIMIT));
                if(pRequest->GetVariableShort(VID_FILE_FOLLOW) && result)
       {
-         TCHAR* fName = _tcsdup(fileName);
-         g_monitorFileList.addMonitoringFile(fName);
+         TCHAR* fileID = _tcsdup(fileNameCode);
+         TCHAR* realName = _tcsdup(fileName);
+         g_monitorFileList.addMonitoringFile(fileID);
          FollowData *flData = new FollowData();
          flData->cbArg = this;
-         flData->pszFile = fName;
+         flData->pszFile = realName;
+         flData->fileId = fileID;
          flData->offset = 0;
          ThreadCreateEx(SendFileUpdatesOverNXCP, 0, (void*)flData);
       }
index 10d339a..58f7b2f 100644 (file)
@@ -6144,7 +6144,8 @@ public class NXCSession implements Session, ScriptLibraryManager, UserManager, S
     * @throws IOException  if socket or file I/O error occurs
     * @throws NXCException if NetXMS server returns an error or operation was timed out
     */
-   public File downloadFileFromAgent(long nodeId, String remoteFileName, long maxFileSize, boolean follow) throws IOException, NXCException
+   public TailFile downloadFileFromAgent(long nodeId, String remoteFileName, long maxFileSize, boolean follow) throws IOException,
+         NXCException
    {
       final NXCPMessage msg = newMessage(NXCPCodes.CMD_GET_AGENT_FILE);
       msg.setVariableInt32(NXCPCodes.VID_OBJECT_ID, (int) nodeId);
@@ -6152,9 +6153,14 @@ public class NXCSession implements Session, ScriptLibraryManager, UserManager, S
       msg.setVariableInt32(NXCPCodes.VID_FILE_SIZE_LIMIT , (int)maxFileSize);
       msg.setVariableInt16(NXCPCodes.VID_FILE_FOLLOW, follow ? 1 : 0);
       sendMessage(msg);
-      waitForRCC(msg.getMessageId()); // first confirmation - server job started
+
+      final NXCPMessage response = waitForRCC(msg.getMessageId()); // first confirmation - server job started
+      TailFile f = new TailFile();
+      f.setId(response.getVariableAsString(NXCPCodes.VID_NAME));
+
       waitForRCC(msg.getMessageId()); // second confirmation - file downloaded to server
-      return waitForFile(msg.getMessageId(), 3600000);
+      f.setFile(waitForFile(msg.getMessageId(), 3600000));
+      return f;
    }
    
    /**
diff --git a/src/java/netxms-client/src/main/java/org/netxms/client/TailFile.java b/src/java/netxms-client/src/main/java/org/netxms/client/TailFile.java
new file mode 100644 (file)
index 0000000..1d7ad77
--- /dev/null
@@ -0,0 +1,49 @@
+/**
+ * 
+ */
+package org.netxms.client;
+
+import java.io.File;
+
+/**
+ * Information about file and it's id
+ * 
+ */
+public class TailFile
+{
+   private String id;
+   private File file;
+
+   /**
+    * @return the id
+    */
+   public String getId()
+   {
+      return id;
+   }
+
+   /**
+    * @param id the id to set
+    */
+   public void setId(String id)
+   {
+      this.id = id;
+   }
+
+   /**
+    * @return the file
+    */
+   public File getFile()
+   {
+      return file;
+   }
+
+   /**
+    * @param file the file to set
+    */
+   public void setFile(File file)
+   {
+      this.file = file;
+   }
+
+}
index 9cc52ae..e8a1e7f 100644 (file)
@@ -18,7 +18,6 @@
  */
 package org.netxms.ui.eclipse.objecttools;
 
-import java.io.File;
 import java.io.IOException;
 import java.net.URLEncoder;
 import java.util.Arrays;
@@ -45,6 +44,7 @@ import org.eclipse.ui.menus.IWorkbenchContribution;
 import org.eclipse.ui.services.IEvaluationService;
 import org.eclipse.ui.services.IServiceLocator;
 import org.netxms.client.NXCSession;
+import org.netxms.client.TailFile;
 import org.netxms.client.events.Alarm;
 import org.netxms.client.objects.AbstractNode;
 import org.netxms.client.objects.AbstractObject;
@@ -463,7 +463,7 @@ public class ObjectToolsDynamicMenu extends ContributionItem implements IWorkben
                        @Override
                        protected void runInternal(IProgressMonitor monitor) throws Exception
                        {
-                               final File file = session.downloadFileFromAgent(node.getObjectId(), fileName, maxFileSize, follow); 
+            final TailFile file = session.downloadFileFromAgent(node.getObjectId(), fileName, maxFileSize, follow);
                                runInUIThread(new Runnable() {
                                        @Override
                                        public void run()
@@ -472,8 +472,9 @@ public class ObjectToolsDynamicMenu extends ContributionItem implements IWorkben
                                                try
                                                {
                                                        String secondaryId = Long.toString(node.getObjectId()) + "&" + URLEncoder.encode(fileName, "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
-                                                       FileViewer view = (FileViewer)window.getActivePage().showView(FileViewer.ID, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
-                                                       view.showFile(file, follow);
+                     FileViewer view = (FileViewer)window.getActivePage().showView(FileViewer.ID, secondaryId,
+                           IWorkbenchPage.VIEW_ACTIVATE);
+                     view.showFile(file.getFile(), follow, file.getId());
                                                }
                                                catch(Exception e)
                                                {
index 8a1798d..f52b4b3 100644 (file)
@@ -62,6 +62,7 @@ public class FileViewer extends ViewPart
        
        private long nodeId;
        private String remoteFileName;
+   private String fileID;
        private File currentFile;
        private StyledText textViewer;
        private final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
@@ -271,9 +272,10 @@ public class FileViewer extends ViewPart
        /**
         * @param file
         */
-       public void showFile(File file, boolean follow)
+   public void showFile(File file, boolean follow, String id)
        {
                currentFile = file;
+      fileID = id;
                textViewer.setText(loadFile(currentFile));
        textViewer.setTopIndex(textViewer.getLineCount());
                this.follow = follow;
@@ -293,7 +295,7 @@ public class FileViewer extends ViewPart
             {
                while(continueWork)
                {
-                  final String s = session.waitForFileTail(remoteFileName, 3000);
+                  final String s = session.waitForFileTail(fileID, 3000);
                   if (s != null)
                   {
                      runInUIThread(new Runnable() {                  
@@ -339,7 +341,7 @@ public class FileViewer extends ViewPart
             @Override
             protected void runInternal(IProgressMonitor monitor) throws Exception
             {
-               session.cancelFileMonitoring(nodeId, remoteFileName);
+               session.cancelFileMonitoring(nodeId, fileID);
             }
             
             @Override
index bac9b14..572035e 100644 (file)
@@ -218,6 +218,23 @@ void AgentConnectionEx::onFileMonitoringData(CSCPMessage *pMsg)
       for(int i = 0; i < result->size(); i++)
       {
          result->get(i)->sendMessage(pMsg);
+      }
+      if(result->size() == 0)
+      {
+         DbgPrintf(6, _T("AgentConnectionEx::onFileMonitoringData: unknown subscription will be canceled."));
+         Node *node = (Node *)object;
+         AgentConnection *conn = node->createAgentConnection();
+         if(conn != NULL)
+         {
+            CSCPMessage request;
+            request.SetId(conn->generateRequestId());
+            request.SetCode(CMD_CANCEL_FILE_MONITORING);
+            request.SetVariable(VID_FILE_NAME, remoteFile);
+            request.SetVariable(VID_OBJECT_ID, node->Id());
+            CSCPMessage* response = conn->customRequest(&request);
+            delete response;
+         }
+         delete conn;
       }
        }
        else
index 6ece636..bbb500f 100644 (file)
@@ -89,7 +89,7 @@ bool FileDownloadJob::run()
        bool success = false;
 
    MONITORED_FILE * newFile = new MONITORED_FILE();
-   _tcscpy(newFile->fileName, m_remoteFile);
+   _tcscpy(newFile->fileName, m_localFile);
    newFile->nodeID = m_node->Id();
    newFile->session = m_session;
 
@@ -148,6 +148,7 @@ bool FileDownloadJob::run()
 
             msg.SetVariable(VID_FILE_SIZE_LIMIT, m_maxFileSize);
             msg.SetVariable(VID_FILE_FOLLOW, (INT16)(m_follow ? 1 : 0));
+            msg.SetVariable(VID_NAME, m_localFile);
 
                                response = conn->customRequest(&msg, m_localFile, appendFile, progressCallback, this);
                                if (response != NULL)
index dc26069..6267c3f 100644 (file)
@@ -10280,6 +10280,7 @@ void ClientSession::getAgentFile(CSCPMessage *request)
                                FileDownloadJob::buildServerFileName(object->Id(), remoteFile, localFile, MAX_PATH);
             bool follow = request->GetVariableShort(VID_FILE_FOLLOW) ? true : false;
                                FileDownloadJob *job = new FileDownloadJob((Node *)object, remoteFile, request->GetVariableLong(VID_FILE_SIZE_LIMIT), follow, this, request->GetId());
+                               msg.SetVariable(VID_NAME, localFile);
                                msg.SetVariable(VID_RCC, AddJob(job) ? RCC_SUCCESS : RCC_INTERNAL_ERROR);
                        }
                        else
@@ -10337,8 +10338,17 @@ void ClientSession::cancelFileMonitoring(CSCPMessage *request)
             if (response != NULL)
             {
                rcc = response->GetVariableLong(VID_RCC);
-               msg.SetVariable(VID_RCC, rcc);
-               debugPrintf(6, _T("File monitoring cancelled sucessfully"));
+               if(rcc == RCC_SUCCESS)
+               {
+                  msg.SetVariable(VID_RCC, rcc);
+                  debugPrintf(6, _T("File monitoring cancelled sucessfully"));
+               }
+               else
+               {
+                  msg.SetVariable(VID_RCC, RCC_INTERNAL_ERROR);
+
+                  debugPrintf(6, _T("Error on agent: %d"), rcc);
+               }
             }
             else
             {