1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include "JobQueue.hpp"
#include "UpdateFactory.hpp"
void Queue::AddJob(const Job& job, const JobPriority& priority)
{
wxMutexLocker lock(m_MutexQueue);
m_Jobs.insert(std::make_pair(priority, job));
m_QueueCount.Post();
}
Job Queue::Pop()
{
Job element;
m_QueueCount.Wait();
m_MutexQueue.Lock();
element = (m_Jobs.begin())->second;
m_Jobs.erase(m_Jobs.begin());
m_MutexQueue.Unlock();
return element;
}
void Queue::Report(const Job::JobEvents& cmd, const wxString& sArg, int iArg)
{
wxCommandEvent evt(wxEVT_THREAD, cmd);
evt.SetString(sArg);
evt.SetInt(iArg);
m_pParent->AddPendingEvent(evt);
}
wxThread::ExitCode WorkerThread::doWork()
{
Sleep(1000);
Job::JobEvents iErr;
m_pQueue->Report(Job::eID_THREAD_STARTED, wxEmptyString, m_ID);
try { while(true) OnJob(); }
catch (Job::JobEvents& i) { m_pQueue->Report(iErr=i, wxEmptyString, m_ID); }
return (wxThread::ExitCode) iErr;
}
void WorkerThread::doJob()
{
int rv;
std::string err;
UpdateFactory uf;
Job job = m_pQueue->Pop();
switch(job.m_cmd)
{
case Job::eID_THREAD_EXIT:
throw Job::eID_THREAD_EXIT;
case Job::eID_THREAD_JOB:
m_pQueue->Report(Job::eID_THREAD_MSG,
wxString::Format(wxT("Job #%d: Connecting to %s:%i"),
job.m_Arg.jobid, job.m_Arg.hostname, job.m_Arg.port), m_ID);
uf.setDest(job.m_Arg.hostname, job.m_Arg.port);
uf.setPass(job.m_Arg.password);
rv = uf.doAuth();
if (uf.getVersion() != EMC_UNKNOWN) {
m_pQueue->Report(Job::eID_THREAD_MSG,
wxString::Format(wxT("Job #%d: Current EnergyManager version: %s"),
job.m_Arg.jobid, mapEmcVersion(uf.getVersion())), m_ID);
}
if (rv != UPDATE_OK) {
mapEmcError(rv, err);
m_pQueue->Report(Job::eID_THREAD_MSGERR,
wxString::Format(wxT("Job #%d: %s."),
job.m_Arg.jobid, err.c_str()), m_ID);
break;
}
m_pQueue->Report(Job::eID_THREAD_MSG,
wxString::Format(wxT("Job #%d: Uploading file \"%s\""),
job.m_Arg.jobid, job.m_Arg.update_file), m_ID);
uf.setUpdateFile(job.m_Arg.update_file.c_str());
rv = uf.loadUpdateFile();
if (rv != UPDATE_OK) {
mapEmcError(rv, err);
m_pQueue->Report(Job::eID_THREAD_MSGERR,
wxString::Format(wxT("Job #%d: %s."),
job.m_Arg.jobid, err.c_str()), m_ID);
break;
}
rv = uf.doUpdate();
mapEmcError(rv, err);
if (rv != UPDATE_OK) {
m_pQueue->Report(Job::eID_THREAD_MSGERR,
wxString::Format(wxT("Job #%d: %s."),
job.m_Arg.jobid, err.c_str()), m_ID);
break;
}
m_pQueue->Report(Job::eID_THREAD_MSGOK,
wxString::Format(wxT("Job #%d: %s."),
job.m_Arg.jobid, err), m_ID);
break;
case Job::eID_THREAD_NULL:
default:
break;
}
}
|