| 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 |
#include "asterisk.h" |
| 47 |
|
| 48 |
#include <math.h> |
| 49 |
#include "asterisk/file.h" |
| 50 |
#include "asterisk/channel.h" |
| 51 |
#include "asterisk/pbx.h" |
| 52 |
#include "asterisk/module.h" |
| 53 |
#include "asterisk/lock.h" |
| 54 |
#include "asterisk/app.h" |
| 55 |
#include "asterisk/config.h" |
| 56 |
#include "asterisk/config_options.h" |
| 57 |
#include "asterisk/say.h" |
| 58 |
#include "asterisk/astobj2.h" |
| 59 |
#include "asterisk/acl.h" |
| 60 |
#include "asterisk/netsock2.h" |
| 61 |
#include "asterisk/strings.h" |
| 62 |
#include "asterisk/cli.h" |
| 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 |
static char *app = "SkelGuessNumber"; |
| 178 |
|
| 179 |
enum option_flags { |
| 180 |
OPTION_CHEAT = (1 << 0), |
| 181 |
OPTION_NUMGAMES = (1 << 1), |
| 182 |
}; |
| 183 |
|
| 184 |
enum option_args { |
| 185 |
OPTION_ARG_NUMGAMES, |
| 186 |
|
| 187 |
OPTION_ARG_ARRAY_SIZE, |
| 188 |
}; |
| 189 |
|
| 190 |
AST_APP_OPTIONS(app_opts,{ |
| 191 |
AST_APP_OPTION('c', OPTION_CHEAT), |
| 192 |
AST_APP_OPTION_ARG('n', OPTION_NUMGAMES, OPTION_ARG_NUMGAMES), |
| 193 |
}); |
| 194 |
|
| 195 |
|
| 196 |
struct skel_global_config { |
| 197 |
AST_DECLARE_STRING_FIELDS( |
| 198 |
AST_STRING_FIELD(prompt); |
| 199 |
AST_STRING_FIELD(wrong); |
| 200 |
AST_STRING_FIELD(right); |
| 201 |
AST_STRING_FIELD(high); |
| 202 |
AST_STRING_FIELD(low); |
| 203 |
AST_STRING_FIELD(lose); |
| 204 |
); |
| 205 |
uint32_t num_games; |
| 206 |
unsigned char cheat:1; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
struct skel_level_state { |
| 211 |
uint32_t wins; |
| 212 |
uint32_t losses; |
| 213 |
double avg_guesses; |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
struct skel_level { |
| 221 |
AST_DECLARE_STRING_FIELDS( |
| 222 |
AST_STRING_FIELD(name); |
| 223 |
); |
| 224 |
uint32_t max_num; |
| 225 |
uint32_t max_guesses; |
| 226 |
struct skel_level_state *state; |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
struct skel_current_game { |
| 237 |
uint32_t total_games; |
| 238 |
uint32_t games_left; |
| 239 |
uint32_t cheat; |
| 240 |
struct skel_level *level_info; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
#define LEVEL_BUCKETS 1 |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
struct skel_config { |
| 253 |
struct skel_global_config *global; |
| 254 |
struct ao2_container *levels; |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static void *skel_config_alloc(void); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static void *skel_level_alloc(const char *cat); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static void *skel_level_find(struct ao2_container *tmp_container, const char *category); |
| 282 |
|
| 283 |
|
| 284 |
static struct aco_type global_option = { |
| 285 |
.type = ACO_GLOBAL, |
| 286 |
.name = "globals", |
| 287 |
.item_offset = offsetof(struct skel_config, global), |
| 288 |
.category_match = ACO_WHITELIST_EXACT, |
| 289 |
.category = "general", |
| 290 |
}; |
| 291 |
|
| 292 |
struct aco_type *global_options[] = ACO_TYPES(&global_option); |
| 293 |
|
| 294 |
|
| 295 |
static struct aco_type sound_option = { |
| 296 |
.type = ACO_GLOBAL, |
| 297 |
.name = "sounds", |
| 298 |
.item_offset = offsetof(struct skel_config, global), |
| 299 |
.category_match = ACO_WHITELIST_EXACT, |
| 300 |
.category = "sounds", |
| 301 |
}; |
| 302 |
|
| 303 |
struct aco_type *sound_options[] = ACO_TYPES(&sound_option); |
| 304 |
|
| 305 |
static const char *level_categories[] = { |
| 306 |
"general", |
| 307 |
"sounds", |
| 308 |
NULL, |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
static struct aco_type level_option = { |
| 313 |
.type = ACO_ITEM, |
| 314 |
.name = "level", |
| 315 |
.category_match = ACO_BLACKLIST_ARRAY, |
| 316 |
.category = (const char *)level_categories, |
| 317 |
.item_alloc = skel_level_alloc, |
| 318 |
.item_find = skel_level_find, |
| 319 |
.item_offset = offsetof(struct skel_config, levels), |
| 320 |
}; |
| 321 |
|
| 322 |
struct aco_type *level_options[] = ACO_TYPES(&level_option); |
| 323 |
|
| 324 |
struct aco_file app_skel_conf = { |
| 325 |
.filename = "app_skel.conf", |
| 326 |
.types = ACO_TYPES(&global_option, &sound_option, &level_option), |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
static AO2_GLOBAL_OBJ_STATIC(globals); |
| 331 |
|
| 332 |
|
| 333 |
static struct ao2_container *games; |
| 334 |
|
| 335 |
|
| 336 |
CONFIG_INFO_STANDARD(cfg_info, globals, skel_config_alloc, |
| 337 |
.files = ACO_FILES(&app_skel_conf), |
| 338 |
); |
| 339 |
|
| 340 |
static void skel_global_config_destructor(void *obj) |
| 341 |
{ |
| 342 |
struct skel_global_config *global = obj; |
| 343 |
ast_string_field_free_memory(global); |
| 344 |
} |
| 345 |
|
| 346 |
static void skel_game_destructor(void *obj) |
| 347 |
{ |
| 348 |
struct skel_current_game *game = obj; |
| 349 |
ao2_cleanup(game->level_info); |
| 350 |
} |
| 351 |
|
| 352 |
static void skel_state_destructor(void *obj) |
| 353 |
{ |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
static struct skel_current_game *skel_game_alloc(struct skel_level *level) |
| 358 |
{ |
| 359 |
struct skel_current_game *game; |
| 360 |
if (!(game = ao2_alloc(sizeof(struct skel_current_game), skel_game_destructor))) { |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
ao2_ref(level, +1); |
| 364 |
game->level_info = level; |
| 365 |
return game; |
| 366 |
} |
| 367 |
|
| 368 |
static void skel_level_destructor(void *obj) |
| 369 |
{ |
| 370 |
struct skel_level *level = obj; |
| 371 |
ast_string_field_free_memory(level); |
| 372 |
ao2_cleanup(level->state); |
| 373 |
} |
| 374 |
|
| 375 |
static int skel_level_hash(const void *obj, const int flags) |
| 376 |
{ |
| 377 |
const struct skel_level *level = obj; |
| 378 |
const char *name = (flags & OBJ_KEY) ? obj : level->name; |
| 379 |
return ast_str_case_hash(name); |
| 380 |
} |
| 381 |
|
| 382 |
static int skel_level_cmp(void *obj, void *arg, int flags) |
| 383 |
{ |
| 384 |
struct skel_level *one = obj, *two = arg; |
| 385 |
const char *match = (flags & OBJ_KEY) ? arg : two->name; |
| 386 |
return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static int custom_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 400 |
{ |
| 401 |
struct skel_global_config *global = obj; |
| 402 |
|
| 403 |
if (!strcasecmp(var->name, "cheat")) { |
| 404 |
global->cheat = ast_true(var->value); |
| 405 |
} else { |
| 406 |
return -1; |
| 407 |
} |
| 408 |
|
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
static void play_files_helper(struct ast_channel *chan, const char *prompts) |
| 413 |
{ |
| 414 |
char *prompt, *rest = ast_strdupa(prompts); |
| 415 |
|
| 416 |
ast_stopstream(chan); |
| 417 |
while ((prompt = ast_strsep(&rest, '&', AST_STRSEP_STRIP | AST_STRSEP_TRIM)) |
| 418 |
&& !ast_stream_and_wait(chan, prompt, "")) { |
| 419 |
ast_stopstream(chan); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
static int app_exec(struct ast_channel *chan, const char *data) |
| 424 |
{ |
| 425 |
int win = 0; |
| 426 |
uint32_t guesses; |
| 427 |
RAII_VAR(struct skel_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 428 |
RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup); |
| 429 |
RAII_VAR(struct skel_current_game *, game, NULL, ao2_cleanup); |
| 430 |
char *parse, *opts[OPTION_ARG_ARRAY_SIZE]; |
| 431 |
struct ast_flags flags; |
| 432 |
AST_DECLARE_APP_ARGS(args, |
| 433 |
AST_APP_ARG(level); |
| 434 |
AST_APP_ARG(options); |
| 435 |
); |
| 436 |
|
| 437 |
if (!cfg) { |
| 438 |
ast_log(LOG_ERROR, "Couldn't access configuration data!\n"); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
if (ast_strlen_zero(data)) { |
| 443 |
ast_log(LOG_WARNING, "%s requires an argument (level[,options])\n", app); |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
parse = ast_strdupa(data); |
| 449 |
|
| 450 |
AST_STANDARD_APP_ARGS(args, parse); |
| 451 |
|
| 452 |
if (args.argc == 2) { |
| 453 |
ast_app_parse_options(app_opts, &flags, opts, args.options); |
| 454 |
} |
| 455 |
|
| 456 |
if (ast_strlen_zero(args.level)) { |
| 457 |
ast_log(LOG_ERROR, "%s requires a level argument\n", app); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
if (!(level = ao2_find(cfg->levels, args.level, OBJ_KEY))) { |
| 462 |
ast_log(LOG_ERROR, "Unknown level: %s\n", args.level); |
| 463 |
return -1; |
| 464 |
} |
| 465 |
|
| 466 |
if (!(game = skel_game_alloc(level))) { |
| 467 |
return -1; |
| 468 |
} |
| 469 |
|
| 470 |
ao2_link(games, game); |
| 471 |
|
| 472 |
|
| 473 |
if (!ast_test_flag(&flags, OPTION_NUMGAMES) || |
| 474 |
ast_strlen_zero(opts[OPTION_ARG_NUMGAMES]) || |
| 475 |
ast_parse_arg(opts[OPTION_ARG_NUMGAMES], PARSE_UINT32, &game->total_games)) { |
| 476 |
game->total_games = cfg->global->num_games; |
| 477 |
} |
| 478 |
game->games_left = game->total_games; |
| 479 |
game->cheat = ast_test_flag(&flags, OPTION_CHEAT) || cfg->global->cheat; |
| 480 |
|
| 481 |
for (game->games_left = game->total_games; game->games_left; game->games_left--) { |
| 482 |
uint32_t num = ast_random() % level->max_num; |
| 483 |
|
| 484 |
ast_debug(1, "They should totally should guess %u\n", num); |
| 485 |
|
| 486 |
|
| 487 |
play_files_helper(chan, cfg->global->prompt); |
| 488 |
ast_say_number(chan, level->max_num, "", ast_channel_language(chan), ""); |
| 489 |
|
| 490 |
for (guesses = 0; guesses < level->max_guesses; guesses++) { |
| 491 |
size_t buflen = log10(level->max_num) + 1; |
| 492 |
char buf[buflen]; |
| 493 |
int guess; |
| 494 |
buf[buflen] = '\0'; |
| 495 |
|
| 496 |
|
| 497 |
ast_readstring(chan, buf, buflen - 1, 2000, 10000, ""); |
| 498 |
if (ast_parse_arg(buf, PARSE_INT32 | PARSE_IN_RANGE, &guess, 0, level->max_num)) { |
| 499 |
if (guesses < level->max_guesses - 1) { |
| 500 |
play_files_helper(chan, cfg->global->wrong); |
| 501 |
} |
| 502 |
continue; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
if (guess == num && !game->cheat) { |
| 507 |
|
| 508 |
win = 1; |
| 509 |
play_files_helper(chan, cfg->global->right); |
| 510 |
guesses++; |
| 511 |
break; |
| 512 |
} else if (guess < num) { |
| 513 |
play_files_helper(chan, cfg->global->low); |
| 514 |
} else { |
| 515 |
play_files_helper(chan, cfg->global->high); |
| 516 |
} |
| 517 |
|
| 518 |
if (guesses < level->max_guesses - 1) { |
| 519 |
play_files_helper(chan, cfg->global->wrong); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
ao2_lock(level->state); |
| 525 |
if (win) { |
| 526 |
++level->state->wins; |
| 527 |
level->state->avg_guesses = ((level->state->wins - 1) * level->state->avg_guesses + guesses) / level->state->wins; |
| 528 |
} else { |
| 529 |
|
| 530 |
level->state->losses++; |
| 531 |
play_files_helper(chan, cfg->global->lose); |
| 532 |
} |
| 533 |
ao2_unlock(level->state); |
| 534 |
} |
| 535 |
|
| 536 |
ao2_unlink(games, game); |
| 537 |
|
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
static struct skel_level *skel_state_alloc(const char *name) |
| 542 |
{ |
| 543 |
struct skel_level *level; |
| 544 |
|
| 545 |
if (!(level = ao2_alloc(sizeof(*level), skel_state_destructor))) { |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
|
| 549 |
return level; |
| 550 |
} |
| 551 |
|
| 552 |
static void *skel_level_find(struct ao2_container *tmp_container, const char *category) |
| 553 |
{ |
| 554 |
return ao2_find(tmp_container, category, OBJ_KEY); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static void *skel_find_or_create_state(const char *category) |
| 566 |
{ |
| 567 |
RAII_VAR(struct skel_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup); |
| 568 |
RAII_VAR(struct skel_level *, level, NULL, ao2_cleanup); |
| 569 |
if (!cfg || !cfg->levels || !(level = ao2_find(cfg->levels, category, OBJ_KEY))) { |
| 570 |
return skel_state_alloc(category); |
| 571 |
} |
| 572 |
ao2_ref(level->state, +1); |
| 573 |
return level->state; |
| 574 |
} |
| 575 |
|
| 576 |
static void *skel_level_alloc(const char *cat) |
| 577 |
{ |
| 578 |
struct skel_level *level; |
| 579 |
|
| 580 |
if (!(level = ao2_alloc(sizeof(*level), skel_level_destructor))) { |
| 581 |
return NULL; |
| 582 |
} |
| 583 |
|
| 584 |
if (ast_string_field_init(level, 128)) { |
| 585 |
ao2_ref(level, -1); |
| 586 |
return NULL; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
if (!(level->state = skel_find_or_create_state(cat))) { |
| 594 |
ao2_ref(level, -1); |
| 595 |
return NULL; |
| 596 |
} |
| 597 |
|
| 598 |
ast_string_field_set(level, name, cat); |
| 599 |
|
| 600 |
return level; |
| 601 |
} |
| 602 |
|
| 603 |
static void skel_config_destructor(void *obj) |
| 604 |
{ |
| 605 |
struct skel_config *cfg = obj; |
| 606 |
ao2_cleanup(cfg->global); |
| 607 |
ao2_cleanup(cfg->levels); |
| 608 |
} |
| 609 |
|
| 610 |
static void *skel_config_alloc(void) |
| 611 |
{ |
| 612 |
struct skel_config *cfg; |
| 613 |
|
| 614 |
if (!(cfg = ao2_alloc(sizeof(*cfg), skel_config_destructor))) { |
| 615 |
return NULL; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), skel_global_config_destructor))) { |
| 620 |
goto error; |
| 621 |
} |
| 622 |
|
| 623 |
if (ast_string_field_init(cfg->global, 128)) { |
| 624 |
goto error; |
| 625 |
} |
| 626 |
|
| 627 |
cfg->levels = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, LEVEL_BUCKETS, |
| 628 |
skel_level_hash, NULL, skel_level_cmp); |
| 629 |
if (!cfg->levels) { |
| 630 |
goto error; |
| 631 |
} |
| 632 |
|
| 633 |
return cfg; |
| 634 |
error: |
| 635 |
ao2_ref(cfg, -1); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
static char *handle_skel_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 640 |
{ |
| 641 |
RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup); |
| 642 |
|
| 643 |
switch(cmd) { |
| 644 |
case CLI_INIT: |
| 645 |
e->command = "skel show config"; |
| 646 |
e->usage = |
| 647 |
"Usage: skel show config\n" |
| 648 |
" List app_skel global config\n"; |
| 649 |
return NULL; |
| 650 |
case CLI_GENERATE: |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
|
| 654 |
if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->global) { |
| 655 |
return NULL; |
| 656 |
} |
| 657 |
|
| 658 |
ast_cli(a->fd, "games per call: %u\n", cfg->global->num_games); |
| 659 |
ast_cli(a->fd, "computer cheats: %s\n", AST_CLI_YESNO(cfg->global->cheat)); |
| 660 |
ast_cli(a->fd, "\n"); |
| 661 |
ast_cli(a->fd, "Sounds\n"); |
| 662 |
ast_cli(a->fd, " prompt: %s\n", cfg->global->prompt); |
| 663 |
ast_cli(a->fd, " wrong guess: %s\n", cfg->global->wrong); |
| 664 |
ast_cli(a->fd, " right guess: %s\n", cfg->global->right); |
| 665 |
|
| 666 |
return CLI_SUCCESS; |
| 667 |
} |
| 668 |
|
| 669 |
static char *handle_skel_show_games(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 670 |
{ |
| 671 |
struct ao2_iterator iter; |
| 672 |
struct skel_current_game *game; |
| 673 |
|
| 674 |
switch(cmd) { |
| 675 |
case CLI_INIT: |
| 676 |
e->command = "skel show games"; |
| 677 |
e->usage = |
| 678 |
"Usage: skel show games\n" |
| 679 |
" List app_skel active games\n"; |
| 680 |
return NULL; |
| 681 |
case CLI_GENERATE: |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
|
| 685 |
#define SKEL_FORMAT "%-15.15s %-15.15s %-15.15s\n" |
| 686 |
#define SKEL_FORMAT1 "%-15.15s %-15u %-15u\n" |
| 687 |
ast_cli(a->fd, SKEL_FORMAT, "Level", "Total Games", "Games Left"); |
| 688 |
iter = ao2_iterator_init(games, 0); |
| 689 |
while ((game = ao2_iterator_next(&iter))) { |
| 690 |
ast_cli(a->fd, SKEL_FORMAT1, game->level_info->name, game->total_games, game->games_left); |
| 691 |
ao2_ref(game, -1); |
| 692 |
} |
| 693 |
ao2_iterator_destroy(&iter); |
| 694 |
#undef SKEL_FORMAT |
| 695 |
#undef SKEL_FORMAT1 |
| 696 |
return CLI_SUCCESS; |
| 697 |
} |
| 698 |
|
| 699 |
static char *handle_skel_show_levels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 700 |
{ |
| 701 |
RAII_VAR(struct skel_config *, cfg, NULL, ao2_cleanup); |
| 702 |
struct ao2_iterator iter; |
| 703 |
struct skel_level *level; |
| 704 |
|
| 705 |
switch(cmd) { |
| 706 |
case CLI_INIT: |
| 707 |
e->command = "skel show levels"; |
| 708 |
e->usage = |
| 709 |
"Usage: skel show levels\n" |
| 710 |
" List the app_skel levels\n"; |
| 711 |
return NULL; |
| 712 |
case CLI_GENERATE: |
| 713 |
return NULL; |
| 714 |
} |
| 715 |
|
| 716 |
if (!(cfg = ao2_global_obj_ref(globals)) || !cfg->levels) { |
| 717 |
return NULL; |
| 718 |
} |
| 719 |
|
| 720 |
#define SKEL_FORMAT "%-15.15s %-11.11s %-12.12s %-8.8s %-8.8s %-12.12s\n" |
| 721 |
#define SKEL_FORMAT1 "%-15.15s %-11u %-12u %-8u %-8u %-8f\n" |
| 722 |
ast_cli(a->fd, SKEL_FORMAT, "Name", "Max number", "Max Guesses", "Wins", "Losses", "Avg Guesses"); |
| 723 |
iter = ao2_iterator_init(cfg->levels, 0); |
| 724 |
while ((level = ao2_iterator_next(&iter))) { |
| 725 |
ast_cli(a->fd, SKEL_FORMAT1, level->name, level->max_num, level->max_guesses, level->state->wins, level->state->losses, level->state->avg_guesses); |
| 726 |
ao2_ref(level, -1); |
| 727 |
} |
| 728 |
ao2_iterator_destroy(&iter); |
| 729 |
#undef SKEL_FORMAT |
| 730 |
#undef SKEL_FORMAT1 |
| 731 |
|
| 732 |
return CLI_SUCCESS; |
| 733 |
} |
| 734 |
|
| 735 |
static struct ast_cli_entry skel_cli[] = { |
| 736 |
AST_CLI_DEFINE(handle_skel_show_config, "Show app_skel global config options"), |
| 737 |
AST_CLI_DEFINE(handle_skel_show_levels, "Show app_skel levels"), |
| 738 |
AST_CLI_DEFINE(handle_skel_show_games, "Show app_skel active games"), |
| 739 |
}; |
| 740 |
|
| 741 |
static int reload_module(void) |
| 742 |
{ |
| 743 |
if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) { |
| 744 |
return AST_MODULE_LOAD_DECLINE; |
| 745 |
} |
| 746 |
|
| 747 |
return 0; |
| 748 |
} |
| 749 |
|
| 750 |
static int unload_module(void) |
| 751 |
{ |
| 752 |
ast_cli_unregister_multiple(skel_cli, ARRAY_LEN(skel_cli)); |
| 753 |
aco_info_destroy(&cfg_info); |
| 754 |
ao2_global_obj_release(globals); |
| 755 |
ao2_cleanup(games); |
| 756 |
return ast_unregister_application(app); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
static int load_module(void) |
| 770 |
{ |
| 771 |
if (aco_info_init(&cfg_info)) { |
| 772 |
goto error; |
| 773 |
} |
| 774 |
|
| 775 |
games = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL); |
| 776 |
if (!games) { |
| 777 |
goto error; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
aco_option_register(&cfg_info, "games", ACO_EXACT, global_options, "3", OPT_UINT_T, 0, FLDSET(struct skel_global_config, num_games)); |
| 782 |
aco_option_register_custom(&cfg_info, "cheat", ACO_EXACT, global_options, "no", custom_bitfield_handler, 0); |
| 783 |
|
| 784 |
|
| 785 |
aco_option_register(&cfg_info, "prompt", ACO_EXACT, sound_options, "please-enter-your&number&queue-less-than", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, prompt)); |
| 786 |
aco_option_register(&cfg_info, "wrong_guess", ACO_EXACT, sound_options, "vm-pls-try-again", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, wrong)); |
| 787 |
aco_option_register(&cfg_info, "right_guess", ACO_EXACT, sound_options, "auth-thankyou", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, right)); |
| 788 |
aco_option_register(&cfg_info, "too_high", ACO_EXACT, sound_options, "high", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, high)); |
| 789 |
aco_option_register(&cfg_info, "too_low", ACO_EXACT, sound_options, "low", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, low)); |
| 790 |
aco_option_register(&cfg_info, "lose", ACO_EXACT, sound_options, "vm-goodbye", OPT_STRINGFIELD_T, 0, STRFLDSET(struct skel_global_config, lose)); |
| 791 |
|
| 792 |
|
| 793 |
aco_option_register(&cfg_info, "max_number", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_num)); |
| 794 |
aco_option_register(&cfg_info, "max_guesses", ACO_EXACT, level_options, NULL, OPT_UINT_T, 0, FLDSET(struct skel_level, max_guesses)); |
| 795 |
|
| 796 |
if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) { |
| 797 |
goto error; |
| 798 |
} |
| 799 |
|
| 800 |
ast_cli_register_multiple(skel_cli, ARRAY_LEN(skel_cli)); |
| 801 |
if (ast_register_application_xml(app, app_exec)) { |
| 802 |
goto error; |
| 803 |
} |
| 804 |
return AST_MODULE_LOAD_SUCCESS; |
| 805 |
|
| 806 |
error: |
| 807 |
aco_info_destroy(&cfg_info); |
| 808 |
ao2_cleanup(games); |
| 809 |
return AST_MODULE_LOAD_DECLINE; |
| 810 |
} |
| 811 |
|
| 812 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Skeleton (sample) Application", |
| 813 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 814 |
.load = load_module, |
| 815 |
.unload = unload_module, |
| 816 |
.reload = reload_module, |
| 817 |
); |