rpc.cc 12.9 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2004, 2005 University of Utah and the Flux Group.
4 5
 * All rights reserved.
 */
6 7 8

#include "config.h"

9
#include <sys/param.h>
10

11
#include <math.h>
12 13
#include <limits.h>
#include <sys/types.h>
14
#include <assert.h>
15
#include <pwd.h>
16

17 18 19
#include <string>
#include <set>
using namespace std;
20

21 22
#include "log.h"
#include "rpc.h"
23 24 25 26 27

/**
 * We cache the connection to the server until all of the RPCs have completed
 * so we do not have to reconnect.
 */
28 29 30 31 32 33 34 35
struct r_rpc_data rpc_data = {
	NULL,
	BOSSNODE,
	DEFAULT_RPC_PORT,
	-1,
	{ NULL, NULL },
	PTHREAD_MUTEX_INITIALIZER
};
36

37
int RPC_init(const char *certpath, const char *host, unsigned short port)
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
	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;
73 74
}

75
int RPC_grab(void)
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
	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) {
		if (rpc_data.conn_proto.proto != NULL) {
			delete rpc_data.conn_proto.proto;
			rpc_data.conn_proto.proto = NULL;
			delete rpc_data.conn_proto.conn;
			rpc_data.conn_proto.conn = NULL;
		}
107
	}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
}

static int
RPC_connect(struct rpc_conn_proto *rcp_out)
{
	ulxr::SSLConnection *sslconn;
	int retval = 0;
	
	assert(rcp_out != NULL);

	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);
	
	if (rpc_data.conn_proto.conn != NULL) {
		*rcp_out = rpc_data.conn_proto;
		rpc_data.conn_proto.conn = NULL;
		rpc_data.conn_proto.proto = NULL;
128
	}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	else {
		rcp_out->conn = sslconn =
			new ulxr::SSLConnection(false,
						rpc_data.host,
						rpc_data.port);
		sslconn->setTimeout(30 * 60);
		rcp_out->proto =
			new ulxr::HttpProtocol(rcp_out->conn,
					       sslconn->getHostName());
		rcp_out->proto->setPersistent(true);
		sslconn->setCryptographyData("",
					     rpc_data.certpath,
					     rpc_data.certpath);
	}
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
146 147
}

148 149
static void
RPC_disconnect(struct rpc_conn_proto *rcp)
150
{
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	assert(rcp != NULL);
	assert(rcp->proto != NULL);
	assert(rcp->conn != NULL);
	
	if (pthread_mutex_lock(&rpc_data.mutex) != 0)
		assert(0);

	if (rpc_data.conn_proto.conn == NULL) {
		rpc_data.conn_proto = *rcp;
	}
	else {
		delete rcp->proto;
		delete rcp->conn;
	}
	rcp->conn = NULL;
	rcp->proto = NULL;
	
	if (pthread_mutex_unlock(&rpc_data.mutex) != 0)
		assert(0);
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
int
RPC_invoke(char *method,
	   emulab::EmulabResponse *er_out,
	   emulab::spa_attr_t tag,
	   ...)
{
	struct rpc_conn_proto rcp;
	int retval = 0;
	va_list args;

	RPC_connect(&rcp);

	va_start(args, tag);
	try
	{
187
		emulab::ServerProxy proxy(rcp.proto, false, XMLRPC_ROOT);
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
		
		*er_out = proxy.invoke(method, tag, args);
		
		if (! er_out->isSuccess()){
			ULXR_CERR << "RPC_invoke failed: "
				  << method
				  << " "
				  << er_out->getOutput()
				  << std::endl;
			retval = -1;
		}
	}
	catch(ulxr::Exception &ex)
	{
		ULXR_COUT << ULXR_PCHAR("Error occured: ") <<
			ULXR_GET_STRING(ex.why()) << std::endl;
		retval = -1;
	}
	catch(...)
	{
		ULXR_COUT << ULXR_PCHAR("unknown Error occured.\n");
		retval = -1;
	}
	va_end(args);
	
	RPC_disconnect(&rcp);

	return retval;
}

int
219 220
RPC_invoke(char *pid, char *eid, char *method, emulab::EmulabResponse *er)
{
221 222 223 224 225 226 227 228 229 230 231 232
	struct rpc_conn_proto rcp;
	int retval = 0;

	assert(pid != NULL);
	assert(eid != NULL);
	assert(method != NULL);
	assert(er != NULL);
	
	RPC_connect(&rcp);
	
	try
	{
233
		emulab::ServerProxy proxy(rcp.proto, false, XMLRPC_ROOT);
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
		
		*er = proxy.invoke(method,
				   emulab::SPA_String, "proj", pid,
				   emulab::SPA_String, "exp", eid,
				   emulab::SPA_TAG_DONE);
		
		if (! er->isSuccess()){
			ULXR_CERR << "RPC_invoke failed: "
				  << method
				  << " "
				  << er->getOutput()
				  << std::endl;
			retval = -1;
		}
	}
	catch(ulxr::Exception &ex)
	{
		ULXR_COUT << ULXR_PCHAR("Error occured: ") <<
			ULXR_GET_STRING(ex.why()) << std::endl;
		retval = -1;
	}
	catch(...)
	{
		ULXR_COUT << ULXR_PCHAR("unknown Error occured.\n");
		retval = -1;
	}

	RPC_disconnect(&rcp);

	return retval;
}

int
RPC_exppath(char *pid, char *eid, char *path_out, size_t path_size)
{
	emulab::EmulabResponse er;
	int retval;

	assert(pid != NULL);
	assert(eid != NULL);
	assert(path_out != NULL);
	assert(path_size > 0);

	if ((retval = RPC_invoke(pid, eid, "experiment.metadata", &er)) == 0) {
		ulxr::RpcString path;
		ulxr::Struct md;

		md = (ulxr::Struct)er.getValue();
		path = md.getMember(ULXR_PCHAR("path"));
		strncpy(path_out, path.getString().c_str(), path_size);
		path_out[path_size - 1] = '\0';
	}

	return retval;
288 289 290 291 292
}

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

295 296 297 298 299 300 301 302
	assert(pid != NULL);
	assert(strlen(pid) > 0);
	assert(eid != NULL);
	assert(strlen(eid) > 0);

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

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
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);
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
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;
	
	ulxr::Array cameras;
	
	cameras = (ulxr::Array)er.getValue();
	for (lpc = 0; lpc < cameras.size(); lpc++) {
		ulxr::RpcString tmp;
		ulxr::Struct attr;
		int port;
354

355
		attr = cameras.getItem(lpc);
356
		tmp = attr.getMember("hostname");
357 358
		port = ((ulxr::Integer)attr.getMember("port")).getInteger();
		fprintf(emcd_config,
Timothy Stack's avatar
Timothy Stack committed
359
			"camera %s %s %d %f %f %f %f\n",
360 361
			area,
			tmp.getString().c_str(),
Timothy Stack's avatar
Timothy Stack committed
362
			port,
363 364
			((ulxr::Double)attr.getMember("loc_x")).getDouble(),
			((ulxr::Double)attr.getMember("loc_y")).getDouble(),
Timothy Stack's avatar
Timothy Stack committed
365 366
			((ulxr::Double)attr.getMember("width")).getDouble(),
			((ulxr::Double)attr.getMember("height")).getDouble());
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
	}
	
	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;
	
	ulxr::Array obstacles;
	
	obstacles = (ulxr::Array)er.getValue();
	for (lpc = 0; lpc < obstacles.size(); lpc++) {
		ulxr::Struct attr;
		
		attr = obstacles.getItem(lpc);
		fprintf(emcd_config,
			"obstacle %s %d %f %f %f %f\n",
			area,
			((ulxr::Integer)attr.getMember("obstacle_id")).
			getInteger(),
			((ulxr::Double)attr.getMember("x1")).getDouble(),
			((ulxr::Double)attr.getMember("y1")).getDouble(),
404
			((ulxr::Double)attr.getMember("x2")).getDouble(),
405 406 407 408
			((ulxr::Double)attr.getMember("y2")).getDouble());
	}
	
	return 0;
409 410 411
}

int
412
RPC_waitforrobots(event_handle_t handle, char *pid, char *eid)
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
	emulab::EmulabResponse er;
	int lpc, rc, retval = 0;
	FILE *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;
	ulxr::Array top, locs;
	ulxr::Struct tables;
	
	top = (ulxr::Array)er.getValue();
	tables = (ulxr::Struct)top.getItem(0);
	tables = tables.getMember("experiment");
	locs = tables.getMember("node_startlocs");
	
	if (locs.size() == 0)
		return 0;
439 440

	info("info: waiting for robots\n");
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	
	if ((emcd_config = fopen("tbdata/emcd.config", "w")) == NULL) {
		fprintf(stderr, "Cannot create emcd.config!\n");
	}
	
	for (lpc = 0; lpc < locs.size(); lpc++) {
		ulxr::Struct loc = (ulxr::Struct)locs.getItem(lpc);
		emulab::EmulabResponse ner;
		ulxr::RpcString tmp, bldg;
		double x, y, orientation;
		struct agent *agent;
		char *vname;
		
		tmp = loc.getMember("vname");
		vname = (char *) tmp.getString().c_str();
		bldg = loc.getMember("building");
		x = ((ulxr::Double)loc.getMember("loc_x")).getDouble();
		y = ((ulxr::Double)loc.getMember("loc_y")).getDouble();
459 460 461
		orientation = ((ulxr::Double)loc.getMember("orientation")).
			getDouble();
		orientation *= M_PI/ 180.0;
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
		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",
				bldg.getString().c_str(),
				lpc + 1,
				agent->nodeid,
				x,
				y,
				orientation,
				agent->name);
			buildings.insert(bldg.getString());
490 491

			AddRobot(handle, agent, x, y, orientation);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
		}
	}
	
	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;
509 510 511
}

int
512
RPC_agentlist(event_handle_t handle, char *pid, char *eid)
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
	emulab::EmulabResponse er;
	int i, foo = RPC_invoke(pid, eid, "experiment.event_agentlist", &er);
	
	if (foo)
		return foo;
	
	ulxr::Array agents = (ulxr::Array)er.getValue();
	
	for (i = 0; i < agents.size(); i++) {
		char *vname, *vnode, *nodeid, *ipaddr, *type;
		ulxr::RpcString tmp;
		ulxr::Array agent = (ulxr::Array)agents.getItem(i);
		
		tmp = agent.getItem(0);
		vname = (char *) tmp.getString().c_str();
		tmp = agent.getItem(1);
		vnode = (char *) tmp.getString().c_str();
		tmp = agent.getItem(2);
		nodeid = (char *) tmp.getString().c_str();
		tmp = agent.getItem(3);
		ipaddr = (char *) tmp.getString().c_str();
		tmp = agent.getItem(4);
		type = (char *) tmp.getString().c_str();
		
		if (AddAgent(handle, vname, vnode, nodeid, ipaddr, type) < 0) {
			return -1;
		}
	}
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
	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;
	
	ulxr::Array groups = (ulxr::Array)er.getValue();
	
	for (i = 0; i < groups.size(); i++) {
		char *groupname, *agentname;
		ulxr::RpcString tmp;
		ulxr::Array group = (ulxr::Array)groups.getItem(i);
		
		tmp = group.getItem(0);
		groupname = (char *) tmp.getString().c_str();
		tmp = group.getItem(1);
		agentname = (char *) tmp.getString().c_str();
		
		if (AddGroup(handle, groupname, agentname) != 0) {
			return -1;
		}
	}
	return 0;
572 573 574 575 576 577
}

int
RPC_eventlist(char *pid, char *eid,
	      event_handle_t handle, address_tuple_t tuple, long basetime)
{
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
	emulab::EmulabResponse er;
	int i, foo = RPC_invoke(pid, eid, "experiment.event_eventlist", &er);
	
	if (foo)
		return foo;

	ulxr::Array events = (ulxr::Array)er.getValue();
	
	for (i = 0; i < events.size(); i++) {
		char *exidx, *extime, *objname, *objtype, *evttype, *exargs;
		char *parent;
		ulxr::RpcString tmp;
		ulxr::Array event = (ulxr::Array)events.getItem(i);
		
		tmp = event.getItem(0);
		exidx = (char *) tmp.getString().c_str();
		tmp = event.getItem(1);
		extime = (char *) tmp.getString().c_str();
		tmp = event.getItem(2);
		objname = (char *) tmp.getString().c_str();
		tmp = event.getItem(3);
		objtype = (char *) tmp.getString().c_str();
		tmp = event.getItem(4);
		evttype = (char *) tmp.getString().c_str();
		tmp = event.getItem(5);
		exargs = (char *) tmp.getString().c_str();
		tmp = event.getItem(6);
		parent = (char *) tmp.getString().c_str();
		
		if (AddEvent(handle, tuple, basetime, exidx,
			     extime, objname, exargs, objtype, evttype,
			     parent) < 0) {
			return -1;
		}
	}
	return 0;
614 615
}