rpc.cc 16 KB
Newer Older
1
/*
2
 * Copyright (c) 2004-2014 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25
 */

#include "config.h"

26
#include <unistd.h>
27 28 29 30 31 32 33 34 35
#include <sys/param.h>

#include <math.h>
#include <limits.h>
#include <sys/types.h>
#include <assert.h>
#include <pwd.h>

#include <string>
36 37
#include <cstring>
#include <sstream>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <set>
using namespace std;

#include "log.h"
#include "rpc.h"

/**
 * We cache the connection to the server until all of the RPCs have completed
 * so we do not have to reconnect.
 */
struct r_rpc_data rpc_data = {
	NULL,
	BOSSNODE,
	DEFAULT_RPC_PORT,
	-1,
53
	NULL,
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
	PTHREAD_MUTEX_INITIALIZER
};

const char *topography_name;

int RPC_init(const char *certpath, const char *host, unsigned short port)
{
	struct passwd *pwd;
	int retval;

	assert(host != NULL);
	assert(strlen(host) > 0);
	
	if ((pwd = getpwuid(getuid())) == NULL) {
		error("uid %d does not exist", getuid());
		retval = -1;
	}
	else if ((retval = pthread_mutex_init(&rpc_data.mutex, NULL)) != 0) {
		error("could not initialize mutex");
		retval = -1;
	}
	else {
		if (certpath == NULL) {
			char buf[BUFSIZ];
			
			snprintf(buf,
				 sizeof(buf),
				 "%s/.ssl/emulab.pem",
				 pwd->pw_dir);
			rpc_data.certpath = strdup(buf);
		}
		if (host != NULL)
			rpc_data.host = host;
		if (port > 0)
			rpc_data.port = port;
		rpc_data.refcount = 0;

		retval = 0;
	}
	
	return retval;
}

int RPC_grab(void)
{
	int retval = 0;

	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);
	
	assert(rpc_data.refcount >= 0);
	
	rpc_data.refcount += 1;
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
	
	return retval;
}

void RPC_drop(void)
{
	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);
	
	assert(rpc_data.refcount > 0);

	rpc_data.refcount -= 1;
	if (rpc_data.refcount == 0) {
123 124 125
		if (rpc_data.transport != NULL) {
			delete rpc_data.transport;
			rpc_data.transport = NULL;
126 127 128 129 130 131 132 133
		}
	}
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
}

static int
134
RPC_connect(xmlrpc_c::clientXmlTransport **transport)
135 136 137
{
	int retval = 0;
	
138
	assert(transport != NULL);
139 140 141 142

	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);
	
143 144 145
	if (rpc_data.transport != NULL) {
		*transport = rpc_data.transport;
		rpc_data.transport = NULL;
146 147
	}
	else {
148 149 150 151 152 153 154 155 156
		*transport = new xmlrpc_c::clientXmlTransport_curl(
			xmlrpc_c::clientXmlTransport_curl::constrOpt()
			.sslcertpasswd("")
			.ssl_cert(rpc_data.certpath)
			.sslkey(rpc_data.certpath)
			.no_ssl_verifyhost(true)
			.no_ssl_verifypeer(true)
//			.timeout(30 * 60 * 1000)
			);
157 158 159 160
	}
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
161 162

	return retval;
163 164 165
}

static void
166
RPC_disconnect(xmlrpc_c::clientXmlTransport **transport)
167
{
168
	assert(transport != NULL);
169 170 171 172
	
	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);

173 174
	if (rpc_data.transport == NULL) {
		rpc_data.transport = *transport;
175 176
	}
	else {
177
		delete *transport;
178 179 180 181 182 183 184 185 186 187 188 189 190 191
	}
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
}

int
RPC_invoke(char *method,
	   emulab::EmulabResponse *er_out,
	   emulab::spa_attr_t tag,
	   ...)
{
	int retval = 0;
	va_list args;
192
	xmlrpc_c::clientXmlTransport *transport = NULL;
193

194 195
	cout << "Beginning call to " << method << "\n";
	RPC_connect(&transport);
196 197 198 199

	va_start(args, tag);
	try
	{
200 201 202 203 204 205 206 207
		ostringstream buffer;
		string url;
		
		buffer << "https://" << rpc_data.host << ':'
		       << rpc_data.port << XMLRPC_ROOT;
		url = buffer.str();
		
		emulab::ServerProxy proxy(transport, false, url.c_str());
208 209 210
		
		*er_out = proxy.invoke(method, tag, args);
		
211 212 213 214 215 216
		if (!er_out->isSuccess()){
			cerr << "RPC_invoke failed: "
			     << method
			     << " "
			     << (string)(er_out->getOutput())
			     << std::endl;
217 218 219
			retval = -1;
		}
	}
220
	catch(girerr::error &ex)
221
	{
222 223
		cout << "Error occured: " <<
			ex.what() << std::endl;
224 225 226 227
		retval = -1;
	}
	catch(...)
	{
228
		cout << "unknown Error occured.\n";
229 230 231 232
		retval = -1;
	}
	va_end(args);
	
233
	RPC_disconnect(&transport);
234

235
	cout << "Finished with call to " << method << "\n";
236 237 238 239 240 241 242
	return retval;
}

int
RPC_invoke(char *pid, char *eid, char *method, emulab::EmulabResponse *er)
{
	int retval = 0;
243
	xmlrpc_c::clientXmlTransport *transport;
244 245 246 247 248

	assert(pid != NULL);
	assert(eid != NULL);
	assert(method != NULL);
	assert(er != NULL);
249 250

	transport = NULL;
251
	
252 253
	cout << "Beginning call to " << method << "\n";
	RPC_connect(&transport);
254 255 256
	
	try
	{
257 258 259 260 261 262 263 264
		ostringstream buffer;
		string url;
		
		buffer << "https://" << rpc_data.host << ':'
		       << rpc_data.port << XMLRPC_ROOT;
		url = buffer.str();
		
		emulab::ServerProxy proxy(transport, false, url.c_str());
265 266 267 268 269 270 271
		
		*er = proxy.invoke(method,
				   emulab::SPA_String, "proj", pid,
				   emulab::SPA_String, "exp", eid,
				   emulab::SPA_TAG_DONE);
		
		if (! er->isSuccess()){
272 273 274 275 276
			cerr << "RPC_invoke failed: "
			     << method
			     << " "
			     << (string)er->getOutput()
			     << std::endl;
277 278 279
			retval = -1;
		}
	}
280
	catch(girerr::error &ex)
281
	{
282 283
		cout << "Error occured: " <<
			ex.what() << std::endl;
284 285 286 287
		retval = -1;
	}
	catch(...)
	{
288
		cout << "unknown Error occured.\n";
289 290 291
		retval = -1;
	}

292 293
	RPC_disconnect(&transport);
	cout << "Finished with call to " << method << "\n";
294 295 296 297 298 299 300 301 302 303 304 305 306 307

	return retval;
}

int
RPC_metadata(char *pid, char *eid)
{
	emulab::EmulabResponse er;
	int retval;

	assert(pid != NULL);
	assert(eid != NULL);

	if ((retval = RPC_invoke(pid, eid, "experiment.metadata", &er)) == 0) {
308 309 310
		string path;
		vector<xmlrpc_c::value> userenv;
		map <string, xmlrpc_c::value> md;
311

312 313 314
		md = (xmlrpc_c::value_struct)er.getValue();
		path = (xmlrpc_c::value_string)md["path"];
		SetExpPath(((string)path).c_str());
315

316 317
		userenv = ((xmlrpc_c::value_array)md["user_environment"]).vectorValueValue();
		
318 319 320 321
		if (userenv.size() > 0) {
			int lpc;

			for (lpc = 0; lpc < userenv.size(); lpc++) {
322
				string name, value;
323
				map<string, xmlrpc_c::value> ue;
324
				
325
				ue = (xmlrpc_c::value_struct)userenv[lpc];
326 327
				name = (xmlrpc_c::value_string)ue["name"];
				value = (xmlrpc_c::value_string)ue["value"];
328

329 330
				if ((retval = AddUserEnv(name.c_str(),
				                         value.c_str())) != 0)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
					return retval;
			}
		}
	}

	return retval;
}

expt_state_t
RPC_expt_state(char *pid, char *eid)
{
	expt_state_t retval = ES_UNKNOWN;
	emulab::EmulabResponse er;

	assert(pid != NULL);
	assert(strlen(pid) > 0);
	assert(eid != NULL);
	assert(strlen(eid) > 0);

	if (RPC_invoke(pid, eid, "experiment.state", &er) == 0) {
		const char *state;
352
		string tmp;
353

354 355
		tmp = (string)(xmlrpc_c::value_string)er.getValue();
		state = tmp.c_str();
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
		if (strcmp(state, "activating") == 0)
			retval = ES_ACTIVATING;
		else if (strcmp(state, "active") == 0)
			retval = ES_ACTIVE;
		else if (strcmp(state, "modify_reswap") == 0)
			retval = ES_MODIFY_RESWAP;
	}

	return retval;
}

int RPC_notifystart(char *pid, char *eid, char *timeline, int set_or_clear)
{
	emulab::EmulabResponse er;
	double time_start = 0.0;
	
	assert(pid != NULL);
	assert(strlen(pid) > 0);
	assert(eid != NULL);
	assert(strlen(eid) > 0);
	assert(timeline != NULL);

	if (set_or_clear) {
		struct timeval tv;
		
		gettimeofday(&tv, NULL);
		time_start = tv.tv_sec + ((double)tv.tv_usec) / 1000000;
	}
	
	return RPC_invoke("experiment.event_time_start",
			  &er,
			  emulab::SPA_String, "pid", pid,
			  emulab::SPA_String, "eid", eid,
			  emulab::SPA_String, "timeline", timeline,
			  emulab::SPA_Double, "time", time_start,
			  emulab::SPA_TAG_DONE);
}

static int
RPC_cameralist(FILE *emcd_config, char *area)
{
	emulab::EmulabResponse er;
	int lpc, rc;
	
	assert(emcd_config != NULL);
	assert(area != NULL);
	
	rc = RPC_invoke("emulab.vision_config",
			&er,
			emulab::SPA_String, "area", area,
			emulab::SPA_TAG_DONE);
	
	if (rc < 0)
		return -1;
	
411
	vector<xmlrpc_c::value> cameras;
412
	
413
	cameras = ((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
414
	for (lpc = 0; lpc < cameras.size(); lpc++) {
415 416
		string tmp;
		map<string, xmlrpc_c::value> attr;
417 418
		int port;

419 420 421
		attr = (xmlrpc_c::value_struct)cameras[lpc];
		tmp = (xmlrpc_c::value_string)attr["hostname"];
		port = (xmlrpc_c::value_int)attr["port"];
422 423 424
		fprintf(emcd_config,
			"camera %s %s %d %f %f %f %f %f %f\n",
			area,
425
		        tmp.c_str(),
426
			port,
427 428 429 430 431 432
		        ((double)(xmlrpc_c::value_double)attr["loc_x"]),
		        ((double)(xmlrpc_c::value_double)attr["loc_y"]),
		        ((double)(xmlrpc_c::value_double)attr["width"]),
		        ((double)(xmlrpc_c::value_double)attr["height"]),
		        ((double)(xmlrpc_c::value_double)attr["fixed_x"]),
		        ((double)(xmlrpc_c::value_double)attr["fixed_y"]));
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	}
	
	return 0;
}

static int
RPC_obstaclelist(FILE *emcd_config, char *area)
{
	emulab::EmulabResponse er;
	int lpc, rc;
	
	assert(emcd_config != NULL);
	assert(area != NULL);
	
	rc = RPC_invoke("emulab.obstacle_config",
			&er,
			emulab::SPA_String, "area", area,
			emulab::SPA_String, "units", "meters",
			emulab::SPA_TAG_DONE);
	
	if (rc < 0)
		return -1;
	
456 457
	vector<xmlrpc_c::value> obstacles =
		((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
458 459
	
	for (lpc = 0; lpc < obstacles.size(); lpc++) {
460 461
		map<string, xmlrpc_c::value> attr =
			(xmlrpc_c::value_struct)obstacles[lpc];
462 463 464 465
		
		fprintf(emcd_config,
			"obstacle %s %d %f %f %f %f\n",
			area,
466 467 468 469 470
		        ((int)(xmlrpc_c::value_int)attr["obstacle_id"]),
		        ((double)(xmlrpc_c::value_double)attr["x1"]),
		        ((double)(xmlrpc_c::value_double)attr["y1"]),
		        ((double)(xmlrpc_c::value_double)attr["x2"]),
		        ((double)(xmlrpc_c::value_double)attr["y2"]));
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
	}
	
	return 0;
}

int
RPC_waitforactive(char *pid, char *eid)
{
	emulab::EmulabResponse er;

	assert(pid != NULL);
	assert(strlen(pid) > 0);
	assert(eid != NULL);
	assert(strlen(eid) > 0);

	return RPC_invoke(pid, eid, "experiment.waitforactive", &er);
}

int
RPC_waitforrobots(event_handle_t handle, char *pid, char *eid)
{
	emulab::EmulabResponse er;
	int lpc, rc, retval = 0;
	FILE *emcd_config;

	unlink("tbdata/emcd.config");
	
	rc = RPC_invoke("experiment.virtual_topology",
			&er,
			emulab::SPA_String, "proj", pid,
			emulab::SPA_String, "exp", eid,
			emulab::SPA_String, "tables", "node_startlocs",
			emulab::SPA_TAG_DONE);
	
	if (rc < 0)
		return rc;
	
	set<string> buildings;
509 510
	vector<xmlrpc_c::value> top, locs;
	map<string, xmlrpc_c::value> tables;
511
	
512 513 514 515
	top = ((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
	tables = (xmlrpc_c::value_struct)top[0];
	tables = ((xmlrpc_c::value_struct)tables["experiment"]);
	locs = ((xmlrpc_c::value_array)tables["node_startlocs"]).vectorValueValue();
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	
	if (locs.size() == 0)
		return 0;

	info("info: waiting for robots\n");
	
	if ((emcd_config = fopen("tbdata/emcd.config", "w")) == NULL) {
		error("Cannot create emcd.config!\n");
		return -1;
	}

	if (topography_name != NULL) {
		fprintf(emcd_config, "topography %s\n", topography_name);
	}
	
	for (lpc = 0; lpc < locs.size(); lpc++) {
532
		map<string, xmlrpc_c::value> loc;
533
		emulab::EmulabResponse ner;
534
		string bldg;
535 536 537
		double x, y, orientation;
		struct agent *agent;
		char *vname;
538 539 540 541 542 543 544 545 546

		loc = ((xmlrpc_c::value_struct)locs[lpc]);
		vname =
			(char *)((string)(xmlrpc_c::value_string)loc["vname"]).c_str();
		bldg = (xmlrpc_c::value_string)loc["building"];
		x = ((xmlrpc_c::value_double)loc["loc_x"]);
		y = ((xmlrpc_c::value_double)loc["loc_y"]);
		orientation =
			((xmlrpc_c::value_double)loc["orientation"]);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		if ((agent = (struct agent *)
		     lnFindName(&agents, vname)) == NULL) {
			error("unknown robot %s\n", vname);
			
			return -1;
		}
		else if ((rc = RPC_invoke(
			"node.statewait",
			&ner,
			emulab::SPA_String, "node", agent->nodeid,
			emulab::SPA_Integer, "timeout", ROBOT_TIMEOUT,
			emulab::SPA_String, "state", "ISUP",
			emulab::SPA_TAG_DONE)) < 0) {
			error("robot %s did not come up!\n", agent->name);
			
			return -1;
		}
		else {
			fprintf(emcd_config,
				"robot %s %d %s %f %f %f %s\n",
567
			        ((string)(bldg)).c_str(),
568 569 570 571 572 573
				lpc + 1,
				agent->nodeid,
				x,
				y,
				orientation * M_PI / 180.0,
				agent->name);
574
			buildings.insert((string)bldg);
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

			AddRobot(handle, agent, x, y, orientation);
		}
	}
	
	for (set<string>::iterator i = buildings.begin();
	     i != buildings.end();
	     i++) {
		if (RPC_cameralist(emcd_config, (char *)(*i).c_str()) < 0)
			return -1;
		
		if (RPC_obstaclelist(emcd_config, (char *)(*i).c_str()) < 0)
			return -1;
	}
	
	fclose(emcd_config);
	emcd_config = NULL;
	
	return retval;
}

int
RPC_agentlist(event_handle_t handle, char *pid, char *eid)
{
	emulab::EmulabResponse er;
	int i, foo = RPC_invoke(pid, eid, "experiment.event_agentlist", &er);
	
	if (foo)
		return foo;
	
605 606
	vector<xmlrpc_c::value> agents =
		((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
607 608
	
	for (i = 0; i < agents.size(); i++) {
609
		string vname, vnode, nodeid, ipaddr, type;
610 611
		vector<xmlrpc_c::value>agent =
			((xmlrpc_c::value_array)agents[i]).vectorValueValue();
612
		
613 614 615 616 617 618 619 620
		vname = (xmlrpc_c::value_string)agent[0];
		vnode = (xmlrpc_c::value_string)agent[1];
		nodeid = (xmlrpc_c::value_string)agent[2];
		ipaddr = (xmlrpc_c::value_string)agent[3];
		type = (xmlrpc_c::value_string)agent[4];
		info("D: adding agent %s\n", (char *)vname.c_str());
		if (AddAgent(handle, vname.c_str(), vnode.c_str(),
		             nodeid.c_str(), ipaddr.c_str(), type.c_str()) < 0) {
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
			return -1;
		}
	}
  
	return 0;
}

int
RPC_grouplist(event_handle_t handle, char *pid, char *eid)
{
	emulab::EmulabResponse er;
	int i, foo = RPC_invoke(pid, eid, "experiment.event_grouplist", &er);
	
	if (foo)
		return foo;
	
637 638
	vector<xmlrpc_c::value> groups =
		((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
639 640
	
	for (i = 0; i < groups.size(); i++) {
641
		string groupname, agentname;
642 643
		vector<xmlrpc_c::value> group =
			((xmlrpc_c::value_array)groups[i]).vectorValueValue();
644
		
645 646 647 648 649 650
		groupname = (xmlrpc_c::value_string)group[0];
		info("D: \tIn GroupList() parsed name %s\n",
		     (char *)groupname.c_str());
		agentname = (xmlrpc_c::value_string)group[1];
		info("D: \tIn GroupList() parsed agent %s\n",
		     (char *)agentname.c_str());
651
		
652 653
		if (AddGroup(handle, groupname.c_str(),
		             agentname.c_str()) != 0) {
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
			return -1;
		}
	}
	return 0;
}

int
RPC_eventlist(char *pid, char *eid,
	      event_handle_t handle, address_tuple_t tuple)
{ /* This function requests all the static events from boss using an RPC call, packing them into its
   * own data structures. */
	emulab::EmulabResponse er;
	int i, foo = RPC_invoke(pid, eid, "experiment.event_eventlist", &er);

// XXX
//	info("rpc.cc:RPC_eventlist(): Just called RPC_invoke()\n");
// XXX	
	if (foo)
		return foo;

// XXX
//	info("rpc.cc:RPC_eventlist(): calling getValue()\n");
676 677 678
// XXX
	vector<xmlrpc_c::value> events =
		((xmlrpc_c::value_array)er.getValue()).vectorValueValue();
679 680 681 682 683
	
// XXX
//	info("rpc.cc:RPC_eventlist(): Interating over invoke results\n");
// XXX	
	for (i = 0; i < events.size(); i++) {
684 685
		string exidx, extime, objname, objtype, evttype, exargs;
		string parent, triggertype;
686 687 688
		vector<xmlrpc_c::value> event =
			((xmlrpc_c::value_array)events[i]).vectorValueValue();

689 690 691 692 693 694 695 696
		exidx = (xmlrpc_c::value_string)event[0];
		extime = (xmlrpc_c::value_string)event[1];
		objname = (xmlrpc_c::value_string)event[2];
		objtype = (xmlrpc_c::value_string)event[3];
		evttype = (xmlrpc_c::value_string)event[4];
		exargs = (xmlrpc_c::value_string)event[5];
		parent = (xmlrpc_c::value_string)event[6];
		triggertype = (xmlrpc_c::value_string)event[7];
697 698 699
// XXX
//	info("rpc.cc:RPC_eventlist(): Adding an event\n");
// XXX 		
700 701 702 703 704
		if (AddEvent(handle, tuple, exidx.c_str(),
			     extime.c_str(), objname.c_str(),
		             exargs.c_str(), objtype.c_str(),
		             evttype.c_str(), parent.c_str(),
		             triggertype.c_str()) < 0) {
705 706 707 708 709 710
			return -1;
		}
	}
	return 0;
}