Newer
Older
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
105
106
107
108
109
110
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <productiondatabaseclient.h>
#include <boost/program_options.hpp>
using namespace std;
using namespace ProductionDatabase;
namespace po = boost::program_options;
static string fileName = "serials.dat";
static bool unavailable = true;
static bool debug = false;
static string username = "";
static string password = "";
static vector<string> apdSerials;
void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]);
void loadSerialsFromFileName(string m_fileName, bool m_debug = false);
int main(int argc, char* argv[]) {
ProductionDatabaseClient *proddb = new ProductionDatabaseClient();
processArgumentsAndQueryMissing(argc, argv);
if (debug) cout << "Intitialized apdUnavailableSetter with version " << proddb->getVersion() << " of the database access libraries." << endl << endl
<< "Now trying to set tempUnavailable to " << unavailable << " for ADPs from serial file " << fileName << endl;
loadSerialsFromFileName(fileName);
if (username == "" || password == "") proddb->queryCredentials();
else proddb->setCredentials(username, password);
DatabaseClientResponse response = proddb->checkConnectivityAndCredentials();
if (response != Successful ) {
cerr << "Connection to database failed because of error: " << response << endl;
return response;
}
if (debug) cout << "Connection successful!" << endl;
proddb->setApdTempUnavailable(apdSerials,unavailable);
cout << "\nTemporary unavailability" << unavailable << " was successfully assigned to APDs from serial file " << fileName << ". At least I hope so. In any case, something happened for " << apdSerials.size() << " APDs."
<< "\nThank you for using apdUnavailableSetter! :)" << endl << endl;
return (-apdSerials.empty());
}
void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]) {
po::options_description desc("Available options");
desc.add_options()
("help", "produce help message")
("available", "Set temporary unavailability to false instead of true")
("fileName", po::value<string>(), "file name or serial numbers [serials.dat]")
("debug", "emit additional messages for debugging purposes")
("user", po::value<string>(), "User name to connect to DB. Only used when combined with pass!")
("pass", po::value<string>(), "Password to connect to DB. Only used when combined with user!")
;
po::variables_map vm;
po::store(po::parse_command_line(m_argc, m_argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
if (vm.count("available"))
unavailable = false;
if (vm.count("fileName")) {
fileName = vm["fileName"].as<string>();
cout << "Reading serials from " << fileName << endl;
}
if (vm.count("user")) {
username = vm["user"].as<string>();
}
if (vm.count("pass")) {
password = vm["pass"].as<string>();
}
if (vm.count("debug")) {
debug = true;
}
}
void loadSerialsFromFileName(string m_fileName, bool m_debug) {
ifstream in(m_fileName.c_str());
if (!in.good()) {
cerr << "Could not load serials. Does the input file exist?" << endl;
exit(0);
}
apdSerials.clear();
string line = "";
while (in.good()) {
getline(in,line);
if (line.length() == 0 || !isdigit(line[0]))
continue;
while (line.length() != 10)
line = std::string("0")+ line;
apdSerials.push_back(line);
if (m_debug) cout << "Found serial: " << line << endl;
}
cerr << "Found " << apdSerials.size() << " APDs" << endl;
return;
}