author | Florian Pose <fp@igh-essen.com> |
Fri, 25 Jul 2008 08:52:45 +0000 | |
changeset 1146 | f18d124d7fbc |
parent 1144 | 7dbfdd61812c |
child 1151 | 1fc1535dec29 |
permissions | -rw-r--r-- |
1142 | 1 |
/***************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
****************************************************************************/ |
|
6 |
||
7 |
#include <iostream> |
|
8 |
using namespace std; |
|
9 |
||
10 |
#include "CommandStates.h" |
|
11 |
||
12 |
/*****************************************************************************/ |
|
13 |
||
14 |
CommandStates::CommandStates(): |
|
15 |
Command("states", "Request application-layer states.") |
|
16 |
{ |
|
17 |
} |
|
18 |
||
19 |
/*****************************************************************************/ |
|
20 |
||
21 |
string CommandStates::helpString() const |
|
22 |
{ |
|
23 |
stringstream str; |
|
24 |
||
25 |
str << getName() << " [OPTIONS] <STATE>" << endl |
|
26 |
<< endl |
|
27 |
<< getBriefDescription() << endl |
|
28 |
<< endl |
|
29 |
<< "Arguments:" << endl |
|
1144
7dbfdd61812c
Bugfixes and improvements.
Florian Pose <fp@igh-essen.com>
parents:
1142
diff
changeset
|
30 |
<< " STATE can be 'INIT', 'PREOP', 'SAFEOP', or 'OP'." << endl |
1142 | 31 |
<< endl |
32 |
<< "Command-specific options:" << endl |
|
33 |
<< " --slave -s <index> Positive numerical ring position," << endl |
|
34 |
<< " or 'all' for all slaves (default)." << endl |
|
35 |
<< endl |
|
36 |
<< numericInfo(); |
|
37 |
||
38 |
return str.str(); |
|
39 |
} |
|
40 |
||
41 |
/****************************************************************************/ |
|
42 |
||
43 |
void CommandStates::execute(MasterDevice &m, const StringVector &args) |
|
44 |
{ |
|
45 |
stringstream err; |
|
46 |
string stateStr; |
|
47 |
uint8_t state = 0x00; |
|
48 |
||
49 |
if (args.size() != 1) { |
|
50 |
err << "'" << getName() << "' takes exactly one argument!"; |
|
51 |
throwInvalidUsageException(err); |
|
52 |
} |
|
53 |
||
54 |
stateStr = args[0]; |
|
55 |
transform(stateStr.begin(), stateStr.end(), |
|
56 |
stateStr.begin(), (int (*) (int)) std::toupper); |
|
57 |
||
58 |
if (stateStr == "INIT") { |
|
59 |
state = 0x01; |
|
60 |
} else if (stateStr == "PREOP") { |
|
61 |
state = 0x02; |
|
62 |
} else if (stateStr == "SAFEOP") { |
|
63 |
state = 0x04; |
|
64 |
} else if (stateStr == "OP") { |
|
65 |
state = 0x08; |
|
66 |
} else { |
|
67 |
err << "Invalid state '" << args[0] << "'!"; |
|
68 |
throwInvalidUsageException(err); |
|
69 |
} |
|
70 |
||
71 |
m.open(MasterDevice::ReadWrite); |
|
72 |
||
73 |
if (slavePosition == -1) { |
|
74 |
unsigned int i, numSlaves = m.slaveCount(); |
|
75 |
for (i = 0; i < numSlaves; i++) |
|
76 |
m.requestState(i, state); |
|
77 |
} else { |
|
78 |
m.requestState(slavePosition, state); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
/*****************************************************************************/ |