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 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2013 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation "AsyncRPC.h"
#endif
#ifdef _WIN32
#include "boinc_win.h"
#endif
#include "config.h"
#if HAVE_XLOCALE_H
#include <xlocale.h>
#endif
#include "stdwx.h"
#include "BOINCGUIApp.h"
#include "MainDocument.h"
#include "AsyncRPC.h"
#include "BOINCBaseFrame.h"
#include "BOINCTaskBar.h"
#include "error_numbers.h"
#include "SkinManager.h"
#include "DlgEventLog.h"
#include "util.h"
extern bool s_bSkipExitConfirmation;
// Delay in milliseconds before showing AsyncRPCDlg during auto-attach to project
#define RPC_WAIT_DLG_DELAY_DURING_AUTOATTACH 60000
// Delay in milliseconds before showing AsyncRPCDlg normally
#define RPC_WAIT_DLG_DELAY 1500
// How often to check for events when minimized and waiting for Demand RPC
#define DELAY_WHEN_MINIMIZED 500
// Delay in milliseconds to allow thread to exit before killing it
#define RPC_KILL_DELAY 2000
ASYNC_RPC_REQUEST::ASYNC_RPC_REQUEST() {
clear();
}
ASYNC_RPC_REQUEST::~ASYNC_RPC_REQUEST() {
clear();
}
void ASYNC_RPC_REQUEST::clear() {
rpcType = (ASYNC_RPC_TYPE) 0;
which_rpc = (RPC_SELECTOR) 0;
exchangeBuf = NULL;
arg1 = NULL;
arg2 = NULL;
arg3 = NULL;
arg4 = NULL;
completionTime = NULL;
RPCExecutionTime = NULL;
resultPtr = NULL;
retval = 0;
isActive = false;
}
bool ASYNC_RPC_REQUEST::isSameAs(ASYNC_RPC_REQUEST& otherRequest) {
if (which_rpc != otherRequest.which_rpc) return false;
if (arg1 != otherRequest.arg1) return false;
if (exchangeBuf != otherRequest.exchangeBuf) return false;
if (arg2 != otherRequest.arg2) return false;
if (arg3 != otherRequest.arg3) return false;
if (arg4 != otherRequest.arg4) return false;
if (rpcType != otherRequest.rpcType) return false;
if (completionTime != otherRequest.completionTime) return false;
if (resultPtr != otherRequest.resultPtr) return false;
// OK if isActive and retval don't match.
return true;
}
AsyncRPC::AsyncRPC(CMainDocument *pDoc) {
m_pDoc = pDoc;
}
AsyncRPC::~AsyncRPC() {}
int AsyncRPC::RPC_Wait(RPC_SELECTOR which_rpc, void *arg1, void *arg2,
void *arg3, void *arg4, bool hasPriority
) {
ASYNC_RPC_REQUEST request;
int retval = 0;
request.which_rpc = which_rpc;
request.arg1 = arg1;
request.arg2 = arg2;
request.arg3 = arg3;
request.arg4 = arg4;
if (which_rpc == RPC_QUIT) {
request.rpcType = RPC_TYPE_ASYNC_NO_REFRESH;
} else {
request.rpcType = RPC_TYPE_WAIT_FOR_COMPLETION;
}
request.RPCExecutionTime = NULL;
retval = m_pDoc->RequestRPC(request, hasPriority);
return retval;
}
RPCThread::RPCThread(CMainDocument *pDoc,
BOINC_Mutex* pRPC_Thread_Mutex,
BOINC_Condition* pRPC_Thread_Condition,
BOINC_Mutex* pRPC_Request_Mutex,
BOINC_Condition* pRPC_Request_Condition)
: wxThread() {
m_pDoc = pDoc;
m_pRPC_Thread_Mutex = pRPC_Thread_Mutex;
m_pRPC_Thread_Condition = pRPC_Thread_Condition;
m_pRPC_Request_Mutex = pRPC_Request_Mutex;
m_pRPC_Request_Condition = pRPC_Request_Condition;
}
void *RPCThread::Entry() {
int retval = 0;
CRPCFinishedEvent RPC_done_event( wxEVT_RPC_FINISHED );
ASYNC_RPC_REQUEST *current_request;
double startTime = 0;
wxMutexError mutexErr = wxMUTEX_NO_ERROR;
wxCondError condErr = wxCOND_NO_ERROR;
#ifdef HAVE__CONFIGTHREADLOCALE
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
setlocale(LC_ALL, "C");
#elif defined(HAVE_USELOCALE)
locale_t RPC_Thread_Locale = newlocale(LC_ALL_MASK, "C", (locale_t) 0);
uselocale(RPC_Thread_Locale);
#endif
m_pRPC_Thread_Mutex->Lock();
m_pDoc->m_bRPCThreadIsReady = true;
while(true) {
// Wait for main thread to wake us
// This does the following:
// (1) Unlocks the Mutex and puts the RPC thread to sleep as an atomic operation.
// (2) On Signal from main thread: locks Mutex again and wakes the RPC thread.
condErr = m_pRPC_Thread_Condition->Wait();
wxASSERT(condErr == wxCOND_NO_ERROR);
if (m_pDoc->m_bShutDownRPCThread) {
#ifdef HAVE_USELOCALE
uselocale(LC_GLOBAL_LOCALE);
freelocale(RPC_Thread_Locale);
#endif
m_pRPC_Thread_Mutex->Unlock(); // Just for safety - not really needed
// Tell CMainDocument that thread has gracefully ended
// We do this here because OnExit() is not called on Windows
m_pDoc->m_RPCThread = NULL;
return 0;
}
current_request = m_pDoc->GetCurrentRPCRequest();
if (!current_request->isActive) continue; // Should never happen
if (current_request->RPCExecutionTime) {
startTime = dtime();
}
retval = ProcessRPCRequest();
if (current_request->RPCExecutionTime) {
*(current_request->RPCExecutionTime) = dtime() - startTime;
}
current_request->retval = retval;
mutexErr = m_pRPC_Request_Mutex->Lock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
current_request->isActive = false;
wxPostEvent( wxTheApp, RPC_done_event );
// Signal() is ignored / discarded unless the main thread is
// currently blocked by m_pRPC_Request_Condition->Wait[Timeout]()
m_pRPC_Request_Condition->Signal();
mutexErr = m_pRPC_Request_Mutex->Unlock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
}
return NULL;
}
int RPCThread::ProcessRPCRequest() {
int retval = 0;
ASYNC_RPC_REQUEST *current_request = m_pDoc->GetCurrentRPCRequest();
switch (current_request->which_rpc) {
// RPC_SELECTORS with no arguments
case RPC_RUN_BENCHMARKS:
case RPC_QUIT:
case RPC_NETWORK_AVAILABLE:
case RPC_PROJECT_ATTACH_FROM_FILE:
case RPC_READ_GLOBAL_PREFS_OVERRIDE:
case RPC_READ_CC_CONFIG:
break;
default:
// All others must have at least one argument
if (current_request->arg1 == NULL) {
wxASSERT(false);
return -1;
}
break;
}
switch (current_request->which_rpc) {
case RPC_AUTHORIZE:
retval = (m_pDoc->rpcClient).authorize((const char*)(current_request->arg1));
break;
case RPC_EXCHANGE_VERSIONS:
retval = (m_pDoc->rpcClient).exchange_versions(*(VERSION_INFO*)(current_request->arg1));
break;
case RPC_GET_STATE:
retval = (m_pDoc->rpcClient).get_state(*(CC_STATE*)(current_request->arg1));
break;
case RPC_GET_RESULTS:
retval = (m_pDoc->rpcClient).get_results(*(RESULTS*)(current_request->arg1), *(bool*)(current_request->arg2));
break;
case RPC_GET_FILE_TRANSFERS:
retval = (m_pDoc->rpcClient).get_file_transfers(*(FILE_TRANSFERS*)(current_request->arg1));
break;
case RPC_GET_SIMPLE_GUI_INFO1:
retval = (m_pDoc->rpcClient).get_simple_gui_info(*(SIMPLE_GUI_INFO*)(current_request->arg1));
break;
case RPC_GET_SIMPLE_GUI_INFO2:
// RPC_GET_SIMPLE_GUI_INFO2 is equivalent to doing both
// RPC_GET_PROJECT_STATUS1 and RPC_GET_RESULTS
retval = (m_pDoc->rpcClient).get_results(*(RESULTS*)(current_request->arg3), *(bool*)(current_request->arg4));
if (!retval) {
retval = (m_pDoc->rpcClient).get_project_status(*(PROJECTS*)(current_request->arg1));
}
break;
case RPC_GET_PROJECT_STATUS1:
retval = (m_pDoc->rpcClient).get_project_status(*(PROJECTS*)(current_request->arg1));
break;
case RPC_GET_PROJECT_STATUS2:
retval = (m_pDoc->rpcClient).get_project_status(*(PROJECTS*)(current_request->arg1));
break;
case RPC_GET_ALL_PROJECTS_LIST:
retval = (m_pDoc->rpcClient).get_all_projects_list(*(ALL_PROJECTS_LIST*)(current_request->arg1));
break;
case RPC_GET_DISK_USAGE:
retval = (m_pDoc->rpcClient).get_disk_usage(*(DISK_USAGE*)(current_request->arg1));
break;
case RPC_PROJECT_OP:
retval = (m_pDoc->rpcClient).project_op(
*(PROJECT*)(current_request->arg1),
(const char*)(current_request->arg2)
);
break;
case RPC_SET_RUN_MODE:
retval = (m_pDoc->rpcClient).set_run_mode(
*(int*)(current_request->arg1),
*(double*)(current_request->arg2)
);
break;
case RPC_SET_GPU_MODE:
retval = (m_pDoc->rpcClient).set_gpu_mode(
*(int*)(current_request->arg1),
*(double*)(current_request->arg2)
);
break;
case RPC_SET_NETWORK_MODE:
retval = (m_pDoc->rpcClient).set_network_mode(
*(int*)(current_request->arg1),
*(double*)(current_request->arg2)
);
break;
case RPC_GET_SCREENSAVER_TASKS:
retval = (m_pDoc->rpcClient).get_screensaver_tasks(
*(int*)(current_request->arg1),
*(RESULTS*)(current_request->arg2)
);
break;
case RPC_RUN_BENCHMARKS:
retval = (m_pDoc->rpcClient).run_benchmarks();
break;
case RPC_SET_PROXY_SETTINGS:
retval = (m_pDoc->rpcClient).set_proxy_settings(*(GR_PROXY_INFO*)(current_request->arg1));
break;
case RPC_GET_PROXY_SETTINGS:
retval = (m_pDoc->rpcClient).get_proxy_settings(*(GR_PROXY_INFO*)(current_request->arg1));
break;
case RPC_GET_NOTICES:
retval = (m_pDoc->rpcClient).get_notices(
*(int*)(current_request->arg1),
*(NOTICES*)(current_request->arg2)
);
break;
case RPC_GET_MESSAGES:
retval = (m_pDoc->rpcClient).get_messages(
*(int*)(current_request->arg1),
*(MESSAGES*)(current_request->arg2),
*(bool*)(current_request->arg3)
);
break;
case RPC_FILE_TRANSFER_OP:
retval = (m_pDoc->rpcClient).file_transfer_op(
*(FILE_TRANSFER*)(current_request->arg1),
(const char*)(current_request->arg2)
);
break;
case RPC_RESULT_OP:
retval = (m_pDoc->rpcClient).result_op(
*(RESULT*)(current_request->arg1),
(const char*)(current_request->arg2)
);
break;
case RPC_GET_HOST_INFO:
retval = (m_pDoc->rpcClient).get_host_info(*(HOST_INFO*)(current_request->arg1));
break;
case RPC_QUIT:
retval = (m_pDoc->rpcClient).quit();
break;
case RPC_ACCT_MGR_INFO:
retval = (m_pDoc->rpcClient).acct_mgr_info(*(ACCT_MGR_INFO*)(current_request->arg1));
break;
case RPC_GET_STATISTICS:
retval = (m_pDoc->rpcClient).get_statistics(*(PROJECTS*)(current_request->arg1));
break;
case RPC_NETWORK_AVAILABLE:
retval = (m_pDoc->rpcClient).network_available();
break;
case RPC_GET_PROJECT_INIT_STATUS:
retval = (m_pDoc->rpcClient).get_project_init_status(*(PROJECT_INIT_STATUS*)(current_request->arg1));
break;
case RPC_GET_PROJECT_CONFIG:
retval = (m_pDoc->rpcClient).get_project_config(*(std::string*)(current_request->arg1));
break;
case RPC_GET_PROJECT_CONFIG_POLL:
retval = (m_pDoc->rpcClient).get_project_config_poll(*(PROJECT_CONFIG*)(current_request->arg1));
break;
case RPC_LOOKUP_ACCOUNT:
retval = (m_pDoc->rpcClient).lookup_account(*(ACCOUNT_IN*)(current_request->arg1));
break;
case RPC_LOOKUP_ACCOUNT_POLL:
retval = (m_pDoc->rpcClient).lookup_account_poll(*(ACCOUNT_OUT*)(current_request->arg1));
break;
case RPC_CREATE_ACCOUNT:
retval = (m_pDoc->rpcClient).create_account(*(ACCOUNT_IN*)(current_request->arg1));
break;
case RPC_CREATE_ACCOUNT_POLL:
retval = (m_pDoc->rpcClient).create_account_poll(*(ACCOUNT_OUT*)(current_request->arg1));
break;
case RPC_PROJECT_ATTACH:
retval = (m_pDoc->rpcClient).project_attach(
(const char*)(current_request->arg1),
(const char*)(current_request->arg2),
(const char*)(current_request->arg3)
);
break;
case RPC_PROJECT_ATTACH_FROM_FILE:
retval = (m_pDoc->rpcClient).project_attach_from_file();
break;
case RPC_PROJECT_ATTACH_POLL:
retval = (m_pDoc->rpcClient).project_attach_poll(*(PROJECT_ATTACH_REPLY*)(current_request->arg1));
break;
case RPC_ACCT_MGR_RPC:
retval = (m_pDoc->rpcClient).acct_mgr_rpc(
(const char*)(current_request->arg1),
(const char*)(current_request->arg2),
(const char*)(current_request->arg3),
(bool)(current_request->arg4 != NULL)
);
break;
case RPC_ACCT_MGR_RPC_POLL:
retval = (m_pDoc->rpcClient).acct_mgr_rpc_poll(*(ACCT_MGR_RPC_REPLY*)(current_request->arg1));
break;
case RPC_GET_NEWER_VERSION:
retval = (m_pDoc->rpcClient).get_newer_version(
*(std::string*)(current_request->arg1),
*(std::string*)(current_request->arg2)
);
break;
case RPC_READ_GLOBAL_PREFS_OVERRIDE:
retval = (m_pDoc->rpcClient).read_global_prefs_override();
break;
case RPC_READ_CC_CONFIG:
retval = (m_pDoc->rpcClient).read_cc_config();
break;
case RPC_GET_CC_STATUS:
retval = (m_pDoc->rpcClient).get_cc_status(*(CC_STATUS*)(current_request->arg1));
break;
case RPC_GET_GLOBAL_PREFS_FILE:
retval = (m_pDoc->rpcClient).get_global_prefs_file(*(std::string*)(current_request->arg1));
break;
case RPC_GET_GLOBAL_PREFS_WORKING:
retval = (m_pDoc->rpcClient).get_global_prefs_working(*(std::string*)(current_request->arg1));
break;
case RPC_GET_GLOBAL_PREFS_WORKING_STRUCT:
retval = (m_pDoc->rpcClient).get_global_prefs_working_struct(
*(GLOBAL_PREFS*)(current_request->arg1),
*(GLOBAL_PREFS_MASK*)(current_request->arg2)
);
break;
case RPC_GET_GLOBAL_PREFS_OVERRIDE:
retval = (m_pDoc->rpcClient).get_global_prefs_override(*(std::string*)(current_request->arg1));
break;
case RPC_SET_GLOBAL_PREFS_OVERRIDE:
retval = (m_pDoc->rpcClient).set_global_prefs_override(*(std::string*)(current_request->arg1));
break;
case RPC_GET_GLOBAL_PREFS_OVERRIDE_STRUCT:
retval = (m_pDoc->rpcClient).get_global_prefs_override_struct(
*(GLOBAL_PREFS*)(current_request->arg1),
*(GLOBAL_PREFS_MASK*)(current_request->arg2)
);
break;
case RPC_SET_GLOBAL_PREFS_OVERRIDE_STRUCT:
retval = (m_pDoc->rpcClient).set_global_prefs_override_struct(
*(GLOBAL_PREFS*)(current_request->arg1),
*(GLOBAL_PREFS_MASK*)(current_request->arg2)
);
break;
case RPC_GET_CC_CONFIG:
retval = (m_pDoc->rpcClient).get_cc_config(
*(CC_CONFIG*)(current_request->arg1),
*(LOG_FLAGS*)(current_request->arg2)
);
break;
case RPC_SET_CC_CONFIG:
retval = (m_pDoc->rpcClient).set_cc_config(
*(CC_CONFIG*)(current_request->arg1),
*(LOG_FLAGS*)(current_request->arg2)
);
break;
case RPC_SET_LANGUAGE:
retval = (m_pDoc->rpcClient).set_language(
(const char*)(current_request->arg1)
);
break;
default:
break;
}
return retval;
}
// TODO: combine RPC requests for different buffers, then just copy the buffer.
int CMainDocument::RequestRPC(ASYNC_RPC_REQUEST& request, bool hasPriority) {
std::vector<ASYNC_RPC_REQUEST>::iterator iter;
int retval = 0;
int response = wxID_OK;
wxMutexError mutexErr = wxMUTEX_NO_ERROR;
long timeToDelay, delayTimeRemaining, timeToSleep;
bool shown = false;
if (!m_RPCThread) return -1;
if ( (request.rpcType < RPC_TYPE_WAIT_FOR_COMPLETION) ||
(request.rpcType >= NUM_RPC_TYPES) ) {
wxASSERT(false);
return -1;
}
// If we are quitting, cancel any pending RPCs
if (request.which_rpc == RPC_QUIT) {
if (current_rpc_request.isActive) {
RPC_requests.erase(RPC_requests.begin()+1, RPC_requests.end());
} else {
RPC_requests.clear();
}
}
// Check if a duplicate request is already on the queue
for (iter=RPC_requests.begin(); iter!=RPC_requests.end(); ++iter) {
if (iter->isSameAs(request)) {
return 0;
}
}
if ((request.rpcType == RPC_TYPE_WAIT_FOR_COMPLETION) && (request.resultPtr == NULL)) {
request.resultPtr = &retval;
}
if (hasPriority) {
// We may want to set hasPriority for some user-initiated events.
// Since the user is waiting, insert this at head of request queue.
// As of 8/14/08, hasPriority is never set true, so hasn't been tested.
iter = RPC_requests.insert(RPC_requests.begin(), request);
} else {
RPC_requests.push_back(request);
}
// Start this RPC if no other RPC is already in progress.
if (RPC_requests.size() == 1) {
// Wait for thread to unlock mutex with m_pRPC_Thread_Condition->Wait()
mutexErr = m_pRPC_Thread_Mutex->Lock(); // Blocks until thread unlocks the mutex
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
// Make sure activation is an atomic operation
request.isActive = false;
current_rpc_request = request;
current_rpc_request.isActive = true;
m_pRPC_Thread_Condition->Signal(); // Unblock the thread
// m_pRPC_Thread_Condition->Wait() will Lock() the mutex upon receiving Signal(),
// causing it to block again if we still have our lock on the mutex.
mutexErr = m_pRPC_Thread_Mutex->Unlock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
}
// If this is a user-initiated event wait for completion but show
// a dialog allowing the user to cancel.
if (request.rpcType == RPC_TYPE_WAIT_FOR_COMPLETION) {
// TODO: proper handling if a second user request is received while first is pending ??
if (m_bWaitingForRPC) {
wxLogMessage(wxT("Second user RPC request while another was pending"));
wxASSERT(false);
return -1;
}
// Don't show dialog if RPC completes before RPC_WAIT_DLG_DELAY
// or while BOINC is minimized or during auto-attach to project
CBOINCBaseFrame* pFrame = wxGetApp().GetFrame();
wxStopWatch Dlgdelay = wxStopWatch();
m_RPCWaitDlg = new AsyncRPCDlg();
m_bWaitingForRPC = true;
if (m_bAutoAttaching) {
// client may auto-attach only when first launched
// so don't keep checking once it is false
m_bAutoAttaching = autoattach_in_progress();
}
// Allow RPC_WAIT_DLG_DELAY seconds for Demand RPC to complete before
// displaying "Please Wait" dialog, but keep checking for completion.
if (m_bAutoAttaching) {
timeToDelay = RPC_WAIT_DLG_DELAY_DURING_AUTOATTACH;
} else {
timeToDelay = RPC_WAIT_DLG_DELAY;
}
delayTimeRemaining = timeToDelay;
while (true) {
if (delayTimeRemaining >= 0) { // Prevent overflow if minimized for a very long time
delayTimeRemaining = timeToDelay - Dlgdelay.Time();
}
if (pFrame) {
shown = pFrame->IsShown();
} else {
shown = false;
}
if (shown) {
if (delayTimeRemaining <= 0) break; // Display the Please Wait dialog
timeToSleep = delayTimeRemaining;
} else {
// Don't show dialog while Manager is minimized, but do
// process events so user can maximize the manager.
//
// NOTE: CBOINCGUIApp::FilterEvent() discards those events
// which might cause posting of more RPC requests while
// we are in this loop, to prevent undesirable recursion.
// Since the manager is minimized, we don't have to worry about
// discarding crucial drawing or command events.
// The filter does allow the the Open Manager menu item from
// the system tray icon and wxEVT_RPC_FINISHED event.
//
timeToSleep = DELAY_WHEN_MINIMIZED; // Allow user to maximize Manager
wxSafeYield(NULL, true);
}
// OnRPCComplete() clears m_bWaitingForRPC and deletes m_RPCWaitDlg if RPC completed
if (! m_bWaitingForRPC) {
return retval;
}
mutexErr = m_pRPC_Request_Mutex->Lock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
// Simulate handling of CRPCFinishedEvent but don't allow any other
// events (so no user activity) to prevent undesirable recursion.
// Since we don't need to filter and discard events, they remain on
// the queue until it is safe to process them.
// Allow RPC thread to run while we wait for it.
if (!current_rpc_request.isActive) {
mutexErr = m_pRPC_Request_Mutex->Unlock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
HandleCompletedRPC();
continue;
}
// Wait for RPC thread to wake us
// This does the following:
// (1) Unlocks the Mutex and puts the main thread to sleep as an atomic operation.
// (2) On Signal from RPC thread: locks Mutex again and wakes the main thread.
m_pRPC_Request_Condition->WaitTimeout(timeToSleep);
mutexErr = m_pRPC_Request_Mutex->Unlock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
}
// Demand RPC has taken longer than RPC_WAIT_DLG_DELAY seconds and
// Manager is not minimized, so display the "Please Wait" dialog
// with a Cancel button. If the RPC does complete while the dialog
// is up, HandleCompletedRPC() will call EndModal with wxID_OK.
//
// NOTE: the Modal dialog permits processing of all events, but
// CBOINCGUIApp::FilterEvent() blocks those events which might cause
// posting of more RPC requests while in this dialog, to prevent
// undesirable recursion.
//
if (m_RPCWaitDlg) {
response = m_RPCWaitDlg->ShowModal();
// Remember time the dialog was closed for use by RunPeriodicRPCs()
m_dtLasAsyncRPCDlgTime = wxDateTime::Now();
if (response != wxID_OK) {
// TODO: If user presses Cancel in Please Wait dialog but request
// has not yet been started, should we just remove it from queue?
// If we make that change, should we also add a separate menu item
// to reset the RPC connection (or does one already exist)?
retval = -1;
// If the RPC continues to get data after we return to
// our caller, it may try to write into a buffer or struct
// which the caller has already deleted. To prevent this,
// we close the socket (disconnect) and kill the RPC thread.
// This is ugly but necessary. We must then reconnect and
// start a new RPC thread.
if (current_rpc_request.isActive) {
current_rpc_request.isActive = false;
rpcClient.close();
RPC_requests.clear();
current_rpc_request.clear();
m_bNeedRefresh = false;
m_bNeedTaskBarRefresh = false;
// We will be reconnected to the same client (if possible) by
// CBOINCDialUpManager::OnPoll() and CNetworkConnection::Poll().
m_pNetworkConnection->SetStateDisconnected();
}
if (response == wxID_EXIT) {
pFrame = wxGetApp().GetFrame();
wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, wxID_EXIT);
s_bSkipExitConfirmation = true;
pFrame->GetEventHandler()->AddPendingEvent(evt);
}
}
if (m_RPCWaitDlg) {
m_RPCWaitDlg->Destroy();
}
m_RPCWaitDlg = NULL;
m_bWaitingForRPC = false;
}
}
return retval;
}
void CMainDocument::KillRPCThread() {
wxMutexError mutexErr = wxMUTEX_NO_ERROR;
int i;
if (!m_RPCThread) {
return;
}
m_bNeedRefresh = false;
m_bNeedTaskBarRefresh = false;
rpcClient.close(); // Abort any async RPC in progress (in case hung)
// On some platforms, Delete() takes effect only when thread calls TestDestroy()
// Wait for thread to unlock mutex with m_pRPC_Thread_Condition->Wait()
mutexErr = m_pRPC_Thread_Mutex->Lock(); // Blocks until thread unlocks the mutex
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
m_bShutDownRPCThread = true;
m_pRPC_Thread_Condition->Signal(); // Unblock the thread
mutexErr = m_pRPC_Thread_Mutex->Unlock(); // Release the mutex so thread can lock it
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
RPC_requests.clear();
current_rpc_request.clear();
// Wait up to RPC_KILL_DELAY milliseconds for thread to exit on its own
for (i=0; i< RPC_KILL_DELAY; ++i) {
boinc_sleep(.001); // Defer to RPC thread for 1 millisecond
if (!m_RPCThread) {
return; // RPC thread sets m_RPCThread to NULL when it exits
}
}
// Thread failed to exit, so forcefully kill it
m_RPCThread->Kill();
}
void CMainDocument::OnRPCComplete(CRPCFinishedEvent&) {
HandleCompletedRPC();
}
void CMainDocument::HandleCompletedRPC() {
int retval = 0;
wxMutexError mutexErr = wxMUTEX_NO_ERROR;
int i, n, requestIndex = -1;
bool stillWaitingForPendingRequests = false;
if (!m_RPCThread) return;
if (current_rpc_request.isActive) return;
// We can get here either via a CRPCFinishedEvent event posted
// by the RPC thread or by a call from RequestRPC. If we were
// called from RequestRPC, the CRPCFinishedEvent will still be
// on the event queue, so we get called twice. Check for this here.
if (current_rpc_request.which_rpc == 0) return; // already handled by a call from RequestRPC
// Find our completed request in the queue
n = (int) RPC_requests.size();
for (i=0; i<n; ++i) {
if (RPC_requests[i].isSameAs(current_rpc_request)) {
requestIndex = i;
} else {
if (RPC_requests[i].rpcType == RPC_TYPE_WAIT_FOR_COMPLETION) {
stillWaitingForPendingRequests = true;
}
}
}
if (! stillWaitingForPendingRequests) {
if (m_RPCWaitDlg) {
if (m_RPCWaitDlg->IsShown()) {
m_RPCWaitDlg->EndModal(wxID_OK);
}
m_RPCWaitDlg->Destroy();
m_RPCWaitDlg = NULL;
}
m_bWaitingForRPC = false;
}
if (requestIndex >= 0) {
// Remove completed request from the queue
RPC_requests.erase(RPC_requests.begin()+requestIndex);
}
retval = current_rpc_request.retval;
if (current_rpc_request.completionTime) {
*(current_rpc_request.completionTime) = wxDateTime::Now();
}
if (current_rpc_request.resultPtr) {
*(current_rpc_request.resultPtr) = retval;
}
// Post-processing
if (! retval) {
if (current_rpc_request.rpcType == RPC_TYPE_ASYNC_WITH_REFRESH_AFTER) {
if (!retval) {
m_bNeedRefresh = true;
}
}
if (current_rpc_request.rpcType == RPC_TYPE_ASYNC_WITH_UPDATE_TASKBAR_ICON_AFTER) {
if (!retval) {
m_bNeedTaskBarRefresh = true;
}
}
switch (current_rpc_request.which_rpc) {
case RPC_GET_STATE:
if (current_rpc_request.exchangeBuf && !retval) {
CC_STATE* arg1 = (CC_STATE*)current_rpc_request.arg1;
CC_STATE* exchangeBuf = (CC_STATE*)current_rpc_request.exchangeBuf;
arg1->projects.swap(exchangeBuf->projects);
arg1->apps.swap(exchangeBuf->apps);
arg1->app_versions.swap(exchangeBuf->app_versions);
arg1->wus.swap(exchangeBuf->wus);
arg1->results.swap(exchangeBuf->results);
exchangeBuf->global_prefs = arg1->global_prefs;
exchangeBuf->version_info = arg1->version_info;
exchangeBuf->executing_as_daemon = arg1->executing_as_daemon;
exchangeBuf->host_info = arg1->host_info;
exchangeBuf->time_stats = arg1->time_stats;
exchangeBuf->have_nvidia = arg1->have_nvidia;
exchangeBuf->have_ati = arg1->have_ati;
}
break;
case RPC_GET_RESULTS:
if (current_rpc_request.exchangeBuf && !retval) {
RESULTS* arg1 = (RESULTS*)current_rpc_request.arg1;
RESULTS* exchangeBuf = (RESULTS*)current_rpc_request.exchangeBuf;
arg1->results.swap(exchangeBuf->results);
}
break;
case RPC_GET_FILE_TRANSFERS:
if (current_rpc_request.exchangeBuf && !retval) {
FILE_TRANSFERS* arg1 = (FILE_TRANSFERS*)current_rpc_request.arg1;
FILE_TRANSFERS* exchangeBuf = (FILE_TRANSFERS*)current_rpc_request.exchangeBuf;
arg1->file_transfers.swap(exchangeBuf->file_transfers);
}
break;
case RPC_GET_SIMPLE_GUI_INFO2:
if (!retval) {
retval = CopyProjectsToStateBuffer(*(PROJECTS*)(current_rpc_request.arg1), *(CC_STATE*)(current_rpc_request.arg2));
}
if (current_rpc_request.exchangeBuf && !retval) {
RESULTS* arg3 = (RESULTS*)current_rpc_request.arg3;
RESULTS* exchangeBuf = (RESULTS*)current_rpc_request.exchangeBuf;
arg3->results.swap(exchangeBuf->results);
}
break;
case RPC_GET_PROJECT_STATUS1:
if (!retval) {
retval = CopyProjectsToStateBuffer(*(PROJECTS*)(current_rpc_request.arg1), *(CC_STATE*)(current_rpc_request.arg2));
}
break;
case RPC_GET_ALL_PROJECTS_LIST:
if (current_rpc_request.exchangeBuf && !retval) {
ALL_PROJECTS_LIST* arg1 = (ALL_PROJECTS_LIST*)current_rpc_request.arg1;
ALL_PROJECTS_LIST* exchangeBuf = (ALL_PROJECTS_LIST*)current_rpc_request.exchangeBuf;
arg1->projects.swap(exchangeBuf->projects);
}
break;
case RPC_GET_DISK_USAGE:
if (current_rpc_request.exchangeBuf && !retval) {
DISK_USAGE* arg1 = (DISK_USAGE*)current_rpc_request.arg1;
DISK_USAGE* exchangeBuf = (DISK_USAGE*)current_rpc_request.exchangeBuf;
arg1->projects.swap(exchangeBuf->projects);
exchangeBuf->d_total = arg1->d_total;
exchangeBuf->d_free = arg1->d_free;
exchangeBuf->d_boinc = arg1->d_boinc;
exchangeBuf->d_allowed = arg1->d_allowed;
}
break;
case RPC_GET_NOTICES:
if (current_rpc_request.exchangeBuf && !retval) {
NOTICES* arg2 = (NOTICES*)current_rpc_request.arg2;
NOTICES* exchangeBuf = (NOTICES*)current_rpc_request.exchangeBuf;
arg2->notices.swap(exchangeBuf->notices);
}
if (!retval) {
CachedNoticeUpdate(); // Call this only when notice buffer is stable
}
m_bWaitingForGetNoticesRPC = false;
break;
case RPC_GET_MESSAGES:
if (current_rpc_request.exchangeBuf && !retval) {
MESSAGES* arg2 = (MESSAGES*)current_rpc_request.arg2;
MESSAGES* exchangeBuf = (MESSAGES*)current_rpc_request.exchangeBuf;
arg2->messages.swap(exchangeBuf->messages);
}
if (!retval) {
CachedMessageUpdate(); // Call this only when message buffer is stable
}
break;
case RPC_GET_HOST_INFO:
if (current_rpc_request.exchangeBuf && !retval) {
HOST_INFO* arg1 = (HOST_INFO*)current_rpc_request.arg1;
HOST_INFO* exchangeBuf = (HOST_INFO*)current_rpc_request.exchangeBuf;
*exchangeBuf = *arg1;
}
break;
case RPC_GET_STATISTICS:
if (current_rpc_request.exchangeBuf && !retval) {
PROJECTS* arg1 = (PROJECTS*)current_rpc_request.arg1;
PROJECTS* exchangeBuf = (PROJECTS*)current_rpc_request.exchangeBuf;
arg1->projects.swap(exchangeBuf->projects);
}
break;
case RPC_GET_CC_STATUS:
if (current_rpc_request.exchangeBuf && !retval) {
CC_STATUS* arg1 = (CC_STATUS*)current_rpc_request.arg1;
CC_STATUS* exchangeBuf = (CC_STATUS*)current_rpc_request.exchangeBuf;
*exchangeBuf = *arg1;
}
break;
case RPC_ACCT_MGR_INFO:
if (current_rpc_request.exchangeBuf && !retval) {
ACCT_MGR_INFO* arg1 = (ACCT_MGR_INFO*)current_rpc_request.arg1;
ACCT_MGR_INFO* exchangeBuf = (ACCT_MGR_INFO*)current_rpc_request.exchangeBuf;
*exchangeBuf = *arg1;
}
break;
default:
// We don't support double buffering for other RPC calls
wxASSERT(current_rpc_request.exchangeBuf == NULL);
break;
}
}
if (current_rpc_request.resultPtr) {
// In case post-processing changed retval
*(current_rpc_request.resultPtr) = retval;
}
// We must call ProcessEvent() rather than AddPendingEvent() here to
// guarantee integrity of data when other events are handled (such as
// Abort, Suspend/Resume, Show Graphics, Update, Detach, Reset, No
// New Work, etc.) Otherwise, if one of those events is pending it
// might be processed first, and the data in the selected rows may not
// match the data which the user selected if any rows were added or
// deleted due to the RPC.
// The refresh event called here adjusts the selections to fix any
// such mismatch before other pending events are processed.
//
// However, the refresh code may itself request a Demand RPC, which
// would cause undesirable recursion if we are already waiting for
// another Demand RPC to complete. In that case, we defer the refresh
// until all pending Demand RPCs have been done.
//
if (m_bNeedRefresh && !m_bWaitingForRPC) {
m_bNeedRefresh = false;
// We must get the frame immediately before using it,
// since it may have been changed by SetActiveGUI().
CBOINCBaseFrame* pFrame = wxGetApp().GetFrame();
if (pFrame) {
CFrameEvent event(wxEVT_FRAME_REFRESHVIEW, pFrame);
pFrame->GetEventHandler()->ProcessEvent(event);
}
}
if (m_bNeedTaskBarRefresh && !m_bWaitingForRPC) {
m_bNeedTaskBarRefresh = false;
CTaskBarIcon* pTaskbar = wxGetApp().GetTaskBarIcon();
if (pTaskbar) {
CTaskbarEvent event(wxEVT_TASKBAR_REFRESH, pTaskbar);
pTaskbar->ProcessEvent(event);
}
}
if (current_rpc_request.rpcType == RPC_TYPE_ASYNC_WITH_REFRESH_EVENT_LOG_AFTER) {
CDlgEventLog* eventLog = wxGetApp().GetEventLog();
if (eventLog) {
eventLog->OnRefresh();
}
}
current_rpc_request.clear();
// Start the next RPC request.
// We can't start this until finished processing the previous RPC's
// event because the two requests may write into the same buffer.
if (RPC_requests.size() > 0) {
// Wait for thread to unlock mutex with m_pRPC_Thread_Condition->Wait()
mutexErr = m_pRPC_Thread_Mutex->Lock(); // Blocks until thread unlocks the mutex
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
// Make sure activation is an atomic operation
RPC_requests[0].isActive = false;
current_rpc_request = RPC_requests[0];
current_rpc_request.isActive = true;
m_pRPC_Thread_Condition->Signal(); // Unblock the thread
// m_pRPC_Thread_Condition->Wait() will Lock() the mutex upon receiving Signal(),
// causing it to block again if we still have our lock on the mutex.
mutexErr = m_pRPC_Thread_Mutex->Unlock();
wxASSERT(mutexErr == wxMUTEX_NO_ERROR);
}
}
int CMainDocument::CopyProjectsToStateBuffer(PROJECTS& p, CC_STATE& ccstate) {
int retval = 0;
unsigned int i;
PROJECT* state_project = NULL;
// flag for delete
for (i=0; i<ccstate.projects.size(); i++) {
state_project = ccstate.projects[i];
state_project->flag_for_delete = true;
}
for (i=0; i<p.projects.size(); i++) {
state_project = ccstate.lookup_project(p.projects[i]->master_url);
if (state_project && (!strcmp(p.projects[i]->master_url, state_project->master_url))) {
// Because the CC_STATE contains several pointers to each element of the
// CC_STATE::projects vector, we must update these elements in place.
*state_project = *(p.projects[i]);
state_project->flag_for_delete = false;
} else {
retval = ERR_NOT_FOUND;
}
continue;
}
// Anything need to be deleted?
if (!retval) {
for (i=0; i<ccstate.projects.size(); i++) {
state_project = ccstate.projects[i];
if (state_project->flag_for_delete) {
retval = ERR_FILE_MISSING;
}
}
}
return retval;
}
BEGIN_EVENT_TABLE(AsyncRPCDlg, wxDialog)
EVT_BUTTON(wxID_EXIT, AsyncRPCDlg::OnExit)
END_EVENT_TABLE()
IMPLEMENT_CLASS(AsyncRPCDlg, wxDialog)
AsyncRPCDlg::AsyncRPCDlg() : wxDialog( NULL, wxID_ANY, wxT(""), wxDefaultPosition ) {
CSkinAdvanced* pSkinAdvanced = wxGetApp().GetSkinManager()->GetAdvanced();
wxString exit_label;
wxASSERT(pSkinAdvanced);
wxString message = wxString(_("Communicating with BOINC client. Please wait ..."));
#ifdef __WXMAC__
exit_label.Printf(_("&Quit %s"), pSkinAdvanced->GetApplicationName().c_str());
#else
exit_label.Printf(_("E&xit %s"), pSkinAdvanced->GetApplicationName().c_str());
#endif
wxString strCaption;
strCaption.Printf(_("%s - Communication"), pSkinAdvanced->GetApplicationName().c_str());
SetTitle(strCaption.c_str());
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
icon_text->Add( CreateTextSizer( message ), 0, wxALIGN_CENTER | wxLEFT, 10 );
topsizer->Add( icon_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
wxStdDialogButtonSizer *sizerBtn = CreateStdDialogButtonSizer(0);
wxButton* exitbutton = new wxButton;
exitbutton->Create( this, wxID_EXIT, exit_label, wxDefaultPosition, wxDefaultSize, 0 );
sizerBtn->Add(exitbutton, 0, wxLEFT|wxRIGHT|wxALL, 5);
wxButton* cancelbutton = new wxButton;
cancelbutton->Create( this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
sizerBtn->Add(cancelbutton, 0, wxLEFT|wxRIGHT|wxALL, 5);
if ( sizerBtn )
topsizer->Add(sizerBtn, 0, wxEXPAND | wxALL, 10 );
SetAutoLayout( true );
SetSizer( topsizer );
topsizer->SetSizeHints( this );
topsizer->Fit( this );
wxSize size( GetSize() );
if (size.x < size.y*3/2)
{
size.x = size.y*3/2;
SetSize( size );
}
Centre( wxBOTH | wxCENTER_FRAME);
}
void AsyncRPCDlg::OnExit(wxCommandEvent& WXUNUSED(eventUnused)) {
EndModal(wxID_EXIT);
}
#if 0
/// For testing: triggered by Advanced / Options menu item.
void CMainDocument::TestAsyncRPC() {
ALL_PROJECTS_LIST pl;
ASYNC_RPC_REQUEST request;
wxDateTime completionTime = wxDateTime((time_t)0);
int req_retval = 0, rpc_result = 0;
completionTime.ResetTime();
request.which_rpc = RPC_GET_ALL_PROJECTS_LIST;
request.arg1 = &pl;
request.exchangeBuf = NULL;
request.arg2 = NULL;
request.arg3 = NULL;
request.arg4 = NULL;
request.rpcType = RPC_TYPE_WAIT_FOR_COMPLETION;
request.completionTime = &completionTime;
// request.result = NULL;
request.resultPtr = &rpc_result; // For testing async RPCs
request.isActive = false;
//retval = rpcClient.get_all_projects_list(pl);
req_retval = RequestRPC(request, true);
wxString s = FormatTime();
wxLogMessage(wxT("Completion time = %s"), s.c_str());
wxLogMessage(wxT("RequestRPC returned %d\n"), req_retval);
::wxSafeYield(NULL, true); // Allow processing of RPC_FINISHED event
wxLogMessage(wxT("rpcClient.get_all_projects_list returned %d\n"), rpc_result);
}
#endif
|