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