Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of research:~rosejn/gosim

  • Loading branch information...
commit 78a62eb0e95080825edcffaa4a2cd684dd67e8ae 2 parents 600bac6 + 9dc788d
Cyrus Hall authored
View
26 Rakefile
@@ -1,29 +1,27 @@
require 'rubygems'
require 'rake'
+require 'rake/clean'
require 'rake/testtask'
require 'rake/rdoctask'
+require 'tools/rakehelp'
+require 'fileutils'
+include FileUtils
-PKG_VERSION = "0.2"
+PKG_VERSION = "0.3"
$VERBOSE = nil
-TEST_CHANGES_SINCE = Time.now - 600 # Recent tests = changed in last 10 minutes
desc "Run all the unit tests"
-task :default => :test
+task :default => [:event_queue, :test]
-# Run all the unit tests
-desc "Run the unit tests in test"
-Rake::TestTask.new(:test) { |t|
-# t.loader = :testrb
- t.libs << "test"
- t.test_files = FileList['test/*_test.rb']
- t.verbose = false
-}
+setup_tests
+setup_clean(["ext/event_queue/*.{so,o}", "ext/event_queue/Makefile", "pkg"])
+setup_extension("event_queue", "event_queue")
# Generate the RDoc documentation
Rake::RDocTask.new(:doc) { |rdoc|
rdoc.main = 'README'
- rdoc.rdoc_files.include('lib/**/*.rb', 'README')
+ rdoc.rdoc_files.include('lib/**/*.rb', 'ext/**/*', 'README')
rdoc.rdoc_files.include('GPL', 'COPYING')
rdoc.rdoc_dir = 'docs/api'
rdoc.title = "GoSim -- Discrete Event Simulation System"
@@ -40,9 +38,11 @@ spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.summary = "Flexible, discrete event simulation system."
s.description = "A discrete event simulator for exploring ideas."
+ s.required_ruby_version = '>= 1.8.4'
s.files = FileList["{test,lib,docs,examples}/**/*"].to_a
s.files += ["Rakefile", "README", "COPYING", "GPL" ]
+ s.test_files = Dir.glob('test/test_*.rb')
s.require_path = "lib"
s.autorequire = "gosim"
s.has_rdoc = true
@@ -60,6 +60,6 @@ Rake::GemPackageTask.new(spec) do |pkg|
end
desc 'Install the gem globally (requires sudo)'
-task :install => :package do |t|
+task :install => [:event_queue, :package] do |t|
`gem install pkg/gosim-#{PKG_VERSION}.gem`
end
View
146 ext/event_queue/Makefile
@@ -0,0 +1,146 @@
+
+SHELL = /bin/sh
+
+#### Start of system configuration section. ####
+
+srcdir = .
+topdir = /usr/lib/ruby/1.8/i486-linux
+hdrdir = $(topdir)
+VPATH = $(srcdir):$(topdir):$(hdrdir)
+prefix = $(DESTDIR)/usr
+exec_prefix = $(prefix)
+sitedir = $(DESTDIR)/usr/local/lib/site_ruby
+rubylibdir = $(libdir)/ruby/$(ruby_version)
+docdir = $(datarootdir)/doc/$(PACKAGE)
+dvidir = $(docdir)
+datarootdir = $(prefix)/share
+archdir = $(rubylibdir)/$(arch)
+sbindir = $(exec_prefix)/sbin
+psdir = $(docdir)
+localedir = $(datarootdir)/locale
+htmldir = $(docdir)
+datadir = $(prefix)/share
+includedir = $(prefix)/include
+infodir = $(datarootdir)/info
+sysconfdir = $(DESTDIR)/etc
+mandir = $(datadir)/man
+libdir = $(exec_prefix)/lib
+sharedstatedir = $(prefix)/com
+oldincludedir = $(DESTDIR)/usr/include
+pdfdir = $(docdir)
+sitearchdir = $(sitelibdir)/$(sitearch)
+bindir = $(exec_prefix)/bin
+localstatedir = $(DESTDIR)/var
+sitelibdir = $(sitedir)/$(ruby_version)
+libexecdir = $(exec_prefix)/libexec
+
+CC = gcc
+LIBRUBY = $(LIBRUBY_SO)
+LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
+LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
+LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
+
+CFLAGS = -fPIC -Wall -g -fno-strict-aliasing -O2 -fPIC
+CPPFLAGS = -I. -I$(topdir) -I$(hdrdir) -I$(srcdir)
+CXXFLAGS = $(CFLAGS)
+DLDFLAGS =
+LDSHARED = $(CC) -shared
+AR = ar
+EXEEXT =
+
+RUBY_INSTALL_NAME = ruby1.8
+RUBY_SO_NAME = ruby1.8
+arch = i486-linux
+sitearch = i486-linux
+ruby_version = 1.8
+ruby = /usr/bin/ruby1.8
+RUBY = $(ruby)
+RM = rm -f
+MAKEDIRS = mkdir -p
+INSTALL = /usr/bin/install -c
+INSTALL_PROG = $(INSTALL) -m 0755
+INSTALL_DATA = $(INSTALL) -m 644
+COPY = cp
+
+#### End of system configuration section. ####
+
+preload =
+
+libpath = $(libdir)
+LIBPATH = -L"$(libdir)"
+DEFFILE =
+
+CLEANFILES =
+DISTCLEANFILES =
+
+extout =
+extout_prefix =
+target_prefix =
+LOCAL_LIBS =
+LIBS = $(LIBRUBYARG_SHARED) -lpthread -ldl -lcrypt -lm -lc
+SRCS = fib.c event_queue.c
+OBJS = fib.o event_queue.o
+TARGET = event_queue
+DLLIB = $(TARGET).so
+STATIC_LIB =
+
+RUBYCOMMONDIR = $(sitedir)$(target_prefix)
+RUBYLIBDIR = $(sitelibdir)$(target_prefix)
+RUBYARCHDIR = $(sitearchdir)$(target_prefix)
+
+TARGET_SO = $(DLLIB)
+CLEANLIBS = $(TARGET).so $(TARGET).il? $(TARGET).tds $(TARGET).map
+CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak
+
+all: $(DLLIB)
+static: $(STATIC_LIB)
+
+clean:
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
+
+distclean: clean
+ @-$(RM) Makefile extconf.h conftest.* mkmf.log
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
+
+realclean: distclean
+install: install-so install-rb
+
+install-so: $(RUBYARCHDIR)
+install-so: $(RUBYARCHDIR)/$(DLLIB)
+$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
+install-rb: pre-install-rb install-rb-default
+install-rb-default: pre-install-rb-default
+pre-install-rb: Makefile
+pre-install-rb-default: Makefile
+$(RUBYARCHDIR):
+ $(MAKEDIRS) $@
+
+site-install: site-install-so site-install-rb
+site-install-so: install-so
+site-install-rb: install-rb
+
+.SUFFIXES: .c .m .cc .cxx .cpp .C .o
+
+.cc.o:
+ $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
+
+.cxx.o:
+ $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
+
+.cpp.o:
+ $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
+
+.C.o:
+ $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
+
+.c.o:
+ $(CC) $(CFLAGS) $(CPPFLAGS) -c $<
+
+$(DLLIB): $(OBJS)
+ @-$(RM) $@
+ $(LDSHARED) $(DLDFLAGS) $(LIBPATH) -o $@ $(OBJS) $(LOCAL_LIBS) $(LIBS)
+
+
+
+$(OBJS): ruby.h defines.h
View
144 ext/event_queue/event_queue.c
@@ -0,0 +1,144 @@
+#include <ruby.h>
+#include <intern.h>
+#include <stdio.h>
+
+#include "fib.h"
+
+typedef struct Event {
+ VALUE method;
+ VALUE receiver;
+ VALUE data;
+ VALUE id;
+ long time;
+} Event;
+
+int compare_events(void *x, void *y)
+{
+ long a, b;
+ a = ((Event*)x)->time;
+ b = ((Event*)y)->time;
+
+ if (a < b)
+ return -1;
+ if (a == b)
+ return 0;
+ return 1;
+}
+
+static struct fibheap *event_queue = NULL;
+static ID time_id = Qnil;
+static ID send_id = Qnil;
+static ID data_hash_id = Qnil;
+static ID entities_id = Qnil;
+static ID object_id = Qnil;
+VALUE rb_gosim;
+VALUE rb_simulation;
+
+static VALUE reset_event_queue(
+ VALUE self)
+{
+ if(event_queue != NULL)
+ fh_deleteheap(event_queue);
+
+ event_queue = fh_makeheap();
+ fh_setcmp(event_queue, compare_events);
+
+ return Qnil;
+}
+
+static VALUE schedule_event(
+ VALUE self,
+ VALUE event_id,
+ VALUE dest_sid,
+ VALUE time,
+ VALUE data)
+{
+ Event *event;
+
+ VALUE entities = rb_ivar_get(self, entities_id);
+ VALUE receiver = rb_hash_aref(entities, dest_sid);
+ VALUE data_hash = rb_cvar_get(rb_simulation, data_hash_id);
+
+ if(NIL_P(receiver) || !rb_respond_to(receiver, SYM2ID(event_id)))
+ {
+ rb_raise(rb_eRuntimeError,
+ "Cannot schedule %s.%s with sid=(%d), invalid method name!",
+ rb_class2name(CLASS_OF(receiver)),
+ rb_id2name(SYM2ID(event_id)),
+ FIX2INT(dest_sid));
+ }
+
+ event = malloc(sizeof(Event));
+ event->method = SYM2ID(event_id);
+ event->receiver = receiver;
+ event->data = data;
+ event->id = rb_funcall(data, object_id, 0);
+
+ event->time = NUM2LONG(time) + NUM2LONG(rb_ivar_get(self, time_id));
+ fh_insert(event_queue, (void *)event);
+
+ // Store a reference to the data in the hash so it doesn't get garbage
+ // collected.
+ rb_hash_aset(data_hash, event->id, data);
+
+ return Qnil;
+}
+
+static VALUE run_main_loop(
+ VALUE self,
+ VALUE end_time)
+{
+ VALUE running = rb_ivar_get(self, rb_intern("@running"));
+ VALUE data_hash = rb_cvar_get(rb_simulation, data_hash_id);
+
+ Event *cur_event = fh_min(event_queue);
+ long end = NUM2LONG(end_time);
+
+ while((running == Qtrue) && (cur_event != NULL) && (cur_event->time <= end))
+ {
+ cur_event = fh_extractmin(event_queue);
+ rb_ivar_set(self, time_id, LONG2NUM(cur_event->time));
+
+ /*
+ printf("%ld: %s.%s\n",
+ cur_event->time,
+ rb_class2name(CLASS_OF(cur_event->receiver)),
+ rb_id2name(cur_event->method));
+ */
+
+ //rb_funcall(cur_event->receiver, send_id, 2, cur_event->method, cur_event->data);
+ rb_funcall(cur_event->receiver, cur_event->method, 1, cur_event->data);
+
+ rb_hash_delete(data_hash, cur_event->id);
+ free(cur_event);
+
+ cur_event = fh_min(event_queue);
+ }
+
+ return Qnil;
+}
+
+static VALUE queue_size(
+ VALUE self)
+{
+ return INT2NUM(fh_size(event_queue));
+}
+
+void Init_event_queue()
+{
+ rb_gosim = rb_define_module("GoSim");
+ rb_simulation = rb_define_class_under(rb_gosim, "Simulation", rb_cObject);
+
+ time_id = rb_intern("@time");
+ send_id = rb_intern("send");
+ data_hash_id = rb_intern("@data_hash");
+ entities_id = rb_intern("@entities");
+ object_id = rb_intern("object_id");
+
+ rb_cvar_set(rb_simulation, data_hash_id, rb_hash_new(), Qfalse);
+
+ rb_define_method(rb_simulation, "schedule_event", schedule_event, 4);
+ rb_define_method(rb_simulation, "reset_event_queue", reset_event_queue, 0);
+ rb_define_method(rb_simulation, "run_main_loop", run_main_loop, 1);
+ rb_define_method(rb_simulation, "queue_size", queue_size, 0);
+}
View
12 ext/event_queue/extconf.rb
@@ -0,0 +1,12 @@
+# Loads mkmf which is used to make makefiles for Ruby extensions
+require 'mkmf'
+
+# Give it a name
+extension_name = 'event_queue'
+
+# The destination
+dir_config(extension_name)
+
+# Do the work
+create_makefile(extension_name)
+
View
702 ext/event_queue/fib.c
@@ -0,0 +1,702 @@
+/*-
+ * Copyright 1997-2003 John-Mark Gurney.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: fib.c,v 1.31 2003/01/14 10:11:30 jmg Exp $
+ *
+ */
+
+#include <fib.h>
+#include <fibpriv.h>
+
+#include <limits.h>
+#include <stdlib.h>
+
+#define swap(type, a, b) \
+ do { \
+ type c; \
+ c = a; \
+ a = b; \
+ b = c; \
+ } while (0) \
+
+#define INT_BITS (sizeof(int) * 8)
+static inline int
+ceillog2(unsigned int a)
+{
+ int oa;
+ int i;
+ int b;
+
+ oa = a;
+ b = INT_BITS / 2;
+ i = 0;
+ while (b) {
+ i = (i << 1);
+ if (a >= (1 << b)) {
+ a /= (1 << b);
+ i = i | 1;
+ } else
+ a &= (1 << b) - 1;
+ b /= 2;
+ }
+ if ((1 << i) == oa)
+ return i;
+ else
+ return i + 1;
+}
+
+/*
+ * Private Heap Functions
+ */
+static void
+fh_deleteel(struct fibheap *h, struct fibheap_el *x)
+{
+ void *data;
+ int key;
+
+ data = x->fhe_data;
+ key = x->fhe_key;
+
+ if (!h->fh_keys)
+ fh_replacedata(h, x, h->fh_neginf);
+ else
+ fh_replacekey(h, x, INT_MIN);
+ if (fh_extractminel(h) != x) {
+ /*
+ * XXX - This should never happen as fh_replace should set it
+ * to min.
+ */
+ abort();
+ }
+
+ x->fhe_data = data;
+ x->fhe_key = key;
+}
+
+static void
+fh_initheap(struct fibheap *new)
+{
+ new->fh_cmp_fnct = NULL;
+ new->fh_neginf = NULL;
+ new->fh_n = 0;
+ new->fh_Dl = -1;
+ new->fh_cons = NULL;
+ new->fh_min = NULL;
+ new->fh_root = NULL;
+ new->fh_keys = 0;
+#ifdef FH_STATS
+ new->fh_maxn = 0;
+ new->fh_ninserts = 0;
+ new->fh_nextracts = 0;
+#endif
+}
+
+static void
+fh_destroyheap(struct fibheap *h)
+{
+ h->fh_cmp_fnct = NULL;
+ h->fh_neginf = NULL;
+ if (h->fh_cons != NULL)
+ free(h->fh_cons);
+ h->fh_cons = NULL;
+ free(h);
+}
+
+/*
+ * Public Heap Functions
+ */
+struct fibheap *
+fh_makekeyheap()
+{
+ struct fibheap *n;
+
+ if ((n = malloc(sizeof *n)) == NULL)
+ return NULL;
+
+ fh_initheap(n);
+ n->fh_keys = 1;
+
+ return n;
+}
+
+struct fibheap *
+fh_makeheap()
+{
+ struct fibheap *n;
+
+ if ((n = malloc(sizeof *n)) == NULL)
+ return NULL;
+
+ fh_initheap(n);
+
+ return n;
+}
+
+int
+fh_size(struct fibheap *h)
+{
+ return h->fh_n;
+}
+
+voidcmp
+fh_setcmp(struct fibheap *h, voidcmp fnct)
+{
+ voidcmp oldfnct;
+
+ oldfnct = h->fh_cmp_fnct;
+ h->fh_cmp_fnct = fnct;
+
+ return oldfnct;
+}
+
+void *
+fh_setneginf(struct fibheap *h, void *data)
+{
+ void *old;
+
+ old = h->fh_neginf;
+ h->fh_neginf = data;
+
+ return old;
+}
+
+struct fibheap *
+fh_union(struct fibheap *ha, struct fibheap *hb)
+{
+ struct fibheap_el *x;
+
+ if (ha->fh_root == NULL || hb->fh_root == NULL) {
+ /* either one or both are empty */
+ if (ha->fh_root == NULL) {
+ fh_destroyheap(ha);
+ return hb;
+ } else {
+ fh_destroyheap(hb);
+ return ha;
+ }
+ }
+ ha->fh_root->fhe_left->fhe_right = hb->fh_root;
+ hb->fh_root->fhe_left->fhe_right = ha->fh_root;
+ x = ha->fh_root->fhe_left;
+ ha->fh_root->fhe_left = hb->fh_root->fhe_left;
+ hb->fh_root->fhe_left = x;
+ ha->fh_n += hb->fh_n;
+ /*
+ * we probably should also keep stats on number of unions
+ */
+
+ /* set fh_min if necessary */
+ if (fh_compare(ha, hb->fh_min, ha->fh_min) < 0)
+ ha->fh_min = hb->fh_min;
+
+ fh_destroyheap(hb);
+ return ha;
+}
+
+void
+fh_deleteheap(struct fibheap *h)
+{
+ /*
+ * We could do this even faster by walking each binomial tree, but
+ * this is simpler to code.
+ */
+ while (h->fh_min != NULL)
+ fhe_destroy(fh_extractminel(h));
+
+ fh_destroyheap(h);
+}
+
+/*
+ * Public Key Heap Functions
+ */
+struct fibheap_el *
+fh_insertkey(struct fibheap *h, int key, void *data)
+{
+ struct fibheap_el *x;
+
+ if ((x = fhe_newelem()) == NULL)
+ return NULL;
+
+ /* just insert on root list, and make sure it's not the new min */
+ x->fhe_data = data;
+ x->fhe_key = key;
+
+ fh_insertel(h, x);
+
+ return x;
+}
+
+int
+fh_minkey(struct fibheap *h)
+{
+ if (h->fh_min == NULL)
+ return INT_MIN;
+ return h->fh_min->fhe_key;
+}
+
+int
+fh_replacekey(struct fibheap *h, struct fibheap_el *x, int key)
+{
+ int ret;
+
+ ret = x->fhe_key;
+ (void)fh_replacekeydata(h, x, key, x->fhe_data);
+
+ return ret;
+}
+
+void *
+fh_replacekeydata(struct fibheap *h, struct fibheap_el *x, int key, void *data)
+{
+ void *odata;
+ int okey;
+ struct fibheap_el *y;
+ int r;
+
+ odata = x->fhe_data;
+ okey = x->fhe_key;
+
+ /*
+ * we can increase a key by deleting and reinserting, that
+ * requires O(lgn) time.
+ */
+ if ((r = fh_comparedata(h, key, data, x)) > 0) {
+ /* XXX - bad code! */
+ abort();
+ fh_deleteel(h, x);
+
+ x->fhe_data = data;
+ x->fhe_key = key;
+
+ fh_insertel(h, x);
+
+ return odata;
+ }
+
+ x->fhe_data = data;
+ x->fhe_key = key;
+
+ /* because they are equal, we don't have to do anything */
+ if (r == 0)
+ return odata;
+
+ y = x->fhe_p;
+
+ if (h->fh_keys && okey == key)
+ return odata;
+
+ if (y != NULL && fh_compare(h, x, y) <= 0) {
+ fh_cut(h, x, y);
+ fh_cascading_cut(h, y);
+ }
+
+ /*
+ * the = is so that the call from fh_delete will delete the proper
+ * element.
+ */
+ if (fh_compare(h, x, h->fh_min) <= 0)
+ h->fh_min = x;
+
+ return odata;
+}
+
+/*
+ * Public void * Heap Functions
+ */
+/*
+ * this will return these values:
+ * NULL failed for some reason
+ * ptr token to use for manipulation of data
+ */
+struct fibheap_el *
+fh_insert(struct fibheap *h, void *data)
+{
+ struct fibheap_el *x;
+
+ if ((x = fhe_newelem()) == NULL)
+ return NULL;
+
+ /* just insert on root list, and make sure it's not the new min */
+ x->fhe_data = data;
+
+ fh_insertel(h, x);
+
+ return x;
+}
+
+void *
+fh_min(struct fibheap *h)
+{
+ if (h->fh_min == NULL)
+ return NULL;
+ return h->fh_min->fhe_data;
+}
+
+void *
+fh_extractmin(struct fibheap *h)
+{
+ struct fibheap_el *z;
+ void *ret;
+
+ ret = NULL;
+
+ if (h->fh_min != NULL) {
+ z = fh_extractminel(h);
+ ret = z->fhe_data;
+#ifndef NO_FREE
+ fhe_destroy(z);
+#endif
+
+ }
+
+ return ret;
+}
+
+void *
+fh_replacedata(struct fibheap *h, struct fibheap_el *x, void *data)
+{
+ return fh_replacekeydata(h, x, x->fhe_key, data);
+}
+
+void *
+fh_delete(struct fibheap *h, struct fibheap_el *x)
+{
+ void *k;
+
+ k = x->fhe_data;
+ if (!h->fh_keys)
+ fh_replacedata(h, x, h->fh_neginf);
+ else
+ fh_replacekey(h, x, INT_MIN);
+ fh_extractmin(h);
+
+ return k;
+}
+
+/*
+ * Statistics Functions
+ */
+#ifdef FH_STATS
+int
+fh_maxn(struct fibheap *h)
+{
+ return h->fh_maxn;
+}
+
+int
+fh_ninserts(struct fibheap *h)
+{
+ return h->fh_ninserts;
+}
+
+int
+fh_nextracts(struct fibheap *h)
+{
+ return h->fh_nextracts;
+}
+#endif
+
+/*
+ * begin of private element fuctions
+ */
+static struct fibheap_el *
+fh_extractminel(struct fibheap *h)
+{
+ struct fibheap_el *ret;
+ struct fibheap_el *x, *y, *orig;
+
+ ret = h->fh_min;
+
+ orig = NULL;
+ /* put all the children on the root list */
+ /* for true consistancy, we should use fhe_remove */
+ for(x = ret->fhe_child; x != orig && x != NULL;) {
+ if (orig == NULL)
+ orig = x;
+ y = x->fhe_right;
+ x->fhe_p = NULL;
+ fh_insertrootlist(h, x);
+ x = y;
+ }
+ /* remove minimum from root list */
+ fh_removerootlist(h, ret);
+ h->fh_n--;
+
+ /* if we aren't empty, consolidate the heap */
+ if (h->fh_n == 0)
+ h->fh_min = NULL;
+ else {
+ h->fh_min = ret->fhe_right;
+ fh_consolidate(h);
+ }
+
+#ifdef FH_STATS
+ h->fh_nextracts++;
+#endif
+
+ return ret;
+}
+
+static void
+fh_insertrootlist(struct fibheap *h, struct fibheap_el *x)
+{
+ if (h->fh_root == NULL) {
+ h->fh_root = x;
+ x->fhe_left = x;
+ x->fhe_right = x;
+ return;
+ }
+
+ fhe_insertafter(h->fh_root, x);
+}
+
+static void
+fh_removerootlist(struct fibheap *h, struct fibheap_el *x)
+{
+ if (x->fhe_left == x)
+ h->fh_root = NULL;
+ else
+ h->fh_root = fhe_remove(x);
+}
+
+static void
+fh_consolidate(struct fibheap *h)
+{
+ struct fibheap_el **a;
+ struct fibheap_el *w;
+ struct fibheap_el *y;
+ struct fibheap_el *x;
+ int i;
+ int d;
+ int D;
+
+ fh_checkcons(h);
+
+ /* assign a the value of h->fh_cons so I don't have to rewrite code */
+ D = h->fh_Dl + 1;
+ a = h->fh_cons;
+
+ for (i = 0; i < D; i++)
+ a[i] = NULL;
+
+ while ((w = h->fh_root) != NULL) {
+ x = w;
+ fh_removerootlist(h, w);
+ d = x->fhe_degree;
+ /* XXX - assert that d < D */
+ while(a[d] != NULL) {
+ y = a[d];
+ if (fh_compare(h, x, y) > 0)
+ swap(struct fibheap_el *, x, y);
+ fh_heaplink(h, y, x);
+ a[d] = NULL;
+ d++;
+ }
+ a[d] = x;
+ }
+ h->fh_min = NULL;
+ for (i = 0; i < D; i++)
+ if (a[i] != NULL) {
+ fh_insertrootlist(h, a[i]);
+ if (h->fh_min == NULL || fh_compare(h, a[i],
+ h->fh_min) < 0)
+ h->fh_min = a[i];
+ }
+}
+
+static void
+fh_heaplink(struct fibheap *h, struct fibheap_el *y, struct fibheap_el *x)
+{
+ /* make y a child of x */
+ if (x->fhe_child == NULL)
+ x->fhe_child = y;
+ else
+ fhe_insertbefore(x->fhe_child, y);
+ y->fhe_p = x;
+ x->fhe_degree++;
+ y->fhe_mark = 0;
+}
+
+static void
+fh_cut(struct fibheap *h, struct fibheap_el *x, struct fibheap_el *y)
+{
+ fhe_remove(x);
+ y->fhe_degree--;
+ fh_insertrootlist(h, x);
+ x->fhe_p = NULL;
+ x->fhe_mark = 0;
+}
+
+static void
+fh_cascading_cut(struct fibheap *h, struct fibheap_el *y)
+{
+ struct fibheap_el *z;
+
+ while ((z = y->fhe_p) != NULL) {
+ if (y->fhe_mark == 0) {
+ y->fhe_mark = 1;
+ return;
+ } else {
+ fh_cut(h, y, z);
+ y = z;
+ }
+ }
+}
+
+/*
+ * begining of handling elements of fibheap
+ */
+static struct fibheap_el *
+fhe_newelem()
+{
+ struct fibheap_el *e;
+
+ if ((e = malloc(sizeof *e)) == NULL)
+ return NULL;
+
+ fhe_initelem(e);
+
+ return e;
+}
+
+static void
+fhe_initelem(struct fibheap_el *e)
+{
+ e->fhe_degree = 0;
+ e->fhe_mark = 0;
+ e->fhe_p = NULL;
+ e->fhe_child = NULL;
+ e->fhe_left = e;
+ e->fhe_right = e;
+ e->fhe_data = NULL;
+}
+
+static void
+fhe_insertafter(struct fibheap_el *a, struct fibheap_el *b)
+{
+ if (a == a->fhe_right) {
+ a->fhe_right = b;
+ a->fhe_left = b;
+ b->fhe_right = a;
+ b->fhe_left = a;
+ } else {
+ b->fhe_right = a->fhe_right;
+ a->fhe_right->fhe_left = b;
+ a->fhe_right = b;
+ b->fhe_left = a;
+ }
+}
+
+static inline void
+fhe_insertbefore(struct fibheap_el *a, struct fibheap_el *b)
+{
+ fhe_insertafter(a->fhe_left, b);
+}
+
+static struct fibheap_el *
+fhe_remove(struct fibheap_el *x)
+{
+ struct fibheap_el *ret;
+
+ if (x == x->fhe_left)
+ ret = NULL;
+ else
+ ret = x->fhe_left;
+
+ /* fix the parent pointer */
+ if (x->fhe_p != NULL && x->fhe_p->fhe_child == x)
+ x->fhe_p->fhe_child = ret;
+
+ x->fhe_right->fhe_left = x->fhe_left;
+ x->fhe_left->fhe_right = x->fhe_right;
+
+ /* clear out hanging pointers */
+ x->fhe_p = NULL;
+ x->fhe_left = x;
+ x->fhe_right = x;
+
+ return ret;
+}
+
+static void
+fh_checkcons(struct fibheap *h)
+{
+ int oDl;
+
+ /* make sure we have enough memory allocated to "reorganize" */
+ if (h->fh_Dl == -1 || h->fh_n > (1 << h->fh_Dl)) {
+ oDl = h->fh_Dl;
+ if ((h->fh_Dl = ceillog2(h->fh_n) + 1) < 8)
+ h->fh_Dl = 8;
+ if (oDl != h->fh_Dl)
+ h->fh_cons = (struct fibheap_el **)realloc(h->fh_cons,
+ sizeof *h->fh_cons * (h->fh_Dl + 1));
+ if (h->fh_cons == NULL)
+ abort();
+ }
+}
+
+static int
+fh_compare(struct fibheap *h, struct fibheap_el *a, struct fibheap_el *b)
+{
+ if (h->fh_keys) {
+ if (a->fhe_key < b->fhe_key)
+ return -1;
+ if (a->fhe_key == b->fhe_key)
+ return 0;
+ return 1;
+ } else
+ return h->fh_cmp_fnct(a->fhe_data, b->fhe_data);
+}
+
+static int
+fh_comparedata(struct fibheap *h, int key, void *data, struct fibheap_el *b)
+{
+ struct fibheap_el a;
+
+ a.fhe_key = key;
+ a.fhe_data = data;
+
+ return fh_compare(h, &a, b);
+}
+
+static void
+fh_insertel(struct fibheap *h, struct fibheap_el *x)
+{
+ fh_insertrootlist(h, x);
+
+ if (h->fh_min == NULL || (h->fh_keys ? x->fhe_key < h->fh_min->fhe_key
+ : h->fh_cmp_fnct(x->fhe_data, h->fh_min->fhe_data) < 0))
+ h->fh_min = x;
+
+ h->fh_n++;
+
+#ifdef FH_STATS
+ if (h->fh_n > h->fh_maxn)
+ h->fh_maxn = h->fh_n;
+ h->fh_ninserts++;
+#endif
+
+}
View
65 ext/event_queue/fib.h
@@ -0,0 +1,65 @@
+/*-
+ * Copyright 1997, 1998-2003 John-Mark Gurney.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: fib.h,v 1.10 2003/01/14 10:11:30 jmg Exp $
+ *
+ */
+
+#ifndef _FIB_H_
+#define _FIB_H_
+
+struct fibheap;
+struct fibheap_el;
+typedef int (*voidcmp)(void *, void *);
+
+/* functions for key heaps */
+struct fibheap *fh_makekeyheap(void);
+struct fibheap_el *fh_insertkey(struct fibheap *, int, void *);
+int fh_minkey(struct fibheap *);
+int fh_replacekey(struct fibheap *, struct fibheap_el *, int);
+void *fh_replacekeydata(struct fibheap *, struct fibheap_el *, int, void *);
+
+/* functions for void * heaps */
+struct fibheap *fh_makeheap(void);
+voidcmp fh_setcmp(struct fibheap *, voidcmp);
+void *fh_setneginf(struct fibheap *, void *);
+struct fibheap_el *fh_insert(struct fibheap *, void *);
+
+/* shared functions */
+void *fh_extractmin(struct fibheap *);
+void *fh_min(struct fibheap *);
+int fh_size(struct fibheap *h);
+void *fh_replacedata(struct fibheap *, struct fibheap_el *, void *);
+void *fh_delete(struct fibheap *, struct fibheap_el *);
+void fh_deleteheap(struct fibheap *);
+struct fibheap *fh_union(struct fibheap *, struct fibheap *);
+
+#ifdef FH_STATS
+int fh_maxn(struct fibheap *);
+int fh_ninserts(struct fibheap *);
+int fh_nextracts(struct fibheap *);
+#endif
+
+#endif /* _FIB_H_ */
View
98 ext/event_queue/fibpriv.h
@@ -0,0 +1,98 @@
+/*-
+ * Copyright 1997, 1999-2003 John-Mark Gurney.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: fibpriv.h,v 1.12 2003/01/14 10:11:30 jmg Exp $
+ *
+ */
+
+#ifndef _FIBPRIV_H_
+#define _FIBPRIV_H_
+
+struct fibheap_el;
+
+/*
+ * global heap operations
+ */
+struct fibheap {
+ int (*fh_cmp_fnct)(void *, void *);
+ int fh_n;
+ int fh_Dl;
+ struct fibheap_el **fh_cons;
+ struct fibheap_el *fh_min;
+ struct fibheap_el *fh_root;
+ void *fh_neginf;
+ int fh_keys : 1;
+#ifdef FH_STATS
+ int fh_maxn;
+ int fh_ninserts;
+ int fh_nextracts;
+#endif
+};
+
+static void fh_initheap(struct fibheap *);
+static void fh_insertrootlist(struct fibheap *, struct fibheap_el *);
+static void fh_removerootlist(struct fibheap *, struct fibheap_el *);
+static void fh_consolidate(struct fibheap *);
+static void fh_heaplink(struct fibheap *h, struct fibheap_el *y,
+ struct fibheap_el *x);
+static void fh_cut(struct fibheap *, struct fibheap_el *, struct fibheap_el *);
+static void fh_cascading_cut(struct fibheap *, struct fibheap_el *);
+static struct fibheap_el *fh_extractminel(struct fibheap *);
+static void fh_checkcons(struct fibheap *h);
+static void fh_destroyheap(struct fibheap *h);
+static int fh_compare(struct fibheap *h, struct fibheap_el *a,
+ struct fibheap_el *b);
+static int fh_comparedata(struct fibheap *h, int key, void *data,
+ struct fibheap_el *b);
+static void fh_insertel(struct fibheap *h, struct fibheap_el *x);
+static void fh_deleteel(struct fibheap *h, struct fibheap_el *x);
+
+/*
+ * specific node operations
+ */
+struct fibheap_el {
+ int fhe_degree;
+ int fhe_mark;
+ struct fibheap_el *fhe_p;
+ struct fibheap_el *fhe_child;
+ struct fibheap_el *fhe_left;
+ struct fibheap_el *fhe_right;
+ int fhe_key;
+ void *fhe_data;
+};
+
+static struct fibheap_el *fhe_newelem(void);
+static void fhe_initelem(struct fibheap_el *);
+static void fhe_insertafter(struct fibheap_el *a, struct fibheap_el *b);
+static inline void fhe_insertbefore(struct fibheap_el *a, struct fibheap_el *b);
+static struct fibheap_el *fhe_remove(struct fibheap_el *a);
+#define fhe_destroy(x) free((x))
+
+/*
+ * general functions
+ */
+static inline int ceillog2(unsigned int a);
+
+#endif /* _FIBPRIV_H_ */
View
21 lib/gosim.rb
@@ -1,11 +1,19 @@
+$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
+
+require 'rational'
require 'logger'
require 'singleton'
require 'observer'
require 'yaml'
require 'zlib'
-require 'pqueue'
-require 'gsl'
+begin
+ #require 'pqueue'
+ require 'event_queue'
+rescue RuntimeError => e
+ warn "event_queue extension not loaded! Loading ruby pqueue instead."
+ require 'pqueue'
+end
module GoSim
MAX_INT = 2**31
@@ -16,12 +24,12 @@ module Base
@@log.level = Logger::FATAL
# So that all derived classes have an easy accessor
- def log(*args)
- @@log.debug(*args)
+ def log(*args, &block)
+ @@log.debug(*args, &block)
end
- def error(*args)
- @@log.fatal(*args)
+ def error(*args, &block)
+ @@log.fatal(*args, &block)
end
# Turn down logging all the way (nice for unit tests etc...)
@@ -36,6 +44,7 @@ def verbose
end
require 'gosim/simulation'
+require 'gosim/defer'
require 'gosim/network'
require 'gosim/event_reader'
require 'gosim/data'
View
120 lib/gosim/defer.rb
@@ -0,0 +1,120 @@
+module GoSim
+ module Net
+
+ class NotCallableError < Exception; end
+ class NotFailureError < Exception; end
+
+ class Failure
+ attr_accessor :result
+
+ def initialize(result)
+ @result = result
+ end
+ end
+
+ # A callback which will be called at some later point.
+ class Deferred
+ PASSTHRU = lambda {|result| return result}
+
+ attr_reader :callbacks, :errbacks
+
+ def initialize
+ @called = false
+ @paused = 0
+ @callbacks = []
+ @errbacks = []
+ end
+
+ def add_callbacks(callback, errback = nil)
+ raise NotCallableError unless is_callable?(callback)
+ @callbacks << callback
+
+ if errback
+ raise NotCallableError unless is_callable?(errback)
+ @errbacks << errback
+ else
+ @errbacks << PASSTHRU
+ end
+
+ run_callbacks if @called
+ end
+
+ def add_callback(callback = nil, &block)
+ if callback
+ add_callbacks(callback)
+ elsif block
+ add_callbacks(block)
+ end
+ end
+
+ def add_errback(errback = nil, &block)
+ if errback
+ add_callbacks(PASSTHRU, errback)
+ elsif block
+ add_callbacks(PASSTHRU, block)
+ end
+ end
+
+ # Stop processing on this Deferred until unpause is called.
+ def pause
+ @paused += 1
+ end
+
+ # Resume processing if any callbacks were added since pause was called.
+ def unpause
+ @paused -= 1
+
+ if @paused > 0
+ return
+ else
+ run_callbacks if @called
+ end
+ end
+
+ def callback(result)
+ @called = true
+ @result = result
+ run_callbacks
+ end
+
+ def errback(result)
+ raise NotFailureError unless is_failure?(result)
+
+ @called = true
+ @result = result
+ run_callbacks
+ end
+
+ # Execute the callbacks in sequence, passing the result of each callback to
+ # the next one until all have been called. If an exception is raised or a
+ # Failure is returned than processing shifts to the errbacks, and likewise if
+ # an errback returns a non Failure result it shifts to the callbacks.
+ def run_callbacks
+ return if @paused > 0
+
+ while @callbacks.any?
+ begin
+ cb = @callbacks.shift
+ eb = @errbacks.shift
+
+ if is_failure?(@result)
+ @result = eb.call(@result)
+ else
+ @result = cb.call(@result)
+ end
+ rescue Exception => exc
+ @result = Failure.new(exc)
+ end
+ end
+ end
+
+ def is_callable?(obj)
+ obj.respond_to?(:call)
+ end
+
+ def is_failure?(obj)
+ obj.is_a?(Failure)
+ end
+ end
+ end
+end
View
193 lib/gosim/network.rb
@@ -1,109 +1,220 @@
module GoSim
module Net
- MEAN_LATENCY = 5
+ LATENCY_MEAN = 100
+ LATENCY_DEV = 50
GSNetworkPacket = Struct.new(:id, :src, :dest, :data)
FailedPacket = Struct.new(:dest, :data)
- LivenessPacket = Struct.new(:alive)
ERROR_NODE_FAILURE = 0
+ class RPCRequest
+ attr_reader :uid, :src, :dest, :method, :args
+
+ @@rpc_counter = 0
+
+ def RPCRequest.next_uid
+ @@rpc_counter += 1
+ end
+
+ def initialize(src, dest, method, args)
+ @src = src
+ @dest = dest
+ @method = method
+ @args = args
+
+ @uid = RPCRequest.next_uid
+ end
+ end
+
+ class RPCResponse
+ attr_reader :uid, :result
+
+ def initialize(uid, result)
+ @uid = uid
+ @result = result
+ end
+ end
+
+ # Add a no-return method to Deferred so it can clear state for methods
+ # without return values.
+ class RPCDeferred < Deferred
+ def initialize(uid = nil)
+ @uid = uid
+
+ super()
+ end
+
+ def no_return
+ Topology.instance.remove_deferred(@uid)
+ end
+ end
+
class Topology < Entity
include Singleton
def initialize()
super()
- GSL::Rng.env_setup
- @rand_gen = GSL::Rng.alloc("mt19937")
- @rand_gen.set(0)
@sim.add_observer(self)
- setup
+ @nodes = {}
+ @rpc_deferreds = {}
end
- def setup(mean_latency = MEAN_LATENCY)
- @mean_latency = mean_latency
- @node_status = {}
-
- return self
+ def set_latency(latency_mean = LATENCY_MEAN, latency_dev = LATENCY_DEV)
+ @latency_mean = latency_mean
+ @latency_dev = latency_dev
end
# Called by simulation when a reset occurs
def update
- log "Resetting Topology..."
+ log {"Resetting Topology..."}
reset
- @node_status = {}
- log "Topology now has sid #{sid}"
+ @nodes = {}
+ log {"Topology now has sid #{sid}"}
+ end
+
+ def register_node(node)
+ @nodes[node.addr] = node
end
- def node_alive(addr, status)
- @node_status[addr] = status
+ def get_node(addr)
+ @nodes[addr]
end
- def send_packet(id, src, receivers, packet)
+ def remove_deferred(uid)
+ @rpc_deferreds.delete(uid)
+ end
+
+ # Simple send packet that is always handled by Node#recv_packet
+ def send_packet(src, receivers, packet)
[*receivers].each do |receiver|
- @sim.schedule_event(:gs_network_packet,
+ @sim.schedule_event(:handle_packet,
@sid,
- @rand_gen.poisson(@mean_latency),
+ rand(@mean_latency) + LATENCY_DEV,
GSNetworkPacket.new(id, src, receiver, packet))
end
end
- def handle_gs_network_packet(packet)
- if @node_status[packet.dest]
+ def recv_packet(packet)
+ if @nodes[packet.dest].alive?
@sim.schedule_event(packet.id, packet.dest, 0, packet.data)
else
- @sim.schedule_event(:failed_packet, packet.src, 0,
+ @sim.schedule_event(:handle_failed_packet, packet.src, 0,
FailedPacket.new(packet.dest, packet.data))
end
end
+
+ # Send an rpc request that gets handled by a specific method on the receiver
+ def rpc_request(src, dest, method, args)
+ request = RPCRequest.new(src, dest, method, args)
+ @sim.schedule_event(:handle_rpc_request,
+ @sid,
+ rand(@mean_latency) + LATENCY_DEV,
+ request)
+
+ deferred = RPCDeferred.new(request.uid)
+ @rpc_deferreds[request.uid] = deferred
+
+ return deferred
+ end
+
+ # Dispatches an RPC request to a specific method, and return a result
+ # unless the method returns nil.
+ def handle_rpc_request(request)
+ #puts "top of request"
+ if @nodes[request.dest].alive?
+ #puts "1 request...#{request.inspect}"
+
+ # If there is no response delete the deferred.
+ # TODO: Maybe we want to signal something to the deferred here also?
+ result = @nodes[request.dest].send(request.method, *request.args)
+
+ @sim.schedule_event(:handle_rpc_response,
+ @sid,
+ rand(@mean_latency) + LATENCY_DEV,
+ RPCResponse.new(request.uid, result))
+ else
+ #puts "2 request..."
+ if @rpc_deferreds.has_key?(request.uid)
+ @rpc_deferreds[request.uid].errback(Failure.new(request))
+ end
+ end
+ end
+
+ def handle_rpc_response(response)
+ #puts "response...#{response}"
+ if @rpc_deferreds.has_key?(response.uid)
+ @rpc_deferreds[response.uid].callback(response.result)
+ end
+ end
+ end
+
+ class RPCInvalidMethodError < Exception; end
+
+ class Peer
+ attr_reader :addr
+
+ def initialize(local_node, remote_addr)
+ @topo = Topology.instance
+
+ @local_node = local_node
+ @remote_node = @topo.get_node(remote_addr)
+ if @remote_node
+ @addr = @remote_node.addr
+ else
+ @addr = nil
+ end
+ end
+
+ def method_missing(method, *args)
+ raise RPCInvalidMethodError.new("#{method} not available on target node!") unless @remote_node.respond_to?(method)
+
+ @topo.rpc_request(@local_node.addr, @remote_node.addr, method, args)
+ end
end
class Node < Entity
- attr_reader :addr, :neighbor_ids
+ attr_reader :addr
- def initialize()
- super()
+ def initialize
+ super
@addr = @sid
@topo = Topology.instance
- @neighbor_ids = []
@alive = true
- @topo.node_alive(@addr, @alive)
- end
- def link(neighbors)
- if neighbors.respond_to?(:to_ary)
- @neighbor_ids += neighbors
- else
- @neighbor_ids << neighbors
- end
+ @topo.register_node(self)
end
def handle_liveness_packet(pkt)
@alive = pkt.alive
- @topo.node_alive(@addr, @alive)
end
def alive?
@alive
end
- def alive=(status)
- log "Setting alive= on #{nid}"
+ def alive(status)
@alive = status
- @topo.node_alive(@addr, @alive)
end
- alias alive alive=
- def send_packet(id, receivers, pkt)
+ def send_packet(receivers, pkt)
@topo.send_packet(id, @sid, receivers, pkt)
end
+ # Override this in your subclass to do custom demuxing.
+ def recv_packet(pkt)
+ log {"default recv_packet handler..."}
+ end
+
# Implement this method to do something specific for your application.
def handle_failed_packet(pkt)
- puts "Got a failed packet! (#{pkt.data.class})"
+ log {"Got a failed packet! (#{pkt.data.class})"}
end
+ def handle_failed_rpc(method, data)
+ log {"Got a failed rpc call: #{method}(#{data.join(', ')})"}
+ end
end
end # module Net
end # module GoSim
View
79 lib/gosim/simulation.rb
@@ -6,30 +6,41 @@ class Entity
@@sid_counter = 0
+ # Used internally by Entity objects that require a unique simulation ID.
def Entity.next_sid
@@sid_counter += 1
@@sid_counter - 1
end
+ # Reset the entity ID counter, mostly here for easy unit testability.
def Entity.reset
@@sid_counter = 0
end
+ # Create a new simulation Entity. This will typically be run when super()
+ # is called from a child class.
def initialize
@sim = Simulation.instance
reset
end
+ # Reset an Entity so that it gets a new simulation ID and
+ # re-registers itself. Typically just for unit testing.
def reset
@sid = Entity.next_sid
@sim.register_entity(@sid, self)
self
end
- def set_timeout(time, is_periodic = false, &block)
- SimTimeout.new(time, is_periodic, block)
+ # Set a block of code to run after wait_time units of time. If the
+ # is_periodic flag is set it will continue to run every wait_time units.
+ def set_timeout(wait_time, is_periodic = false, &block)
+ SimTimeout.new(wait_time, is_periodic, block)
end
+ # Override the default inspect so entities with lots of state don't fill
+ # the screen during debug. Implement your own inspect method to print
+ # useful information about your Entity.
def inspect
"<GoSim::Entity sid=#{@sid}>"
end
@@ -59,14 +70,12 @@ def initialize(time, is_periodic, block)
def setup_timer
@active = true
- @sim.schedule_event(:timeout, @sid, @time, self)
- #log "Timeout started for #{@sid} in #{@time} units"
+ @sim.schedule_event(:handle_timeout, @sid, @time, self)
end
alias start reset
def cancel
@active = false
- #log "Timeout stopped for #{@sid}"
end
alias stop cancel
@@ -76,7 +85,6 @@ def start
end
def handle_timeout(timeout)
- #log "sid -> #{@sid} running timeout"
# Test twice in case the timeout was canceled in the block.
@block.call(self) if @active
setup_timer if @active and @is_periodic
@@ -123,6 +131,7 @@ def start_client(ip, port = PORT_NUMBER)
def initialize
@trace = Logger.new(STDOUT)
+ #GC.disable
reset
end
@@ -131,7 +140,9 @@ def reset
@time = 0
@end_time = 1000
@running = false
- @event_queue = PQueue.new(proc {|x,y| x.time < y.time})
+
+ reset_event_queue
+
@entities = {}
@handlers = {}
@@ -144,6 +155,16 @@ def reset
self
end
+ if not method_defined?(:reset_event_queue)
+ def reset_event_queue
+ @event_queue = PQueue.new(proc {|x,y| x.time < y.time})
+ end
+
+ def queue_size
+ @event_queue.size
+ end
+ end
+
def register_entity(sid, entity)
@entities[sid] = entity
@handlers[sid] = {}
@@ -153,10 +174,6 @@ def add_handler(sid, event_id, &block)
@handlers[sid][event_id] = block
end
- def queue_size
- @event_queue.size
- end
-
def num_entities
@entities.size
end
@@ -176,36 +193,40 @@ def trace_log(device)
alias trace_log= trace_log
# Schedule a new event by putting it into the event queue
- def schedule_event(event_id, dest_id, time, data)
- #log "#{dest_id} is scheduling #{event_id} for #{@time + time}"
- event_id = ("handle_" + event_id.to_s).to_sym
- @event_queue.push(Event.new(event_id, dest_id, @time + time, data))
+ if not method_defined?(:schedule_event)
+ def schedule_event(event_id, dest_sid, time, data)
+ @event_queue.push(Event.new(event_id, dest_sid, @time + time, data))
+ end
end
- def run(end_time = MAX_INT)
- return if @running # Disallow after starting once
- @running = true
-
- #log ("Running simulation until: #{end_time}")
- begin
+ if not method_defined?(:run_main_loop)
+ def run_main_loop(end_time)
while(@running and (cur_event = @event_queue.top) and (cur_event.time <= end_time))
- #log ("Handling %s event at %d\n" % [cur_event.data.class, cur_event.time])
-
@event_queue.pop
-
@time = last_time = cur_event.time
-
@entities[cur_event.dest_id].send(cur_event.event_id, cur_event.data)
end
+ end
+ end
+
+ def run(end_time = 2**30)
+ return if @running # Disallow after starting once
+ @running = true
+
+ #log ("Running simulation until: #{end_time}")
+ begin
+ run_main_loop(end_time)
rescue Exception => e
- error "GoSim error occurred sending:\n#{cur_event.data.inspect}\nto destination: #{cur_event.dest_id}.#{cur_event.event_id}"
- puts "Exception: #{e}"
- print e.backtrace.join("\n")
+ error "GoSim error occurred in main event loop!"
+ puts "Generated Exception: #{e}"
+ puts e.backtrace.join("\n")
stop
end
@running = false
- @time = last_time || end_time # Do this so we are at the correct time even if no events fired.
+
+ # Do this so we are at the correct time even if no events fired.
+ @time = end_time if @time < end_time
# Make sure to write out all the data files when simulation finishes.
DataSet.flush_all
View
39 test/benchmark.rb
@@ -1,39 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-
-require 'rational'
-require 'gosim'
-
-require 'rubygems'
-require 'benchmark'
-
-class Benchmarker < GoSim::Entity
- attr_reader :counter
-
- def initialize(n)
- super()
-
- @counter = 0
-
- n.times do |t|
- @sim.schedule_event(:item, @sid, t * 10, t)
- end
- end
-
- def handle_item(t)
- @counter += 1
- end
-end
-
-NUM_EVENTS = 100000
-
-sim = GoSim::Simulation::instance
-
-Benchmark.bm do |stat|
- puts "Starting benchmark for #{NUM_EVENTS} events:\n"
-
- b = Benchmarker.new(NUM_EVENTS)
- stat.report { sim.run }
-
- puts "Benchmark complete after counting #{b.counter} events.\n"
-end
-
View
93 test/defer_test.rb
@@ -0,0 +1,93 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+
+require 'test/unit'
+
+require 'gosim/defer'
+
+class TestDefer < Test::Unit::TestCase
+ include GoSim::Net
+
+ def setup
+ @cb_result = 0
+ @eb_result = 0
+ end
+
+ def test_add_cb
+ d = Deferred.new
+
+ d.add_callbacks(method(:good_cb))
+ d.add_callbacks(method(:good_cb), method(:good_eb))
+ d.add_callback {|r| good_cb(r) }
+ d.add_errback(method(:good_eb))
+ d.add_callback(method(:good_cb))
+
+ val = 0
+ d.callback(val)
+ assert(@cb_result == 4)
+ end
+
+ def test_add_eb
+ d = Deferred.new
+
+ d.add_callback(method(:good_cb))
+ d.add_callbacks(method(:good_to_bad_cb))
+ d.add_callbacks(method(:good_cb), method(:good_eb))
+ d.add_errback(method(:good_eb))
+ d.add_errback(method(:good_eb))
+ d.add_errback(method(:good_eb))
+ d.add_errback(method(:bad_to_good_eb))
+ d.add_callback(method(:good_cb))
+
+ val = 0
+ d.callback(val)
+ assert_equal(4, @eb_result)
+ assert_equal(2, @cb_result)
+ end
+
+ def test_errback
+ d = Deferred.new
+
+ d.add_errback(method(:good_eb))
+ d.add_errback {|f| good_eb(f) }
+ d.add_errback(method(:good_eb))
+
+ val = 0
+ d.errback(Failure.new(val))
+ assert_equal(3, @eb_result)
+ end
+
+ def test_pause
+ d = Deferred.new
+
+ d.callback(0)
+
+ d.add_callback(method(:good_cb))
+ d.add_callback(method(:good_cb))
+ assert_equal(2, @cb_result)
+
+ d.pause
+ d.add_callback(method(:good_cb))
+ assert_equal(2, @cb_result)
+ d.add_callback(method(:good_cb))
+ d.unpause
+ assert_equal(4, @cb_result)
+ end
+
+ def good_cb(val)
+ @cb_result = val += 1
+ return @cb_result
+ end
+
+ def good_to_bad_cb(val)
+ Failure.new(val)
+ end
+
+ def good_eb(failure)
+ @eb_result += 1
+ return failure
+ end
+
+ def bad_to_good_eb(failure)
+ failure.result
+ end
+end
View
70 test/network_test.rb
@@ -13,13 +13,29 @@ def initialize
@got_message = false
@pkt_cache = []
@failed_packets = 0
+ @neighbors = {}
+ end
+
+ def add_neighbor(addr)
+ @neighbors[addr] = GoSim::Net::Peer.new(self, addr)
+ end
+
+ def start_flood(pkt)
+ forward_packet(pkt)
end
def handle_packet(pkt)
@got_message = true
+ forward_packet(pkt)
+ end
+ def forward_packet(pkt)
unless @pkt_cache.index(pkt.seq_num)
- send_packet(:packet, @neighbor_ids, pkt) unless @neighbor_ids.empty?
+ @neighbors.values.each do |n|
+ d = n.handle_packet(pkt)
+ d.add_errback {|f| @failed_packets += 1}
+ end
+
@pkt_cache << pkt.seq_num
end
end
@@ -27,10 +43,6 @@ def handle_packet(pkt)
def got_message?
@got_message
end
-
- def handle_failed_packet(pkt)
- @failed_packets += 1
- end
end
class TestNetworkSimulation < Test::Unit::TestCase
@@ -41,31 +53,12 @@ def setup
@sim = GoSim::Simulation.instance
@topo = GoSim::Net::Topology.instance
- #@sim.verbose
@sim.quiet
end
def teardown
@sim.reset
end
-
- def test_linking
- nodes = {}
- NUM_NODES.times do
- n = TestNode.new
- nodes[n.sid] = n
- end
-
- n = TestNode.new
- n.link(nodes.keys)
- assert_equal(NUM_NODES, n.neighbor_ids.size)
-
- n = TestNode.new
- n.link(nodes[0])
- assert_equal(1, n.neighbor_ids.size)
- n.link(nodes[1])
- assert_equal(2, n.neighbor_ids.size)
- end
def test_flood
nodes = {}
@@ -79,35 +72,34 @@ def test_flood
nodes.each do |sid, node|
(rand(CONNECTIVITY) + 1).times do
- neighbor = nodes.keys[rand(NUM_NODES)]
- node.link(neighbor) unless neighbor == sid
+ n_addr = nodes.keys[rand(NUM_NODES)]
+ node.add_neighbor(n_addr) unless n_addr == node.addr
end
end
- @sim.schedule_event(:packet, nodes.keys[0], 0, Packet.new(1))
+ @sim.schedule_event(:start_flood, nodes.keys[0], 0, Packet.new(1))
@sim.run
- nodes.values.each { |node| assert(node.got_message?) }
+ nodes.values.each { |node| assert(node.got_message?, "#{node.addr}->#{node.got_message?}") }
end
def test_liveness_and_failure
- nodes = {}
node_a = TestNode.new
node_b = TestNode.new
- node_a.link(node_b.sid)
+ node_a.add_neighbor(node_b.addr)
- 2.times {|i| @sim.schedule_event(:packet, node_a.sid, 0, Packet.new(i)) }
- 2.times {|i| @sim.schedule_event(:packet, node_a.sid,
- i + GoSim::Net::MEAN_LATENCY * 2,
- Packet.new(i+2)) }
- @sim.schedule_event(:liveness_packet,
- node_b.sid,
- GoSim::Net::MEAN_LATENCY * 2,
- GoSim::Net::LivenessPacket.new(false))
+ 10.times {|i| @sim.schedule_event(:handle_packet, node_a.sid, i*1000, Packet.new(i)) }
+ @sim.schedule_event(:alive, node_b.sid, 5000, false)
@sim.run
- assert_equal(2, node_a.failed_packets)
+ assert_equal(5, node_a.failed_packets)
+ end
+
+ def test_net_deferred
+ node_a = TestNode.new
+ node_b = TestNode.new
+
end
end
View
25 test/simulation_test.rb
@@ -12,7 +12,7 @@ def initialize(neighbor, num_items)
# Schedule a bunch of new product events.
num_items.times do |t|
- @sim.schedule_event(:item, @sid, t * 10, Item.new("foo", :receive))
+ @sim.schedule_event(:new_item, @sid, t * 10, Item.new("foo", :receive))
end
dir_name = File.join(File.dirname(__FILE__), "output")
@@ -20,8 +20,9 @@ def initialize(neighbor, num_items)
@dataset = GoSim::DataSet.new(:producer, dir_name)
end
- def handle_item(event)
- @sim.schedule_event(:item, @neighbor, 5, event)
+ def new_item(event)
+ log {"got new #{event.class} event"}
+ @sim.schedule_event(:new_item, @neighbor, 5, event)
@dataset.log(@sid, event.name)
end
end
@@ -34,7 +35,7 @@ def initialize
@received = 0
end
- def handle_item(event)
+ def new_item(event)
@received += 1
end
end
@@ -72,11 +73,21 @@ def test_logging
end
def test_scheduler
+ num_items = 10000
consumer = Consumer.new
- producer = Producer.new(consumer.sid, 10)
- assert_equal(10, @sim.queue_size, "Schedule event not correctly adding to queue.")
+ producer = Producer.new(consumer.sid, num_items)
+ assert_equal(num_items, @sim.queue_size, "Schedule event not correctly adding to queue.")
@sim.run
+ assert_equal(num_items, consumer.received)
+ end
+
+ def test_single_step
+ num_items = 10
+ consumer = Consumer.new
+ producer = Producer.new(consumer.sid, num_items)
+
+ (num_items * 10).times {|i| @sim.run(i) }
assert_equal(10, consumer.received)
end
@@ -89,7 +100,7 @@ def test_data_set
producer = Producer.new(consumer.sid, 2)
@sim.run
- assert_equal("0, #{producer.sid}, foo\n10, #{producer.sid}, foo\n", IO::read(file))
+ assert_equal("0: #{producer.sid}, foo\n10: #{producer.sid}, foo\n", IO::read(file))
# Now try with an attached handler instead
@sim.reset
View
40 tools/benchmark.rb
@@ -0,0 +1,40 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+
+require 'rational'
+
+require 'gosim'
+
+require 'rubygems'
+require 'benchmark'
+
+class Benchmarker < GoSim::Entity
+ attr_reader :counter
+
+ def initialize(n)
+ super()
+
+ @counter = 0
+
+ n.times do |t|
+ @sim.schedule_event(:handle_item, @sid, t * 10 + 1, t)
+ end
+ end
+
+ def handle_item(t)
+ @counter += 1
+ end
+end
+
+def run_benchmark(num_events)
+ sim = GoSim::Simulation::instance
+
+ puts "Starting benchmark for #{num_events} events:\n"
+ Benchmark.bm do |stat|
+ b = Benchmarker.new(num_events)
+ stat.report { sim.run }
+ end
+end
+
+num_events = ARGV.first.to_i || 1000000
+
+run_benchmark(num_events)
View
118 tools/rakehelp.rb
@@ -0,0 +1,118 @@
+def make(makedir)
+ Dir.chdir(makedir) do
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
+ end
+end
+
+
+def extconf(dir)
+ Dir.chdir(dir) do ruby "extconf.rb" end
+end
+
+
+def setup_tests
+ desc "Run the unit tests in test"
+ Rake::TestTask.new(:test) do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/*_test.rb']
+ t.verbose = true
+ end
+end
+
+
+def setup_clean otherfiles
+ files = ['build/*', '**/*.o', '**/*.so', '**/*.a', 'lib/*-*', '**/*.log'] + otherfiles
+ CLEAN.include(files)
+end
+
+
+def setup_rdoc files
+ Rake::RDocTask.new do |rdoc|
+ rdoc.rdoc_dir = 'doc/rdoc'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.add(files)
+ end
+end
+
+
+def setup_extension(dir, extension)
+ ext = "ext/#{dir}"
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
+ ext_files = FileList[
+ "#{ext}/*.c",
+ "#{ext}/*.h",
+ "#{ext}/extconf.rb",
+ "#{ext}/Makefile",
+ "lib"
+ ]
+
+ task "lib" do
+ directory "lib"
+ end
+
+ desc "Builds just the #{extension} extension"
+ task extension.to_sym => ["#{ext}/Makefile", ext_so ]
+
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
+ extconf "#{ext}"
+ end
+
+ file ext_so => ext_files do
+ make "#{ext}"
+ cp ext_so, "lib"
+ end
+end
+
+
+def base_gem_spec(pkg_name, pkg_version)
+ rm_rf "test/coverage"
+ pkg_version = pkg_version
+ pkg_name = pkg_name
+ pkg_file_name = "#{pkg_name}-#{pkg_version}"
+ Gem::Specification.new do |s|
+ s.name = pkg_name
+ s.version = pkg_version
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = [ "README" ]
+
+ s.files = %w(COPYING LICENSE README Rakefile) +
+ Dir.glob("{bin,doc/rdoc,test}/**/*") +
+ Dir.glob("ext/**/*.{h,c,rb,rl}") +
+ Dir.glob("{examples,tools,lib}/**/*.rb")
+
+ s.require_path = "lib"
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
+ s.bindir = "bin"
+ end
+end
+
+def setup_gem(pkg_name, pkg_version)
+ spec = base_gem_spec(pkg_name, pkg_version)
+ yield spec if block_given?
+
+ Rake::GemPackageTask.new(spec) do |p|
+ p.gem_spec = spec
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
+ end
+end
+
+def sub_project(project, *targets)
+ targets.each do |target|
+ Dir.chdir "projects/#{project}" do
+ sh %{rake --trace #{target.to_s} }
+ end
+ end
+end
+
+# Conditional require rcov/rcovtask if present
+begin
+ require 'rcov/rcovtask'
+
+ Rcov::RcovTask.new do |t|
+ t.test_files = FileList['test/test*.rb']
+ t.rcov_opts << "-x /usr"
+ t.output_dir = "test/coverage"
+ end
+rescue Object
+end
Please sign in to comment.
Something went wrong with that request. Please try again.