-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathloader.cpp
185 lines (157 loc) · 4.93 KB
/
loader.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Microsoft Corporation. All rights reserved.
#include <iostream>
#include <filesystem>
#include "yaml-cpp/yaml.h"
#include "parser.hpp"
#include "loader.hpp"
#include "getopt.h"
#include "jbpf_common.h"
#include "jbpf_lcm_api.h"
#include "jbpf_lcm_ipc.h"
namespace jbpf_lcm_cli {
namespace loader {
using namespace std;
enum lcm_cli_type
{
load,
unload
};
union request
{
jbpf_codeletset_load_req load;
jbpf_codeletset_unload_req unload;
};
struct lcm_cli_config
{
request req;
lcm_cli_type typ;
jbpf_lcm_ipc_address_t addr;
};
inline bool
exists(const string& name)
{
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
load_req_outcome
parseArgs(int ac, char** av, lcm_cli_config* opts)
{
stringstream default_address;
default_address << JBPF_DEFAULT_RUN_PATH << "/" << JBPF_DEFAULT_NAMESPACE << "/" << JBPF_DEFAULT_LCM_SOCKET;
auto address = default_address.str();
string filename;
bool set_direction = false;
bool set_config = false;
for (;;) {
switch (getopt(ac, av, "a:c:lu")) {
case 'a': {
address = string(optarg);
if (address.length() > JBPF_LCM_IPC_ADDRESS_LEN - 1) {
cout << "-a length must be at most " << JBPF_LCM_IPC_ADDRESS_LEN - 1 << endl;
return JBPF_LCM_CLI_INVALID_ARGS;
}
continue;
}
case 'c': {
filename = string(optarg);
continue;
}
case 'l': {
if (set_direction) {
cout << "Invalid arguments: cannot specify both load and unload" << endl;
return JBPF_LCM_CLI_INVALID_ARGS;
}
(*opts).typ = load;
set_direction = true;
continue;
}
case 'u': {
if (set_direction) {
cout << "Invalid arguments: cannot specify both load and unload" << endl;
return JBPF_LCM_CLI_INVALID_ARGS;
}
(*opts).typ = unload;
set_direction = true;
continue;
}
case '?':
case 'h':
default:
cout << "Usage jbpf_lcm_cli [options]" << endl
<< "Options:" << endl
<< "-a <address>\tjbpf lcm ipc address (defaults to " << default_address.str() << ")" << endl
<< "-c <config>\tcodeletset configuration file" << endl
<< "-l\t\tload codeletset" << endl
<< "-u\t\tunload codeletset" << endl
<< endl;
printf("Help/Usage Example\n");
return JBPF_LCM_CLI_EXIT;
case -1:
break;
}
break;
}
address.copy(opts->addr.path, JBPF_LCM_IPC_NAME_LEN - 1);
opts->addr.path[address.length()] = '\0';
if (!exists(filename)) {
cout << "File " << filename << " does not exist" << endl;
return JBPF_LCM_CLI_INVALID_ARGS;
}
if (!set_direction) {
cout << "Invalid arguments: must specify either -l or -u" << endl;
return JBPF_LCM_CLI_INVALID_ARGS;
}
YAML::Node cfg = YAML::LoadFile(filename);
jbpf_lcm_cli::parser::parse_req_outcome parse_ret;
switch ((*opts).typ) {
case load: {
vector<string> codeletset_elems;
codeletset_elems.push_back(address);
parse_ret = jbpf_lcm_cli::parser::parse_jbpf_codeletset_load_req(cfg, &(*opts).req.load, codeletset_elems);
break;
}
case unload:
parse_ret = jbpf_lcm_cli::parser::parse_jbpf_codeletset_unload_req(cfg, &(*opts).req.unload);
break;
}
switch (parse_ret) {
case jbpf_lcm_cli::parser::JBPF_LCM_PARSE_REQ_FAILED:
default:
return JBPF_LCM_CLI_PARSE_FAILED;
case jbpf_lcm_cli::parser::JBPF_LCM_PARSE_REQ_SUCCESS:
return JBPF_LCM_CLI_REQ_SUCCESS;
case jbpf_lcm_cli::parser::JBPF_LCM_PARSE_VERIFIER_FAILED:
return JBPF_LCM_CLI_VERIFIER_FAILED;
}
}
load_req_outcome
run_loader(int ac, char** av)
{
lcm_cli_config conf = {0};
auto parse_ret = parseArgs(ac, av, &conf);
if (parse_ret == JBPF_LCM_CLI_EXIT)
return JBPF_LCM_CLI_REQ_SUCCESS;
if (parse_ret != JBPF_LCM_CLI_REQ_SUCCESS)
return parse_ret;
auto ret = 0;
switch (conf.typ) {
case load:
cout << "Loading codelet set: " << conf.req.load.codeletset_id.name << endl;
ret = jbpf_lcm_ipc_send_codeletset_load_req(&conf.addr, &conf.req.load);
break;
case unload:
cout << "Unloading codelet set: " << conf.req.unload.codeletset_id.name << endl;
ret = jbpf_lcm_ipc_send_codeletset_unload_req(&conf.addr, &conf.req.unload);
break;
}
switch (ret) {
case JBPF_LCM_IPC_REQ_SUCCESS:
return JBPF_LCM_CLI_REQ_SUCCESS;
case JBPF_LCM_IPC_REQ_FAIL:
default:
cout << "Request failed with response " << ret << endl;
return JBPF_LCM_CLI_REQ_FAILURE;
}
}
} // namespace loader
} // namespace jbpf_lcm_cli