X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/51f5efedb7e873fa0c82bdc8bb0aa78ae3e827c7..de7d161240f434a809ae7739fbb15a1db6ab86f2:/src/bindings/ruby/simgrid_ruby.c diff --git a/src/bindings/ruby/simgrid_ruby.c b/src/bindings/ruby/simgrid_ruby.c index c02fd553aa..d6c5338185 100644 --- a/src/bindings/ruby/simgrid_ruby.c +++ b/src/bindings/ruby/simgrid_ruby.c @@ -1,6 +1,7 @@ /* SimGrid Ruby bindings */ -/* Copyright (c) 2010, the SimGrid team. All right reserved. */ +/* Copyright (c) 2010. The SimGrid Team. + * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -16,196 +17,215 @@ VALUE rb_msg; VALUE rb_task; VALUE rb_host; - //Init Msg From Ruby -static void msg_init(VALUE Class,VALUE args) +static void msg_init(VALUE Class, VALUE args) { - char **argv=NULL; + char **argv = NULL; const char *tmp; - int argc,type,i; - VALUE *ptr ; + int argc, type, i; + VALUE *ptr; // Testing The Args Type - type = TYPE(args); - if (type != T_ARRAY ) { - rb_raise(rb_eRuntimeError,"Bad arguments to msg_init (expecting an array)"); + type = TYPE(args); + if (type != T_ARRAY) { + rb_raise(rb_eRuntimeError, + "Bad arguments to msg_init (expecting an array)"); return; } - ptr= RARRAY(args)->ptr; - argc= RARRAY(args)->len; - // Create C Array to Hold Data_Get_Struct + ptr = RARRAY(args)->ptr; + argc = RARRAY(args)->len; + // Create C array to hold data_get_struct argc++; argv = xbt_new0(char *, argc); argv[0] = strdup("ruby"); - for (i=0;iptr; - argv[i+1] = strdup(tmp); + argv[i + 1] = strdup(tmp); } // Calling C Msg_Init Method - MSG_global_init(&argc,argv); + MSG_global_init(&argc, argv); // Cleanups - for (i=0;idata);// ??!! - } - - if (MSG_OK != MSG_clean()){ - rb_raise(rb_eRuntimeError,"MSG_clean() failed"); + for (cpt = 0; cpt < host_count; cpt++) { + rbHost = (VALUE) ((hosts[cpt])->data); } return; } -static void msg_createEnvironment(VALUE class,VALUE plateformFile) { +static void msg_clean(VALUE class) +{ + if (MSG_OK != MSG_clean()) + rb_raise(rb_eRuntimeError, "MSG_clean() failed"); + +} + +static void msg_createEnvironment(VALUE class, VALUE plateformFile) +{ int type = TYPE(plateformFile); - if ( type != T_STRING ) - rb_raise(rb_eRuntimeError,"Bad Argument's Type"); - const char * platform = RSTRING(plateformFile)->ptr; + if (type != T_STRING) + rb_raise(rb_eRuntimeError, "Bad Argument's Type"); + const char *platform = RSTRING(plateformFile)->ptr; MSG_create_environment(platform); - DEBUG1("Create Environment (%s)...Done",platform); + DEBUG1("Create Environment (%s)...Done", platform); } //deploy Application -static void msg_deployApplication(VALUE class,VALUE deploymentFile ) { +static void msg_deployApplication(VALUE class, VALUE deploymentFile) +{ int type = TYPE(deploymentFile); - if ( type != T_STRING ) - rb_raise(rb_eRuntimeError,"Bad Argument's Type for deployApplication "); + if (type != T_STRING) + rb_raise(rb_eRuntimeError, + "Bad Argument's Type for deployApplication "); const char *dep_file = RSTRING(deploymentFile)->ptr; surf_parse_reset_parser(); surfxml_add_callback(STag_surfxml_process_cb_list, - rb_application_handler_on_begin_process); - + rb_application_handler_on_begin_process); surfxml_add_callback(ETag_surfxml_argument_cb_list, - rb_application_handler_on_process_arg); + rb_application_handler_on_process_arg); surfxml_add_callback(STag_surfxml_prop_cb_list, - rb_application_handler_on_property); + rb_application_handler_on_property); surfxml_add_callback(ETag_surfxml_process_cb_list, - rb_application_handler_on_end_process); + rb_application_handler_on_end_process); surf_parse_open(dep_file); rb_application_handler_on_start_document(); - if(surf_parse()) - rb_raise(rb_eRuntimeError,"surf_parse() failed"); + if (surf_parse()) + rb_raise(rb_eRuntimeError, "surf_parse() failed"); surf_parse_close(); + rb_application_handler_on_end_document(); - DEBUG1("Deploy Application(%s)...Done",dep_file); + DEBUG1("Deploy Application(%s)...Done", dep_file); } // INFO -static void msg_info(VALUE class,VALUE msg) { - const char *s = RSTRING(msg)->ptr; - INFO1("%s",s); -} -static void msg_debug(VALUE class,VALUE msg) { +static void msg_info(VALUE class, VALUE msg) +{ const char *s = RSTRING(msg)->ptr; - DEBUG1("%s",s); + INFO1("%s", s); } -// Get Clock FIXME: return the double instead of displaying it -static void msg_get_clock(VALUE class) { - - printf("Simulation time %f\n",MSG_get_clock()); - +static void msg_debug(VALUE class, VALUE msg) +{ + const char *s = RSTRING(msg)->ptr; + DEBUG1("%s", s); } -// Ruby intropspection : Instanciate a ruby Class From its Name -// Used by ProcessFactory::createProcess - -static VALUE msg_new_ruby_instance(VALUE class,VALUE className) { - ruby_init(); - ruby_init_loadpath(); - char * p_className = RSTRING(className)->ptr; - - return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),0, 0); -} +// get Clock +static VALUE msg_get_clock(VALUE class) +{ + return rb_float_new(MSG_get_clock()); -//This Time With Args -static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args) { - ruby_init(); - ruby_init_loadpath(); - char * p_className = RSTRING(className)->ptr; - return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"), 1, args); } - -extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */ - -typedef VALUE(*rb_meth)(ANYARGS); -void Init_simgrid_ruby() { - xbt_ctx_factory_to_use = "ruby"; +typedef VALUE(*rb_meth) (ANYARGS); +void Init_libsimgrid() +{ + factory_initializer_to_use = SIMIX_ctx_ruby_factory_init; // Modules rb_msg = rb_define_module("MSG"); - //Associated Environment Methods! - rb_define_module_function(rb_msg,"init",(rb_meth)msg_init,1); - rb_define_module_function(rb_msg,"run",(rb_meth)msg_run,0); - rb_define_module_function(rb_msg,"createEnvironment",(rb_meth)msg_createEnvironment,1); - rb_define_module_function(rb_msg,"deployApplication",(rb_meth)msg_deployApplication,1); - rb_define_module_function(rb_msg,"info",(rb_meth)msg_info,1); - rb_define_module_function(rb_msg,"debug",(rb_meth)msg_debug,1); - rb_define_module_function(rb_msg,"getClock",(rb_meth)msg_get_clock,0); - rb_define_module_function(rb_msg,"rubyNewInstance",(rb_meth)msg_new_ruby_instance,1); - rb_define_module_function(rb_msg,"rubyNewInstanceArgs",(rb_meth)msg_new_ruby_instance_with_args,2); - - // Associated Process Methods - rb_define_module_function(rb_msg,"processCreate",(rb_meth)rb_process_create,2); - rb_define_method(rb_msg,"processSuspend",(rb_meth)rb_process_suspend,1); - rb_define_method(rb_msg,"processResume",(rb_meth)rb_process_resume,1); - rb_define_method(rb_msg,"processIsSuspend",(rb_meth)rb_process_isSuspended,1); - rb_define_method(rb_msg,"processKill",(rb_meth)rb_process_kill_up,1); - rb_define_method(rb_msg,"processGetHost",(rb_meth)rb_process_getHost,1); - rb_define_method(rb_msg,"processExit",(rb_meth)rb_process_exit,1); + //Associated Environment Methods + rb_define_module_function(rb_msg, "init", (rb_meth) msg_init, 1); + rb_define_module_function(rb_msg, "run", (rb_meth) msg_run, 0); + rb_define_module_function(rb_msg, "createEnvironment", + (rb_meth) msg_createEnvironment, 1); + rb_define_module_function(rb_msg, "deployApplication", + (rb_meth) msg_deployApplication, 1); + rb_define_module_function(rb_msg, "info", (rb_meth) msg_info, 1); + rb_define_module_function(rb_msg, "debug", (rb_meth) msg_debug, 1); + rb_define_module_function(rb_msg, "getClock", (rb_meth) msg_get_clock, + 0); + rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0); + + //Associated Process Methods + rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend, + 1); + rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume, + 1); + rb_define_method(rb_msg, "processIsSuspend", + (rb_meth) rb_process_isSuspended, 1); + rb_define_method(rb_msg, "processKill", (rb_meth) rb_process_kill_up, 1); + rb_define_method(rb_msg, "processKillDown", + (rb_meth) rb_process_kill_down, 1); + rb_define_method(rb_msg, "processGetHost", (rb_meth) rb_process_getHost, + 1); + rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1); //Classes - rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject); - rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject); - - //Task Methods FIXME: Convert to methods - rb_define_module_function(rb_task,"new",(rb_meth)rb_task_new,3); - rb_define_module_function(rb_task,"compSize",(rb_meth)rb_task_comp,1); - rb_define_module_function(rb_task,"name",(rb_meth)rb_task_name,1); - rb_define_module_function(rb_task,"execute",(rb_meth)rb_task_execute,1); - rb_define_module_function(rb_task,"send",(rb_meth)rb_task_send,2); - rb_define_module_function(rb_task,"receive",(rb_meth)rb_task_receive,1); - rb_define_module_function(rb_task,"sender",(rb_meth)rb_task_sender,1); - rb_define_module_function(rb_task,"source",(rb_meth)rb_task_source,1); - rb_define_module_function(rb_task,"listen",(rb_meth)rb_task_listen,2); - rb_define_module_function(rb_task,"listenFromHost",(rb_meth)rb_task_listen_host,3); + rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject); + rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject); + + //Task Methods + rb_define_module_function(rb_task, "new", (rb_meth) rb_task_new, 3); + rb_define_module_function(rb_task, "compSize", (rb_meth) rb_task_comp, + 1); + rb_define_module_function(rb_task, "name", (rb_meth) rb_task_name, 1); + rb_define_module_function(rb_task, "execute", (rb_meth) rb_task_execute, + 1); + rb_define_module_function(rb_task, "send", (rb_meth) rb_task_send, 2); + rb_define_module_function(rb_task, "receive", (rb_meth) rb_task_receive, + 1); + rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender, + 1); + rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source, + 1); + rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen, + 2); + rb_define_module_function(rb_task, "listenFromHost", + (rb_meth) rb_task_listen_host, 3); + rb_define_module_function(rb_task, "setPriority", + (rb_meth) rb_task_set_priority, 2); + rb_define_module_function(rb_task, "cancel", (rb_meth) rb_task_cancel, + 1); + rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data, + 1); + rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data, + 2); + rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data, + 1); //Host Methods - rb_define_module_function(rb_host,"getByName",(rb_meth)rb_host_get_by_name,1); - rb_define_module_function(rb_host,"name",(rb_meth)rb_host_name,1); - rb_define_module_function(rb_host,"speed",(rb_meth)rb_host_speed,1); - rb_define_module_function(rb_host,"number",(rb_meth)rb_host_number,0); - rb_define_module_function(rb_host,"setData",(rb_meth)rb_host_set_data,2); - rb_define_module_function(rb_host,"getData",(rb_meth)rb_host_get_data,1); - rb_define_module_function(rb_host,"isAvail",(rb_meth)rb_host_is_avail,1); + rb_define_module_function(rb_host, "getByName", + (rb_meth) rb_host_get_by_name, 1); + rb_define_module_function(rb_host, "name", (rb_meth) rb_host_name, 1); + rb_define_module_function(rb_host, "speed", (rb_meth) rb_host_speed, 1); + rb_define_module_function(rb_host, "number", (rb_meth) rb_host_number, + 0); + rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail, + 1); + rb_define_module_function(rb_host, "getHostProcess", + (rb_meth) rb_host_process, 1); + rb_define_module_function(rb_host, "all", + (rb_meth) rb_host_get_all_hosts, 0); }