-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsx_123.cpp
419 lines (353 loc) · 7.77 KB
/
awsx_123.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/***************************************************************************
* Copyright (C) 2022, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include <iostream>
#include <signal.h>
#include <getopt.h>
#include "awsx9.h"
#define TAG "awsx_123"
#define USE_AWSX9
// ** app **
static int is_quit = 0;
static int test_DynamoDB = 0;
static int test_S3 = 0;
static int app_quit(void)
{
return is_quit;
}
static void app_set_quit(int mode)
{
is_quit = mode;
}
#ifdef USE_AWSX9
#define DYDB_TABLE_NAME_MUSIC "Music"
#define DYDB_PK_NAME_ARTIST "Artist"
#define DYDB_PK_VAL_LANKA "Lanka"
#define DYDB_PK_VAL_ACME_BAND "Acme Band"
#define DYDB_SK_NAME_SONG_TITLE "SongTitle"
#define DYDB_SK_VAL_LANKA "Lanka"
#define DYDB_SK_VAL_HAPPY_DAY "Happy Day"
DyDB_InfoX_t dydb_t_Music =
{
.dydb_cli = NULL,
.isinit = 0,
.isfree = 0,
.table_name = DYDB_TABLE_NAME_MUSIC,
.pk = DYDB_PK_NAME_ARTIST,
.pk_val = DYDB_PK_VAL_LANKA,
.sk = DYDB_SK_NAME_SONG_TITLE,
.sk_val = DYDB_SK_VAL_LANKA,
};
#define DYDB_TABLE_NAME_DEMO "Demo"
#define DYDB_PK_NAME_PK "PK"
#define DYDB_SK_NAME_SK "SK"
DyDB_InfoX_t dydb_t_Demo =
{
.dydb_cli = NULL,
.isinit = 0,
.isfree = 0,
.table_name = DYDB_TABLE_NAME_DEMO,
.pk = DYDB_PK_NAME_PK,
.pk_val = DYDB_PK_VAL_LANKA,
.sk = DYDB_SK_NAME_SK,
.sk_val = DYDB_SK_VAL_HAPPY_DAY,
};
static void aws_dynamodb_demo(void)
{
DBG_IF_LN(DBG_TXT_ENTER);
{
dydb_ctx_init(&dydb_t_Music, awsX_dydb_cli_get());
dydb_ctx_init(&dydb_t_Demo, awsX_dydb_cli_get());
}
#if (1)
{
DyDB_InfoX_t *dydb_ctx = &dydb_t_Music;
#if (1)
DBG_WN_LN(">>>>> dydb_del_item <<<<<");
{
//** del_item **
dydb_del_item(dydb_ctx);
}
#endif
#if (1)
DBG_WN_LN(">>>>> dydb_put_item <<<<<");
{
//** put_item **
dydb_ctx_attrX_free(dydb_ctx);
dydb_ctx_attrX_addS(dydb_ctx, (char*)"AlbumTitle", (char*)"Album123");
dydb_ctx_attrX_addS(dydb_ctx, (char*)"Awards", (char*)"1");
char sponsor_val[LEN_OF_VAL128] = "dog:mouse:tiger";
dydb_ctx_attrX_addListS(dydb_ctx, (char*)"Sponsor", sponsor_val);
dydb_put_item(dydb_ctx);
}
{
dydb_get_item(dydb_ctx);
dydb_show_attrX(dydb_ctx);
}
#endif
#if (1)
DBG_WN_LN(">>>>> dydb_update_item <<<<<");
{
dydb_ctx_attrX_free(dydb_ctx);
dydb_ctx_attrX_addS(dydb_ctx, (char*)"Awards", (char*)"1");
dydb_ctx_attrX_addN(dydb_ctx, (char*)"garbage", 4567);
dydb_update_item(dydb_ctx);
}
{
dydb_get_item(dydb_ctx);
dydb_show_attrX(dydb_ctx);
}
#endif
#if (1)
DBG_WN_LN(">>>>> dydb_remove_attributes <<<<<");
{
char attributes[LEN_OF_VAL1024] = "garbage";
dydb_remove_attributes(dydb_ctx, attributes);
dydb_get_item(dydb_ctx);
dydb_show_attrX(dydb_ctx);
}
#endif
#if (1)
DBG_WN_LN(">>>>> dydb_query_item <<<<<");
{
dydb_query_item(dydb_ctx);
dydb_show_itemX(dydb_ctx);
}
#endif
#if (1)
DBG_WN_LN(">>>>> dydb_scan_item <<<<<");
{
dydb_scan_item(dydb_ctx);
dydb_show_itemX(dydb_ctx);
}
#endif
}
#endif
#if (1)
{
DyDB_InfoX_t *dydb_ctx = &dydb_t_Demo;
#if (1)
dydb_list_tables(dydb_ctx);
dydb_show_tableX(dydb_ctx);
DBG_WN_LN(">>>>> dydb_create_table <<<<<");
dydb_create_table(dydb_ctx);
dydb_list_tables(dydb_ctx);
dydb_show_tableX(dydb_ctx);
#endif
DBG_WN_LN(">>>>> dydb_describe_table <<<<<");
dydb_describe_table(dydb_ctx);
dydb_show_table(dydb_ctx->tableDesc);
#if (1)
DBG_WN_LN(">>>>> dydb_delete_table <<<<<");
dydb_delete_table(dydb_ctx);
dydb_list_tables(dydb_ctx);
dydb_show_tableX(dydb_ctx);
#endif
}
#endif
}
S3_InfoX_t s3_b_Demo =
{
.s3_cli = NULL,
.isinit = 0,
.isfree = 0,
};
static void aws_s3_demo(void)
{
DBG_IF_LN(DBG_TXT_ENTER);
#if (1)
{
S3_InfoX_t *s3_ctx = &s3_b_Demo;
s3_ctx_init(s3_ctx, awsX_s3_cli_get());
char to_bucket[LEN_OF_DIRNAME512] = "demox9";
char from_bucket[LEN_OF_DIRNAME512] = "demox9";
#if (1)
DBG_WN_LN(">>>>> s3_put_object (local -> bucket/key) <<<<<");
s3_ctx_init_put(s3_ctx, (char *)"README.md", to_bucket, (char *)"love_letter_s3.txt");
s3_put_object(s3_ctx);
#endif
#if (1)
DBG_WN_LN(">>>>> s3_get_object (bucket/key -> local) <<<<<");
s3_ctx_init_get(s3_ctx, from_bucket, (char *)"love_letter_s3.txt", (char *)"love_letter.txt");
s3_get_object(s3_ctx);
#endif
#if (1)
DBG_WN_LN(">>>>> s3_copy_object (bucket/key -> bucket/key) <<<<<");
s3_ctx_init_copy(s3_ctx, from_bucket, (char *)"love_letter_s3.txt", to_bucket, (char *)"love_letter_bak.txt");
s3_copy_object(s3_ctx);
#endif
#if (1)
DBG_WN_LN(">>>>> s3_get_object (bucket/key -> NULL) <<<<<");
s3_ctx_init_delete(s3_ctx, from_bucket, (char *)"love_letter_bak.txt");
s3_delete_object(s3_ctx);
#endif
}
#endif
}
static void aws_open(void)
{
if (test_DynamoDB == 1)
{
aws_dynamodb_demo();
}
if (test_S3 == 1)
{
aws_s3_demo();
}
}
static void aws_free(void)
{
if (test_DynamoDB == 1)
{
dydb_ctx_free(&dydb_t_Music);
dydb_ctx_free(&dydb_t_Demo);
}
if (test_S3 == 1)
{
s3_ctx_free(&s3_b_Demo);
}
awsX_free();
}
static void aws_init(void)
{
awsX_init();
}
#endif
static void app_stop(void)
{
if (app_quit()==0)
{
app_set_quit(1);
#ifdef USE_AWSX9
aws_free();
#endif
}
}
static void app_loop(void)
{
#ifdef USE_AWSX9
aws_open();
#endif
}
static int app_init(void)
{
int ret = 0;
return ret;
}
static void app_exit(void)
{
app_stop();
}
static void app_signal_handler(int signum)
{
DBG_ER_LN("(signum: %d)", signum);
switch (signum)
{
case SIGINT:
case SIGTERM:
case SIGHUP:
app_stop();
break;
case SIGPIPE:
break;
case SIGUSR1:
break;
case SIGUSR2:
dbg_lvl_round();
DBG_ER_LN("dbg_lvl_get(): %d", dbg_lvl_get());
DBG_ER_LN("(Version: %s)", version_show());
break;
}
}
static void app_signal_register(void)
{
signal(SIGINT, app_signal_handler);
signal(SIGTERM, app_signal_handler);
signal(SIGHUP, app_signal_handler);
signal(SIGUSR1, app_signal_handler);
signal(SIGUSR2, app_signal_handler);
signal(SIGPIPE, SIG_IGN);
}
int option_index = 0;
const char* short_options = "d:bsh";
static struct option long_options[] =
{
{ "debug", required_argument, NULL, 'd' },
{ "db", no_argument, NULL, 'b' },
{ "s3", no_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
static void app_showusage(int exit_code)
{
printf("Usage: %s\n"
" -d, --debug debug level\n"
" -b, --db DynamoDB Demo \n"
" -s, --s3 S3 Demo \n"
" -h, --help\n", TAG);
printf("Version: %s\n", version_show());
printf("Example:\n"
" %s -d 4\n", TAG);
exit(exit_code);
}
static void app_ParseArguments(int argc, char **argv)
{
int opt;
while ((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1)
{
switch (opt)
{
case 'd':
if (optarg)
{
dbg_lvl_set(atoi(optarg));
}
break;
case 'b':
{
test_DynamoDB = 1;
}
break;
case 's':
{
test_S3 = 1;
}
break;
default:
app_showusage(-1);
break;
}
}
if ((test_DynamoDB | test_S3) == 0)
{
app_showusage(-1);
}
}
int main(int argc, char** argv)
{
#ifdef USE_AWSX9
// must run before app_ParseArguments
aws_init();
#endif
app_ParseArguments(argc, argv);
app_signal_register();
atexit(app_exit);
if (app_init() == -1)
{
return -1;
}
app_loop();
DBG_WN_LN(DBG_TXT_BYE_BYE);
return 0;
}