GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — rcssserver/src/monitor.cpp at master · CLSFramework/rcssserver Expand file tree/ Copy pathmonitor.cppMore file actions 755 lines (660 loc) · 20.1 KB / Copy pathmonitor.cppFile metadata and controls755 lines (660 loc) · 20.1 KB 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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 // -*-c++-*- /*************************************************************************** monitor.cc A class providing the communication interface for remote monitors that connect to the server ------------------- begin : 27-DEC-2001 copyright : (C) 2001 by The RoboCup Soccer Server Maintainance Group. email : [email protected] ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU LGPL as published by the Free Software * * Foundation; either version 3 of the License, or (at your option) any * * later version. * * * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include "monitor.h" #include "dispsender.h" #include "initsendermonitor.h" #include "serializermonitor.h" #include "stadium.h" #include "player.h" #include "team.h" #include "types.h" #include "xpmholder.h" #include <sstream> // #ifdef HAVE_NETINET_IN_H // #include <netinet/in.h> // #endif // #ifdef HAVE_WINDOWS_H // #include <windows.h> // #endif namespace { PlayMode play_mode_id( const char * mode ) { static const char * playmode_strings[] = PLAYMODE_STRINGS; for ( int n = 0; n < PM_MAX; ++n ) { if ( ! std::strcmp( playmode_strings[n], mode ) ) { return static_cast< PlayMode >( n ); } } return PM_Null; } } Monitor::Monitor( Stadium & stadium, const double & version ) : M_init_observer( new rcss::InitObserverMonitor ), M_observer( new rcss::ObserverMonitor ), M_stadium( stadium ), M_version ( version ), M_playmode( PM_Null ), M_team_l_name( "" ), M_team_r_name( "" ), M_team_l_score( 0 ), M_team_r_score( 0 ), M_team_l_pen_taken( 0 ), M_team_r_pen_taken( 0 ) { } Monitor::~Monitor() { if ( M_init_observer ) { delete M_init_observer; M_init_observer = static_cast< rcss::InitObserverMonitor * >( 0 ); } if ( M_observer ) { delete M_observer; M_observer = static_cast< rcss::ObserverMonitor * >( 0 ); } } void Monitor::parseMsg( char * msg, const size_t & len ) { char * str = msg; if ( str[ len - 1 ] != 0 ) { if ( version() < 0 || version() >= 2.0 ) { sendMsg( MSG_BOARD, "(warning message_not_null_terminated)" ); } str[ len ] = 0; } parseCommand( str ); } bool Monitor::setSenders() { //std::cerr << "Monitor::setSenders() version=" << version() << std::endl; rcss::SerializerMonitor::Creator ser_cre; if ( ! rcss::SerializerMonitor::factory().getCreator( ser_cre, (int)version() ) ) { std::cout << "Unsupported monitor protocol version. version=" << version() << std::endl; return false; } const rcss::SerializerMonitor::Ptr ser = ser_cre(); if ( ! ser ) { std::cout << "Could not create monitor serializer. " << std::endl; return false; } // // disp sender // { rcss::DispSenderMonitor::Params disp_params( getTransport(), *this, ser, M_stadium ); rcss::DispSenderMonitor::Creator disp_cre; if ( ! rcss::DispSenderMonitor::factory().getCreator( disp_cre, (int)version() ) ) { std::cerr << "failed to create DispSenderMonitor v" << version() << std::endl; return false; } M_observer->setDispSender( disp_cre( disp_params ) ); } // // init sender // { rcss::InitSenderMonitor::Params init_params( getTransport(), *this, ser, M_stadium ); rcss::InitSenderMonitor::Creator init_cre; if ( ! rcss::InitSenderMonitor::factory().getCreator( init_cre, (int)version() ) ) { std::cerr << "failed to create InitSenderMonitor v" << version() << std::endl; return false; } M_init_observer->setInitSender( init_cre( init_params ) ); } return true; } void Monitor::sendInit() { M_init_observer->sendServerParams(); M_init_observer->sendPlayerParams(); M_init_observer->sendPlayerTypes(); } void Monitor::sendPlayMode() { if ( M_playmode == M_stadium.playmode() ) { return; } M_playmode = M_stadium.playmode(); M_init_observer->sendPlayMode(); } void Monitor::sendScore() { if ( M_team_l_score != M_stadium.teamLeft().point() || M_team_l_pen_taken != M_stadium.teamLeft().penaltyTaken() || M_team_r_score != M_stadium.teamRight().point() || M_team_r_pen_taken != M_stadium.teamRight().penaltyTaken() || M_team_l_name != M_stadium.teamLeft().name() || M_team_r_name != M_stadium.teamRight().name() ) { M_team_l_name = M_stadium.teamLeft().name(); M_team_r_name = M_stadium.teamRight().name(); M_team_l_score = M_stadium.teamLeft().point(); M_team_r_score = M_stadium.teamRight().point(); M_team_l_pen_taken = M_stadium.teamLeft().penaltyTaken(); M_team_r_pen_taken = M_stadium.teamRight().penaltyTaken(); M_init_observer->sendScore(); } } void Monitor::sendTeamGraphics() { const int MAX_SEND = ( M_stadium.playmode() == PM_BeforeKickOff ? 32 : 8 ); int send_count = 0; // int left_send_count = 0; // int right_send_count = 0; if ( M_left_sent_graphics.size() < M_stadium.teamLeft().teamGraphics().size() ) { for ( Team::GraphCont::const_reverse_iterator it = M_stadium.teamLeft().teamGraphics().rbegin(), rend = M_stadium.teamLeft().teamGraphics().rend(); it != rend; ++it ) { if ( send_count >= MAX_SEND ) break; if ( M_left_sent_graphics.find( it->first ) != M_left_sent_graphics.end() ) { continue; } // std::cerr << "Monitor::sendTeamGraphics() send left " // << it->first.first << ',' // << it->first.second // << '\n'; M_observer->sendTeamGraphic( LEFT, it->first.first, it->first.second ); M_left_sent_graphics.insert( it->first ); ++send_count; // ++left_send_count; } } if ( M_right_sent_graphics.size() < M_stadium.teamRight().teamGraphics().size() ) { for ( Team::GraphCont::const_reverse_iterator it = M_stadium.teamRight().teamGraphics().rbegin(), rend = M_stadium.teamRight().teamGraphics().rend(); it != rend; ++it ) { if ( send_count >= MAX_SEND ) break; if ( M_right_sent_graphics.find( it->first ) != M_right_sent_graphics.end() ) { continue; } // std::cerr << "Monitor::sendTeamGraphics() send right " // << it->first.first << ',' // << it->first.second // << '\n'; M_observer->sendTeamGraphic( RIGHT, it->first.first, it->first.second ); M_right_sent_graphics.insert( it->first ); ++send_count; // ++right_send_count; } } // if ( send_count > 0 ) // { // std::cerr << "Monitor::sendTeamGraphics() send_count=" << send_count << '\n' // << " left send_count=" << left_send_count << '\n' // << " right send_count=" << right_send_count << '\n' // << std::flush; // } } void Monitor::sendShow() { M_observer->sendShow(); sendTeamGraphics(); } int Monitor::sendMsg( const BoardType board, const char * msg ) { M_observer->sendMsg( board, msg ); // if ( version() >= 3.0 ) // { // char buf[MaxMesg]; // snprintf( buf, MaxMesg, // "(msg %d %d \"%s\")", // M_stadium.time(), board, msg ); // return RemoteClient::send( buf, std::strlen( buf ) + 1 ); // } // else if ( version() >= 2.0 ) // { // dispinfo_t2 minfo; // minfo.mode = htons( MSG_MODE ); // minfo.body.msg.board = htons( board ); // std::strncpy( minfo.body.msg.message, msg, max_message_length_for_display ); // return RemoteClient::send( reinterpret_cast< char * >( &minfo ), // sizeof( dispinfo_t2 ) ); // } // else if ( version() >= 1.0 ) // { // dispinfo_t minfo; // minfo.mode = htons( MSG_MODE ); // minfo.body.msg.board = htons( board ); // std::strncpy( minfo.body.msg.message, msg, max_message_length_for_display ); // return RemoteClient::send( reinterpret_cast< const char * >( &minfo ), // sizeof( dispinfo_t ) ); // } return 0; } bool Monitor::parseCommand( const char * message ) { if ( ! std::strcmp( message, "(dispbye)" ) ) { disable(); return true; } else if ( ! std::strcmp( message, "(dispstart)" ) ) { M_stadium.kickOff(); return true; } else if ( ! std::strncmp( message, "(dispplayer", 11 ) ) { return dispplayer( message ); } else if ( ! std::strncmp( message, "(dispdiscard", 12 ) ) { return dispdiscard( message ); } else if ( ! std::strncmp( message, "(compression", 12 ) ) { return compression( message ); } else if ( ! std::strncmp( message, "(dispfoul", 9 ) ) { return dispfoul( message ); } else if ( ! std::strncmp( message, "(dispcard", 9 ) ) { return dispcard( message ); } else if ( ServerParam::instance().coachMode() || ServerParam::instance().coachWithRefereeMode() ) { if ( ! std::strncmp( message, "(start)", 7 ) ) { M_stadium.kickOff(); sendMsg( MSG_BOARD, "(ok start)" ); return true; } else if ( ! std::strncmp( message, "(change_mode", 12 ) ) { return coach_change_mode( message ); } else if ( ! std::strncmp( message, "(move", 5 ) ) { return coach_move( message ); } else if ( ! std::strncmp( message, "(recover", 8 ) ) { return coach_recover(); } else if ( ! std::strcmp( message, "change_player_type" ) ) { return coach_change_player_type( message ); } else if ( ! std::strncmp( message, "(check_ball", 11 ) ) { return coach_check_ball(); } else { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } } else { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } return true; } bool Monitor::dispfoul( const char * command ) { // foul or drop_ball int x, y, side; if ( std::sscanf( command, " ( dispfoul %d %d %d ) ", &x, &y, &side ) != 3 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } double real_x = x / SHOWINFO_SCALE; double real_y = y / SHOWINFO_SCALE; if ( static_cast< Side >( side ) == NEUTRAL ) { M_stadium.dropBall( PVector( real_x, real_y ) ); } else { M_stadium.callFoul( static_cast< Side >( side ), PVector( real_x, real_y ) ); } return true; } bool Monitor::dispplayer( const char * command ) { // a player is given new position by the monitor int side, unum; int x, y, a; if ( std::sscanf( command, " ( dispplayer %d %d %d %d %d ) ", &side, &unum, &x, &y, &a ) != 5 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } double real_x = x / SHOWINFO_SCALE; double real_y = y / SHOWINFO_SCALE; double angle = Deg2Rad( a ); PVector vel( 0.0, 0.0 ); return M_stadium.movePlayer( static_cast< Side >( side ), unum, PVector( real_x, real_y ), &angle, &vel ); } bool Monitor::dispdiscard( const char * command ) { // a player is discarded by the monitor int side = 0, unum = 0; if ( std::sscanf( command, " ( dispdiscard %d %d ) ", &side, &unum ) != 2 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } M_stadium.discardPlayer( static_cast< Side >( side ), unum ); return true; } bool Monitor::dispcard( const char * command ) { // a player is punished (given the yellow/red card) by the monitor int side = 0, unum = 0; char card[8]; if ( std::sscanf( command, " ( dispcard %d %d %7[^)] ) ", &side, &unum, card ) != 3 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } if ( ! std::strcmp( "yellow", card ) ) { M_stadium.yellowCard( static_cast< Side >( side ), unum ); } else if ( ! std::strcmp( "red", card ) ) { M_stadium.redCard( static_cast< Side >( side ), unum ); } else { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } return true; } bool Monitor::compression( const char * command ) { int level = 0; if ( std::sscanf( command, " ( compression %d ) ", &level ) != 1 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } #ifdef HAVE_LIBZ if ( level > 9 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } std::ostringstream reply; reply << "(ok compression " << level << ")"; sendMsg( MSG_BOARD, reply.str().c_str() ); setCompressionLevel( level ); return true; #else sendMsg( MSG_BOARD, "(warning compression_unsupported)" ); return false; #endif } bool Monitor::coach_change_mode( const char * command ) { char new_mode[128]; if ( std::sscanf( command, " ( change_mode %127[-0-9a-zA-Z.+*/?<>_] ) ", new_mode ) != 1 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } PlayMode mode_id = play_mode_id( new_mode ); if ( mode_id == PM_Null ) { sendMsg( MSG_BOARD, "(error illegal_mode)" ); return false; } M_stadium.changePlayMode( mode_id ); sendMsg( MSG_BOARD, "(ok change_mode)" ); return true; } bool Monitor::coach_move( const char * command ) { char obj[128]; double x = 0.0, y = 0.0, ang = 0.0, velx = 0.0, vely = 0.0; int n = std::sscanf( command, " ( move ( %127[^)] ) %lf %lf %lf %lf %lf ) ", obj, &x, &y, &ang, &velx, &vely ); if ( n < 3 || std::isnan( x ) != 0 || std::isnan( y ) != 0 || std::isnan( ang ) != 0 || std::isnan( velx ) != 0 || std::isnan( vely ) != 0 ) { sendMsg( MSG_BOARD, "(error illegal_object_form)" ); return false; } std::string obj_name = "("; obj_name += obj; obj_name += ')'; if ( obj_name == BALL_NAME ) { M_stadium.clearBallCatcher(); if ( n == 3 || n == 4 ) { M_stadium.moveBall( PVector( x, y ), PVector( 0.0, 0.0 ) ); } else if ( n == 6 ) { M_stadium.moveBall( PVector( x, y ), PVector( velx, vely ) ); } else { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } } else { char teamname[128]; int unum = 0; if ( std::sscanf( obj_name.c_str(), PLAYER_NAME_FORMAT, teamname, &unum ) != 2 || unum < 1 || MAX_PLAYER < unum ) { sendMsg( MSG_BOARD, "(error illegal_object_form)" ); return false; } Side side = ( M_stadium.teamLeft().name() == teamname ? LEFT : M_stadium.teamRight().name() == teamname ? RIGHT : NEUTRAL ); PVector pos( x, y ); PVector vel( velx, vely ); ang = Deg2Rad( rcss::bound( ServerParam::instance().minMoment(), ang, ServerParam::instance().maxMoment() ) ); if ( n == 3 ) { M_stadium.movePlayer( side, unum, pos, nullptr, nullptr ); } else if ( n == 4 ) { M_stadium.movePlayer( side, unum, pos, &ang, nullptr ); } else if ( n == 6 ) { M_stadium.movePlayer( side, unum, pos, &ang, &vel ); } else { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } } sendMsg( MSG_BOARD, "(ok move)" ); return true; } bool Monitor::coach_recover() { M_stadium.recoveryPlayers(); sendMsg( MSG_BOARD, "(ok recover)" ); return true; } bool Monitor::coach_check_ball() { std::ostringstream ost; static const char * s_ball_pos_info_str[] = BALL_POS_INFO_STRINGS; BallPosInfo info = M_stadium.ballPosInfo(); ost << "(ok check_ball " << M_stadium.time() << " " ; ost << s_ball_pos_info_str[info] << ")"; ost << std::ends; sendMsg( MSG_BOARD, ost.str().c_str() ); return true; } bool Monitor::coach_change_player_type( const char * command ) { char teamname[16]; int unum, player_type; if ( std::sscanf( command, " ( change_player_type %15s %d %d ) ", teamname, &unum, &player_type ) != 3 ) { sendMsg( MSG_BOARD, "(error illegal_command_form)" ); return false; } const Team * team = nullptr; if ( M_stadium.teamLeft().name() == teamname ) { team = &( M_stadium.teamLeft() ); } else if ( M_stadium.teamRight().name() == teamname ) { team = &( M_stadium.teamRight() ); } if ( ! team ) { sendMsg( MSG_BOARD, "(warning no_team_found)" ); return false; } if ( player_type < 0 || player_type >= PlayerParam::instance().playerTypes() ) { sendMsg( MSG_BOARD, "(error out_of_range_player_type)" ); return false; } const Player * player = nullptr; for ( int i = 0; i < team->size(); ++i ) { const Player * p = team->player( i ); if ( p && p->unum() == unum ) { player = p; break; } } if ( ! player ) { sendMsg( MSG_BOARD, "(warning no_such_player)" ); return false; } M_stadium.substitute( player, player_type ); char buf[128]; snprintf( buf, 127, "(ok change_player_type %s %d %d)", teamname, unum, player_type ); sendMsg( MSG_BOARD, buf ); return true; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |