libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
ScheduledTask.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #include "ScheduledTask.hpp"
23 
24 #include "../../../core/trace.hpp"
25 #include "../../../core/unicode_conversions.hpp"
26 
27 /////////////////////////////////////////////////////////////////////////////
28 
29 const TCHAR NetScheduleJobAdd_name[]=_T("NetScheduleJobAdd");
30 const TCHAR NetScheduleJobDel_name[]=_T("NetScheduleJobDel");
31 
32 using namespace libjmmcg;
33 using namespace NTUtils;
34 
35 /////////////////////////////////////////////////////////////////////////////
36 
37 ScheduledTask::ScheduledTask(void) :
38  LoadLibraryWrapper(netapi32_nt_lib_name),
39  pNetScheduleJobAdd(reinterpret_cast<NetScheduleJobAddType>(::GetProcAddress(Handle(),NetScheduleJobAdd_name))),
40  pNetScheduleJobDel(reinterpret_cast<NetScheduleJobDelType>(::GetProcAddress(Handle(),NetScheduleJobDel_name))) {
41  assert(pNetScheduleJobAdd);
42  assert(pNetScheduleJobDel);
43 }
44 
45 ScheduledTask::~ScheduledTask(void) {
46  std::vector<std::pair<std::wstring,unsigned long> >::iterator iter=jobs.begin();
47  while (iter!=jobs.end()) {
48  (*pNetScheduleJobDel)(iter->first.c_str(),iter->second,iter->second);
49  JMMCG_TRACE(_T("ScheduledTask::~ScheduledTask(): Deleted job: ID:")<<iter->second<<_T(" on machine: ")<<WStringToTString(iter->first));
50  ++iter;
51  }
52 }
53 
54 bool
55 ScheduledTask::AddJob(const std::wstring &machine,const AT_INFO &at,unsigned long &jobid) {
56  if ((*pNetScheduleJobAdd)(machine.c_str(),const_cast<BYTE *>(reinterpret_cast<const BYTE *>(&at)),&jobid)==NERR_Success) {
57  jobs.push_back(jobs_list_type::value_type(machine,jobid));
58 #ifdef _DEBUG
59  JMMCG_TRACE(_T("ScheduledTask::AddJob(...): Added job: ID:")<<jobid<<_T(" on machine: ")<<WStringToTString(machine));
60  JMMCG_TRACE(_T("\tJob time: ")<<Time(at.JobTime));
61  TCHAR buff[33];
62  _ultot_s(at.DaysOfMonth,buff,2);
63  TRACE1(_T("\tDays it runs on in a month '%032.32s',\n"),buff);
64  _itot_s((int)at.DaysOfWeek,buff,2);
65  TRACE1(_T("\tDays it runs on in a week '%08.8s',\n"),buff);
66  TRACE1(_T("\tJob flags: %u,\n"),(short unsigned int)at.Flags);
67  TRACE1(_T("\tCommand line: '%ls'.\n"),at.Command);
68 #endif
69  return false;
70  }
71  return true;
72 }
73 
74 bool
75 ScheduledTask::DeleteJob(const std::wstring &machine,const unsigned long job_id) {
76  if ((*pNetScheduleJobDel)(machine.c_str(),job_id,job_id)==NERR_Success) {
77  const jobs_list_type::iterator iter(std::find_if(jobs.begin(),jobs.end(),std::bind2nd(SameObj<std::pair<std::wstring,unsigned long>,jobs_list_type::value_type > (),jobs_list_type::value_type(machine,job_id))));
78  if (iter!=jobs.end()) {
79  jobs.erase(iter);
80  JMMCG_TRACE(_T("ScheduledTask::DeleteJob(...): Deleted job: ID:")<<job_id<<_T(" on machine: ")<<WStringToTString(machine));
81  }
82  return false;
83  }
84  JMMCG_TRACE(_T("ScheduledTask::DeleteJob(...): Failed to delete job: ID:")<<job_id<<_T(" on machine: ")<<WStringToTString(machine));
85  return true;
86 }
87 
88 tstring
89 ScheduledTask::Time(const unsigned long msec) {
90  tostringstream ret;
91  ret
92  <<std::setw(2)<<std::setfill(_T('0'))<<msec/3600000 // hrs
93  <<_T(":")<<std::setw(2)<<std::setfill(_T('0'))<<(msec%3600000)/60000 // mins
94  <<_T(":")<<std::setw(2)<<std::setfill(_T('0'))<<(msec%60000)/1000 // sec
95  <<_T(".")<<std::setw(2)<<std::setfill(_T('0'))<<msec%1000; // msec
96  return ret.str();
97 }