event.pm 18.4 KB
Newer Older
1 2 3 4 5
# This file was automatically generated by SWIG
package event;
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
6
@EXPORT = qw(address_tuple_alloc address_tuple_free event_register event_unregister c_event_poll dont_use_this_function_because_it_does_not_work event_notify event_schedule event_notification_alloc event_notification_free event_notification_clone event_notification_get_double event_notification_get_int32 event_notification_get_int64 event_notification_get_opaque c_event_notification_get_string event_notification_put_double event_notification_put_int32 event_notification_put_int64 event_notification_put_opaque event_notification_put_string event_notification_remove c_event_subscribe xmalloc xrealloc allocate_callback_data free_callback_data dequeue_callback_data enqueue_callback_data perl_stub_callback stub_event_subscribe event_notification_get_string event_notification_get_site event_notification_get_expt event_notification_get_group event_notification_get_host event_notification_get_objtype event_notification_get_objname event_notification_get_eventtype event_notification_get_arguments event_notification_set_arguments event_notification_get_sender event_notification_set_sender event_handle_server_set event_handle_server_get event_handle_status_set event_handle_status_get address_tuple_site_set address_tuple_site_get address_tuple_expt_set address_tuple_expt_get address_tuple_group_set address_tuple_group_get address_tuple_host_set address_tuple_host_get address_tuple_objtype_set address_tuple_objtype_get address_tuple_objname_set address_tuple_objname_get address_tuple_eventtype_set address_tuple_eventtype_get address_tuple_scheduler_set address_tuple_scheduler_get callback_data_callback_notification_set callback_data_callback_notification_get callback_data_next_set callback_data_next_get  );
7 8 9
package eventc;
bootstrap event;
var_event_init();
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
# ---------- BASE METHODS -------------

package event;

sub TIEHASH {
    my ($classname,$obj) = @_;
    return bless $obj, $classname;
}

sub CLEAR { }

sub this {
    my $ptr = shift;
    return tied(%$ptr);
}


# ------- FUNCTION WRAPPERS --------

package event;

sub address_tuple_alloc {
    my @args = @_;
    my $result = eventc::address_tuple_alloc(@args);
    return undef if (!defined($result));
    my %resulthash;
    tie %resulthash, "address_tuple", $result;
    return bless \%resulthash, "address_tuple";
}
sub address_tuple_free {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::address_tuple_free(@args);
    return $result;
}
sub event_register {
    my @args = @_;
    my $result = eventc::event_register(@args);
    return undef if (!defined($result));
    my %resulthash;
    tie %resulthash, "event_handle", $result;
    return bless \%resulthash, "event_handle";
}
sub event_unregister {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_unregister(@args);
    return $result;
}
sub c_event_poll {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::c_event_poll(@args);
    return $result;
}
sub dont_use_this_function_because_it_does_not_work {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::dont_use_this_function_because_it_does_not_work(@args);
    return $result;
}
sub event_notify {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notify(@args);
    return $result;
}
sub event_schedule {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_schedule(@args);
    return $result;
}
sub event_notification_alloc {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    $args[1] = tied(%{$args[1]});
    my $result = eventc::event_notification_alloc(@args);
    return $result;
}
sub event_notification_free {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_free(@args);
    return $result;
}
sub event_notification_clone {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_clone(@args);
    return $result;
}
sub event_notification_get_double {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_double(@args);
    return $result;
}
sub event_notification_get_int32 {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_int32(@args);
    return $result;
}
sub event_notification_get_int64 {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_int64(@args);
    return $result;
}
sub event_notification_get_opaque {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_opaque(@args);
    return $result;
}
sub c_event_notification_get_string {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::c_event_notification_get_string(@args);
    return $result;
}
sub event_notification_put_double {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_put_double(@args);
    return $result;
}
sub event_notification_put_int32 {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_put_int32(@args);
    return $result;
}
sub event_notification_put_int64 {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_put_int64(@args);
    return $result;
}
sub event_notification_put_opaque {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_put_opaque(@args);
    return $result;
}
sub event_notification_put_string {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_put_string(@args);
    return $result;
}
sub event_notification_remove {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_remove(@args);
    return $result;
}
sub c_event_subscribe {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    $args[2] = tied(%{$args[2]});
    my $result = eventc::c_event_subscribe(@args);
    return $result;
}
*xmalloc = *eventc::xmalloc;
*xrealloc = *eventc::xrealloc;
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
sub allocate_callback_data {
    my @args = @_;
    my $result = eventc::allocate_callback_data(@args);
    return undef if (!defined($result));
    my %resulthash;
    tie %resulthash, "callback_data", $result;
    return bless \%resulthash, "callback_data";
}
sub free_callback_data {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::free_callback_data(@args);
    return $result;
}
sub dequeue_callback_data {
    my @args = @_;
    my $result = eventc::dequeue_callback_data(@args);
    return undef if (!defined($result));
    my %resulthash;
    tie %resulthash, "callback_data", $result;
    return bless \%resulthash, "callback_data";
}
sub enqueue_callback_data {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::enqueue_callback_data(@args);
    return $result;
}
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
sub perl_stub_callback {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::perl_stub_callback(@args);
    return $result;
}
sub stub_event_subscribe {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    $args[1] = tied(%{$args[1]});
    my $result = eventc::stub_event_subscribe(@args);
    return $result;
}
sub event_notification_get_string {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_string(@args);
    return $result;
}
sub event_notification_get_site {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_site(@args);
    return $result;
}
sub event_notification_get_expt {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_expt(@args);
    return $result;
}
sub event_notification_get_group {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_group(@args);
    return $result;
}
sub event_notification_get_host {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_host(@args);
    return $result;
}
sub event_notification_get_objtype {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_objtype(@args);
    return $result;
}
sub event_notification_get_objname {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_objname(@args);
    return $result;
}
sub event_notification_get_eventtype {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_eventtype(@args);
    return $result;
}
sub event_notification_get_arguments {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_arguments(@args);
    return $result;
}
sub event_notification_set_arguments {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_set_arguments(@args);
    return $result;
}
sub event_notification_get_sender {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_get_sender(@args);
    return $result;
}
sub event_notification_set_sender {
    my @args = @_;
    $args[0] = tied(%{$args[0]});
    my $result = eventc::event_notification_set_sender(@args);
    return $result;
}

############# Class : event_handle ##############

package event_handle;
@ISA = qw( event );
%OWNER = ();
%BLESSEDMEMBERS = (
);

%ITERATORS = ();
sub DISOWN {
    my $self = shift;
    my $ptr = tied(%$self);
    delete $OWNER{$ptr};
    };

sub ACQUIRE {
    my $self = shift;
    my $ptr = tied(%$self);
    $OWNER{$ptr} = 1;
    };

sub FETCH {
    my ($self,$field) = @_;
    my $member_func = "eventc::event_handle_${field}_get";
    my $val = &$member_func($self);
    if (exists $BLESSEDMEMBERS{$field}) {
        return undef if (!defined($val));
        my %retval;
        tie %retval,$BLESSEDMEMBERS{$field},$val;
        return bless \%retval, $BLESSEDMEMBERS{$field};
    }
    return $val;
}

sub STORE {
    my ($self,$field,$newval) = @_;
    my $member_func = "eventc::event_handle_${field}_set";
    if (exists $BLESSEDMEMBERS{$field}) {
        &$member_func($self,tied(%{$newval}));
    } else {
        &$member_func($self,$newval);
    }
}

sub FIRSTKEY {
    my $self = shift;
    $ITERATORS{$self} = ['server', 'status', ];
    my $first = shift @{$ITERATORS{$self}};
    return $first;
}

sub NEXTKEY {
    my $self = shift;
    $nelem = scalar @{$ITERATORS{$self}};
    if ($nelem > 0) {
        my $member = shift @{$ITERATORS{$self}};
        return $member;
    } else {
        $ITERATORS{$self} = ['server', 'status', ];
        return ();
    }
}


############# Class : address_tuple ##############

package address_tuple;
@ISA = qw( event );
%OWNER = ();
%BLESSEDMEMBERS = (
);

%ITERATORS = ();
sub DISOWN {
    my $self = shift;
    my $ptr = tied(%$self);
    delete $OWNER{$ptr};
    };

sub ACQUIRE {
    my $self = shift;
    my $ptr = tied(%$self);
    $OWNER{$ptr} = 1;
    };

sub FETCH {
    my ($self,$field) = @_;
    my $member_func = "eventc::address_tuple_${field}_get";
    my $val = &$member_func($self);
    if (exists $BLESSEDMEMBERS{$field}) {
        return undef if (!defined($val));
        my %retval;
        tie %retval,$BLESSEDMEMBERS{$field},$val;
        return bless \%retval, $BLESSEDMEMBERS{$field};
    }
    return $val;
}

sub STORE {
    my ($self,$field,$newval) = @_;
    my $member_func = "eventc::address_tuple_${field}_set";
    if (exists $BLESSEDMEMBERS{$field}) {
        &$member_func($self,tied(%{$newval}));
    } else {
        &$member_func($self,$newval);
    }
}

sub FIRSTKEY {
    my $self = shift;
    $ITERATORS{$self} = ['site', 'expt', 'group', 'host', 'objtype', 'objname', 'eventtype', 'scheduler', ];
    my $first = shift @{$ITERATORS{$self}};
    return $first;
}

sub NEXTKEY {
    my $self = shift;
    $nelem = scalar @{$ITERATORS{$self}};
    if ($nelem > 0) {
        my $member = shift @{$ITERATORS{$self}};
        return $member;
    } else {
        $ITERATORS{$self} = ['site', 'expt', 'group', 'host', 'objtype', 'objname', 'eventtype', 'scheduler', ];
        return ();
    }
}


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
############# Class : callback_data ##############

package callback_data;
@ISA = qw( event );
%OWNER = ();
%BLESSEDMEMBERS = (
    next => 'callback_data',
);

%ITERATORS = ();
sub DISOWN {
    my $self = shift;
    my $ptr = tied(%$self);
    delete $OWNER{$ptr};
    };

sub ACQUIRE {
    my $self = shift;
    my $ptr = tied(%$self);
    $OWNER{$ptr} = 1;
    };

sub FETCH {
    my ($self,$field) = @_;
    my $member_func = "eventc::callback_data_${field}_get";
    my $val = &$member_func($self);
    if (exists $BLESSEDMEMBERS{$field}) {
        return undef if (!defined($val));
        my %retval;
        tie %retval,$BLESSEDMEMBERS{$field},$val;
        return bless \%retval, $BLESSEDMEMBERS{$field};
    }
    return $val;
}

sub STORE {
    my ($self,$field,$newval) = @_;
    my $member_func = "eventc::callback_data_${field}_set";
    if (exists $BLESSEDMEMBERS{$field}) {
        &$member_func($self,tied(%{$newval}));
    } else {
        &$member_func($self,$newval);
    }
}

sub FIRSTKEY {
    my $self = shift;
    $ITERATORS{$self} = ['callback_notification', 'next', ];
    my $first = shift @{$ITERATORS{$self}};
    return $first;
}

sub NEXTKEY {
    my $self = shift;
    $nelem = scalar @{$ITERATORS{$self}};
    if ($nelem > 0) {
        my $member = shift @{$ITERATORS{$self}};
        return $member;
    } else {
        $ITERATORS{$self} = ['callback_notification', 'next', ];
        return ();
    }
}


485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
# ------- VARIABLE STUBS --------

package event;

*MAXHOSTNAMELEN = *eventc::MAXHOSTNAMELEN;
*ADDRESSTUPLE_ANY = *eventc::ADDRESSTUPLE_ANY;
*ADDRESSTUPLE_ALL = *eventc::ADDRESSTUPLE_ALL;
*OBJECTTYPE_TESTBED = *eventc::OBJECTTYPE_TESTBED;
*OBJECTTYPE_TRAFGEN = *eventc::OBJECTTYPE_TRAFGEN;
*EVENT_HOST_ANY = *eventc::EVENT_HOST_ANY;
*EVENT_NULL = *eventc::EVENT_NULL;
*EVENT_TEST = *eventc::EVENT_TEST;
*EVENT_SCHEDULE = *eventc::EVENT_SCHEDULE;
*EVENT_TRAFGEN_START = *eventc::EVENT_TRAFGEN_START;
*EVENT_TRAFGEN_STOP = *eventc::EVENT_TRAFGEN_STOP;
500 501 502 503 504

my %__callback_data_list_hash;
tie %__callback_data_list_hash,"callback_data", $eventc::callback_data_list;
$callback_data_list= \%__callback_data_list_hash;
bless $callback_data_list, callback_data;
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
*event_string_buffer = *eventc::event_string_buffer;
1;
#
# CODE PAST THIS POINT WAS NOT AUTOMATICALLY GENERATED BY SWIG
#
# For now, this has to get cat'ed onto the end of event.pm, since it
# doesn't seem possible to get SWIG to just pass it through into the
# output file
#

#
# Stash away the given callback and data, and call the C event_subscribe
# function that uses a stub callback
#
sub event_subscribe($$$;$) {
	my ($handle,$function,$tuple,$data) = @_;
	$event::callback = $function;
	$event::callback_data = $data;
	return stub_event_subscribe($handle,$tuple);
}

#
# Clear $callback_ready, call the C event_poll function, and see if the
# C callback got called (as evidenced by $callback_ready getting set) If it
# did, call the perl callback function.
#
sub event_poll($) {
	my $handle = shift;

	my $rv = c_event_poll($handle);
	if ($rv) {
		die "Trouble in event_poll - returned $rv\h";
	}

539 540
	while (my $data = dequeue_callback_data() ) {
		&$event::callback($handle,$data->{callback_notification},
541
				$event::callback_data);
542 543
		event_notification_free($handle,$data->{callback_notification});
		free_callback_data($data);
544 545 546 547 548
	}

	return 0;
}

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
#
# NOTE: The following line will only work if this module is included by
# a file that has already done a 'use lib' to get the path to testbed
# libraries in the INC path. But, since they had to do that to get this
# library anyway, shouldn't be a problem. (Didn't want to have to make
# this a .in file.)
#
use libtestbed;

#
# Conveniece functions - Intended to work like DBQuery* from libdb .
# Much of this code shamlessly ripped off from libdb.pm
#

#
# Warn and send email after a failed event send. First argument is the error
# message to display. The contents of $EventErrorString is also printed.
# 
# usage: EventWarn(char *message)
#
sub EventWarn($) {
	my($message) = $_[0];
	my($text, $progname);

	#
	# Must taint check $PROGRAM_NAME cause it comes from outside. Silly!
	#
	if ($PROGRAM_NAME =~ /^([-\w.\/]+)$/) {
		$progname = $1;
	} else {
		$progname = "Tainted";
	}

	$text = "$message - In $progname\n" .
		"$EventErrorString\n";

	print STDERR "*** $text";

	libtestbed::SENDMAIL($TBOPS, "EventError - $message", $text);
}

#
# Same as above, but die after the warning.
# 
# usage: EventFatal(char *message);
#
sub EventFatal($) {
	my($message) = $_[0];

	EventWarn($message);

	die("\n");
}


#
# Conveniece function - Intended to work like DBQueryFatal from libdb
#
sub EventSendFatal(@) {
	my @tuple = @_;
    
	my $result = EventSend(@tuple);

	if (!$result) {
		EventFatal("Event Send failed");
	}

	return $result;
}

#
# Conveniece function - Intended to work like DBQueryWarn from libdb
#
sub EventSendWarn(@) {
	my @tuple = @_;
    
	my $result = EventSend(@tuple);

	if (!$result) {
		EventFatal("Event Send failed");
	}

	return $result;
}

sub EventSend(@) {
	my %tuple_values = @_;

637 638 639 640 641 642 643 644 645 646 647 648 649
	#
	# Only connect on the first call - thereafter, just use the existing
	# handle. The handle gets disconnected in the END block below
	#
	if (!$event::EventSendHandle) {
		my $URL = "elvin://" . TB_BOSSNODE;

		$event::EventSendHandle = event_register($URL,0);
		if (!$event::EventSendHandle) {
			$EventErrorString =
				"Unable to register with the event system";
			return undef;
		}
650 651 652 653 654 655 656 657 658 659 660 661 662
	}

	my $tuple = address_tuple_alloc();
	if (!$tuple) {
		$EventErrorString = "Unable to allocate an address tuple";
		return undef;
	}

	#
	# Set the values the user requested
	#
	%$tuple = %tuple_values;

663 664
	my $notification = event_notification_alloc($event::EventSendHandle,
		$tuple);
665 666 667 668 669
	if (!$notification) {
		$EventErrorString = "Could not allocate notification";
		return undef;
	}

670
	if (!event_notify($event::EventSendHandle, $notification)) {
671 672 673 674
		$EventErrorString = "Could not send event notification";
		return undef;
	}

675
	event_notification_free($event::EventSendHandle, $notification);
676 677 678 679 680
	address_tuple_free($tuple);

	return 1;
}

681 682 683 684 685 686 687 688 689 690
#
# When we exit, unregister with the event system if we're connected
#
END {
    	if ($event::EventSendHandle) {
		if (event_unregister($event::EventSendHandle) == 0) {
			warn "Could not unregister with event system";
		}
	}
}
691 692 693

push @EXPORT, qw(event_subscribe event_poll EventSend EventSendFatal
	EventSendWarn);
694 695
1;