scheduled tasks minor refactoring
[public/netxms.git] / src / server / core / upload_job.cpp
CommitLineData
74d2f1ea 1/*
4cd1e46b 2** NetXMS - Network Management System
8581943e 3** Copyright (C) 2003-2014 Victor Kirhenshtein
4cd1e46b
AK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: upload_job.cpp
20**
21**/
22
23#include "nxcore.h"
24
74d2f1ea 25/**
26 * Static members
27 */
4cd1e46b
AK
28int FileUploadJob::m_activeJobs = 0;
29int FileUploadJob::m_maxActiveJobs = 10;
30MUTEX FileUploadJob::m_sharedDataMutex = INVALID_MUTEX_HANDLE;
31
74d2f1ea 32/**
33 * Static initializer
34 */
4cd1e46b
AK
35void FileUploadJob::init()
36{
37 m_sharedDataMutex = MutexCreate();
38 m_maxActiveJobs = ConfigReadInt(_T("MaxActiveUploadJobs"), 10);
39}
40
74d2f1ea 41/**
42 * Constructor
43 */
4cd1e46b 44FileUploadJob::FileUploadJob(Node *node, const TCHAR *localFile, const TCHAR *remoteFile, UINT32 userId, bool createOnHold)
c42b4551 45 : ServerJob(_T("UPLOAD_FILE"), _T("Upload file to managed node"), node->getId(), userId, createOnHold)
4cd1e46b
AK
46{
47 m_node = node;
48 node->incRefCount();
49
50 TCHAR buffer[1024];
51 _sntprintf(buffer, 1024, _T("Upload file %s"), GetCleanFileName(localFile));
52 setDescription(buffer);
53
54 m_localFile = _tcsdup(localFile);
55 m_remoteFile = (remoteFile != NULL) ? _tcsdup(remoteFile) : NULL;
56
57 _sntprintf(buffer, 1024, _T("Local file: %s; Remote file: %s"), m_localFile, CHECK_NULL(m_remoteFile));
58 m_info = _tcsdup(buffer);
59}
60
74d2f1ea 61/**
62 * Destructor
63 */
4cd1e46b
AK
64FileUploadJob::~FileUploadJob()
65{
66 m_node->decRefCount();
67 safe_free(m_localFile);
68 safe_free(m_remoteFile);
69 safe_free(m_info);
70}
71
74d2f1ea 72/**
73 * Run job
74 */
4cd1e46b
AK
75bool FileUploadJob::run()
76{
77 bool success = false;
74d2f1ea 78
4cd1e46b
AK
79 while(true)
80 {
81 MutexLock(m_sharedDataMutex);
82 if (m_activeJobs < m_maxActiveJobs)
83 {
84 m_activeJobs++;
85 MutexUnlock(m_sharedDataMutex);
86 break;
87 }
88 MutexUnlock(m_sharedDataMutex);
89 ThreadSleep(5);
90 }
91
92 AgentConnectionEx *conn = m_node->createAgentConnection();
93 if (conn != NULL)
94 {
95 m_fileSize = (INT64)FileSize(m_localFile);
96 UINT32 rcc = conn->uploadFile(m_localFile, m_remoteFile, uploadCallback, this);
97 if (rcc == ERR_SUCCESS)
98 {
99 success = true;
100 }
101 else
102 {
103 setFailureMessage(AgentErrorCodeToText(rcc));
104 }
105 }
106 else
107 {
108 setFailureMessage(_T("Agent connection not available"));
109 }
74d2f1ea 110
4cd1e46b
AK
111 MutexLock(m_sharedDataMutex);
112 m_activeJobs--;
113 MutexUnlock(m_sharedDataMutex);
114
115 return success;
116}
117
74d2f1ea 118/**
119 * Upload progress callback
120 */
4cd1e46b
AK
121void FileUploadJob::uploadCallback(INT64 size, void *arg)
122{
123 if (((FileUploadJob *)arg)->m_fileSize > 0)
124 ((FileUploadJob *)arg)->markProgress((int)(size * _LL(100) / ((FileUploadJob *)arg)->m_fileSize));
125 else
126 ((FileUploadJob *)arg)->markProgress(100);
127}
128
74d2f1ea 129/**
130 * Get additional info for logging
131 */
4cd1e46b
AK
132const TCHAR *FileUploadJob::getAdditionalInfo()
133{
134 return m_info;
135}