2004-01-30 22:19:19 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2004-02-28 19:11:40 +00:00
|
|
|
#ifdef _WIN32
|
2004-03-04 11:41:43 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <stdio.h>
|
2004-01-21 07:07:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2004-01-21 22:54:35 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string.h>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2004-01-21 07:07:16 +00:00
|
|
|
|
|
|
|
#include "parse.h"
|
|
|
|
#include "client_state.h"
|
|
|
|
|
|
|
|
GUI_RPC_CONN::GUI_RPC_CONN(int s) {
|
|
|
|
sock = s;
|
2004-02-28 19:11:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
fout = _fdopen(_dup(sock), "w");
|
|
|
|
#else
|
2004-01-21 07:07:16 +00:00
|
|
|
fout = fdopen(dup(sock), "w");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI_RPC_CONN::~GUI_RPC_CONN() {
|
2004-02-28 19:11:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
2004-01-21 07:07:16 +00:00
|
|
|
close(sock);
|
|
|
|
fclose(fout);
|
2004-01-21 22:54:35 +00:00
|
|
|
#endif
|
2004-01-21 07:07:16 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 19:11:40 +00:00
|
|
|
static PROJECT* get_project(char* buf, FILE* fout) {
|
|
|
|
string url;
|
|
|
|
if (!parse_str(buf, "<project_url>", url)) {
|
|
|
|
fprintf(fout, "<error>Missing project URL</error>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PROJECT* p = gstate.lookup_project(url.c_str());
|
|
|
|
if (!p) {
|
|
|
|
fprintf(fout, "<error>No such project</error>\n");
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_result_show_graphics(char* buf, FILE* fout) {
|
|
|
|
string result_name;
|
|
|
|
PROJECT* p = get_project(buf, fout);
|
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
if (!parse_str(buf, "<result_name>", result_name)) {
|
|
|
|
fprintf(fout, "<error>Missing result name</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RESULT* rp = gstate.lookup_result(p, result_name.c_str());
|
|
|
|
if (!rp) {
|
|
|
|
fprintf(fout, "<error>No such result</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ACTIVE_TASK* atp = gstate.lookup_active_task_by_result(rp);
|
|
|
|
if (!atp) {
|
|
|
|
fprintf(fout, "<error>Result not active</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
atp->request_graphics_mode(MODE_WINDOW);
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handle_project_reset(char* buf, FILE* fout) {
|
|
|
|
PROJECT* p = get_project(buf, fout);
|
|
|
|
if (p) {
|
|
|
|
gstate.reset_project(p);
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_project_attach(char* buf, FILE* fout) {
|
|
|
|
string url, authenticator;
|
|
|
|
if (!parse_str(buf, "<url>", url)) {
|
|
|
|
fprintf(fout, "<error>Missing URL</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!parse_str(buf, "<authenticator>", authenticator)) {
|
|
|
|
fprintf(fout, "<error>Missing authenticator</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gstate.add_project(url.c_str(), authenticator.c_str());
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_project_detach(char* buf, FILE* fout) {
|
|
|
|
PROJECT* p = get_project(buf, fout);
|
|
|
|
if (p) {
|
|
|
|
gstate.detach_project(p);
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_project_update(char* buf, FILE* fout) {
|
|
|
|
PROJECT* p = get_project(buf, fout);
|
|
|
|
if (p) {
|
|
|
|
p->sched_rpc_pending = true;
|
|
|
|
p->min_rpc_time = 0;
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_set_run_mode(char* buf, FILE* fout) {
|
|
|
|
if (match_tag(buf, "<always>")) {
|
|
|
|
gstate.user_run_request = USER_RUN_REQUEST_ALWAYS;
|
|
|
|
} else if (match_tag(buf, "<never>")) {
|
|
|
|
gstate.user_run_request = USER_RUN_REQUEST_NEVER;
|
|
|
|
} else if (match_tag(buf, "<auto>")) {
|
|
|
|
gstate.user_run_request = USER_RUN_REQUEST_AUTO;
|
|
|
|
} else {
|
|
|
|
fprintf(fout, "<error>Missing mode</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_run_benchmarks(char* buf, FILE* fout) {
|
|
|
|
// TODO: suspend activities; make sure run at right priority
|
|
|
|
//
|
2004-03-21 00:10:15 +00:00
|
|
|
gstate.start_cpu_benchmarks();
|
2004-02-28 19:11:40 +00:00
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_set_proxy_settings(char* buf, FILE* fout) {
|
|
|
|
string proxy_server_name;
|
|
|
|
int proxy_server_port;
|
|
|
|
if (!parse_str(buf, "<proxy_server_name>", proxy_server_name)) {
|
|
|
|
fprintf(fout, "<error>Proxy server name missing</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!parse_int(buf, "<proxy_server_port>", proxy_server_port)) {
|
|
|
|
fprintf(fout, "<error>Proxy server port missing</error>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
safe_strcpy(gstate.proxy_server_name, proxy_server_name.c_str());
|
|
|
|
gstate.proxy_server_port = proxy_server_port;
|
|
|
|
fprintf(fout, "<success/>\n");
|
|
|
|
}
|
|
|
|
|
2004-01-21 07:07:16 +00:00
|
|
|
int GUI_RPC_CONN::handle_rpc() {
|
2004-02-28 19:11:40 +00:00
|
|
|
char buf[1024];
|
2004-01-21 07:07:16 +00:00
|
|
|
int n;
|
|
|
|
|
2004-01-30 22:19:19 +00:00
|
|
|
// read the request message in one read()
|
|
|
|
// so that the core client won't hang because
|
|
|
|
// of malformed request msgs
|
|
|
|
//
|
2004-02-28 19:11:40 +00:00
|
|
|
n = read(sock, buf, 1024);
|
2004-01-21 07:07:16 +00:00
|
|
|
if (n <= 0) return -1;
|
|
|
|
buf[n] = 0;
|
|
|
|
printf("got %s\n", buf);
|
2004-01-22 19:30:42 +00:00
|
|
|
if (match_tag(buf, "<get_state")) {
|
|
|
|
gstate.write_state(fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<result_show_graphics>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_result_show_graphics(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<project_reset>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_project_reset(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<project_attach>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_project_attach(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<project_detach>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_project_detach(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<project_update>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_project_update(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<set_run_mode>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_set_run_mode(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<run_benchmarks>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_run_benchmarks(buf, fout);
|
2004-01-30 22:19:19 +00:00
|
|
|
} else if (match_tag(buf, "<set_proxy_settings>")) {
|
2004-02-28 19:11:40 +00:00
|
|
|
handle_set_proxy_settings(buf, fout);
|
2004-01-21 07:07:16 +00:00
|
|
|
} else {
|
|
|
|
fprintf(fout, "<unrecognized/>\n");
|
|
|
|
}
|
|
|
|
fflush(fout);
|
2004-01-21 22:54:35 +00:00
|
|
|
return 0;
|
2004-01-21 07:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GUI_RPC_CONN_SET::insert(GUI_RPC_CONN* p) {
|
|
|
|
gui_rpcs.push_back(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GUI_RPC_CONN_SET::init(char* path) {
|
2004-02-28 19:11:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
sockaddr addr;
|
|
|
|
#else
|
2004-01-21 07:07:16 +00:00
|
|
|
sockaddr_un addr;
|
2004-02-28 19:11:40 +00:00
|
|
|
#endif
|
2004-01-21 07:07:16 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
unlink(path);
|
2004-02-28 19:11:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
addr.sa_family = AF_UNIX;
|
|
|
|
strcpy(addr.sa_data, path);
|
|
|
|
WSADATA wsdata;
|
|
|
|
WORD wVersionRequested = MAKEWORD(1, 1);
|
|
|
|
WSAStartup(wVersionRequested, &wsdata);
|
2004-03-02 03:46:04 +00:00
|
|
|
#else
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(addr.sun_path, path);
|
|
|
|
#endif
|
2004-01-21 07:07:16 +00:00
|
|
|
lsock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (lsock < 0) {
|
|
|
|
perror("socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
retval = bind(lsock, (const sockaddr*)(&addr), sizeof(addr));
|
|
|
|
if (retval) {
|
|
|
|
perror("bind");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
retval = listen(lsock, 999);
|
|
|
|
if (retval) {
|
|
|
|
perror("listen");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GUI_RPC_CONN_SET::poll() {
|
|
|
|
unsigned int i;
|
|
|
|
fd_set read_fds, error_fds;
|
|
|
|
int sock, n, retval;
|
|
|
|
vector<GUI_RPC_CONN*>::iterator iter;
|
|
|
|
GUI_RPC_CONN* gr;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
FD_ZERO(&error_fds);
|
|
|
|
FD_SET(lsock, &read_fds);
|
|
|
|
for (i=0; i<gui_rpcs.size(); i++) {
|
|
|
|
gr = gui_rpcs[i];
|
|
|
|
FD_SET(gr->sock, &read_fds);
|
|
|
|
FD_SET(gr->sock, &error_fds);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
|
|
|
n = select(FD_SETSIZE, &read_fds, 0, &error_fds, &tv);
|
|
|
|
if (FD_ISSET(lsock, &read_fds)) {
|
|
|
|
sock = accept(lsock, 0, 0);
|
|
|
|
GUI_RPC_CONN* gr = new GUI_RPC_CONN(sock);
|
|
|
|
insert(gr);
|
|
|
|
}
|
|
|
|
iter = gui_rpcs.begin();
|
|
|
|
while (iter != gui_rpcs.end()) {
|
|
|
|
gr = *iter;
|
|
|
|
if (FD_ISSET(gr->sock, &error_fds)) {
|
|
|
|
delete gr;
|
|
|
|
gui_rpcs.erase(iter);
|
|
|
|
} else {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter = gui_rpcs.begin();
|
|
|
|
while (iter != gui_rpcs.end()) {
|
|
|
|
gr = *iter;
|
|
|
|
if (FD_ISSET(gr->sock, &read_fds)) {
|
|
|
|
retval = gr->handle_rpc();
|
|
|
|
if (retval) {
|
|
|
|
delete gr;
|
|
|
|
gui_rpcs.erase(iter);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
return (n != 0);
|
|
|
|
}
|