+#include <Process.hpp>\r
+\r
+namespace msg\r
+{\r
+ \r
+Process* Process::currentProcess = NULL;\r
+\r
+// Default constructor.\r
+Process::Process()\r
+{\r
+ this->nativeProcess = NULL;\r
+}\r
+\r
+Process::Process(const char* hostname, const char* name)\r
+throw(HostNotFoundException)\r
+{\r
+ Host host = Host::getByName(hostname);\r
+ \r
+ create(host, name, 0, NULL); \r
+}\r
+\r
+Process::Process(const char* hostname, const char* name, int argc, char** argv)\r
+throw(HostNotFoundException)\r
+{\r
+ Host host = Host::getByName(hostname);\r
+ \r
+ create(host, name, argc, argv); \r
+}\r
+\r
+Process::Process(const Host& rHost, const char* name)\r
+throw(HostNotFoundException)\r
+{\r
+ \r
+ create(rHost, name, 0, NULL); \r
+}\r
+\r
+Process::Process(const Host& rHost, const char* name, int argc, char** argv)\r
+throw(HostNotFoundException)\r
+{\r
+ \r
+ create(rHost, name, argc, argv); \r
+}\r
+\r
+int Process::run(int argc, char** argv)\r
+{\r
+ Process* process =(Process*)argv[argc];\r
+ \r
+ return process->main(argc, argv);\r
+}\r
+\r
+void Process::create(const Host& rHost, const char* name, int argc, char** argv)\r
+throw(HostNotFoundException)\r
+{\r
+ smx_process_t nativeCurrentProcess = NULL;\r
+ nativeProcess = xbt_new0(s_smx_process_t, 1);\r
+ smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);\r
+ smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());\r
+ \r
+ argv = (char**)realloc(argc + 1, sizeo(char*));\r
+ \r
+ argv[argc] = (char*)this;\r
+ \r
+ \r
+ // TODO throw HostNotFoundException if host is NULL\r
+ \r
+ // Simulator Data\r
+ simdata->smx_host = nativeHost;\r
+ simdata->mutex = NULL;\r
+ simdata->cond = NULL;\r
+ simdata->argc = argc;\r
+ simdata->argv = argv;\r
+ simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);\r
+ \r
+ /* Process structure */\r
+ nativeProcess->name = xbt_strdup(name);\r
+ nativeProcess->simdata = simdata;\r
+ \r
+ // Set process data\r
+ nativeProcess->data = NULL;\r
+ \r
+ // Set process properties\r
+ simdata->properties = NULL;\r
+ \r
+ xbt_swag_insert(nativeProcess, nativeHost->simdata->process_list);\r
+ \r
+ /* fix current_process, about which xbt_context_start mocks around */\r
+ nativeCurrentProcess = simix_global->current_process;\r
+ xbt_context_start(nativeProcess->simdata->context);\r
+ simix_global->current_process = nativeCurrentProcess;\r
+ \r
+ xbt_swag_insert(nativeProcess, simix_global->process_list);\r
+ DEBUG2("Inserting %s(%s) in the to_run list", nativeProcess->name, nativeHost->name);\r
+ xbt_swag_insert(nativeProcess, simix_global->process_to_run);\r
+}\r
+\r
+\r
+int Process::killAll(int resetPID) \r
+{\r
+ return MSG_process_killall(resetPID);\r
+}\r
+\r
+void Process::suspend(void)\r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_process_suspend(nativeProcess)) \r
+ {\r
+ // TODO throw NativeException.\r
+ }\r
+}\r
+\r
+void Process::resume(void) \r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_process_resume(nativeProcess))\r
+ {\r
+ // TODO throw NativeException.\r
+ }\r
+}\r
+\r
+\r
+bool Process::isSuspended(void)\r
+{\r
+ return (bool)MSG_process_is_suspended(nativeProcess);\r
+} \r
+\r
+\r
+Host& Process::getHost(void) \r
+throw(NativeException) \r
+{\r
+ m_host_t nativeHost = MSG_process_get_host(nativeProcess);\r
+ \r
+ if(!nativeHost->data) \r
+ {\r
+ // TODO throw NativeException.\r
+ return NULL;\r
+ }\r
+\r
+ // return the reference to the Host object\r
+ return (*((Host*)nativeHost->data));\r
+} \r
+\r
+Process& Process::fromPID(int PID) \r
+throw(ProcessNotFoundException, NativeException)\r
+{\r
+ Process* process = NULL;\r
+ m_process_t nativeProcess = MSG_process_from_PID(PID);\r
+ \r
+ \r
+ if(!process) \r
+ {\r
+ throw ProcessNotFoundException;\r
+ return NULL;\r
+ }\r
+ \r
+ process = Process::fromNativeProcess(nativeProcess);\r
+ \r
+ if(!process) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*process); \r
+}\r
+\r
+// TODO implement this method\r
+Process& Process::fromNativeProcess(m_process_t nativeProcess)\r
+{\r
+ \r
+}\r
+\r
+int Process::getPID(void)\r
+{\r
+ return MSG_process_get_PID(nativeProcess);\r
+}\r
+\r
+int Process::getPPID(void)\r
+{\r
+ return MSG_process_get_PPID(nativeProcess);\r
+}\r
+\r
+\r
+Process& Process::currentProcess(void)\r
+throw(NativeException)\r
+{\r
+ Process* currentProcess = NULL;\r
+ m_process_t currentNativeProcess = MSG_process_self();\r
+\r
+\r
+ if(!currentNativeProcess) \r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+ \r
+ currentProcess = Process::fromNativeProcess(currentNativeProcess);\r
+ \r
+ if(!currentProcess) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*currentProcess); \r
+}\r
+\r
+int Process::currentProcessPID(void)\r
+{\r
+ return MSG_process_self_PID();\r
+}\r
+\r
+\r
+int Process::currentProcessPPID(void)\r
+{\r
+ return MSG_process_self_PPID();\r
+}\r
+\r
+void Process::migrate(const Host& rHost)\r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+ \r
+}\r
+\r
+void Process::sleep(double seconds)\r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_process_sleep(seconds))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+ \r
+}\r
+\r
+void Process::putTask(const Host& rHost, int channel, const Task& rTask)\r
+throw( NativeException)\r
+{\r
+ if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+}\r
+\r
+void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout) \r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+}\r
+\r
+Task& Process::getTask(int channel) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ \r
+ if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL)) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*((Task*)(nativeTask->data)));\r
+}\r
+\r
+Task& Process::getTask(int channel, double timeout) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ \r
+ if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL)) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*((Task*)(nativeTask->data)));\r
+}\r
+\r
+Task& Process::getTask(int channel, const Host& rHost) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ \r
+ if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost)) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*((Task*)(nativeTask->data)));\r
+}\r
+\r
+Task& Process::getTask(int channel, double timeout, const Host& rHost)\r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ \r
+ if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost)) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+ \r
+ return (*((Task*)(nativeTask->data)));\r
+}\r
+\r
+void Process::sendTask(const char* alias, const Task& rTask, double timeout) \r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+ \r
+}\r
+\r
+void Process::sendTask(const char* alias, const Task& rTask) \r
+throw(NativeException)\r
+{\r
+ if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+}\r
+\r
+void Process::sendTask(const Task& rTask) \r
+throw(NativeException)\r
+{\r
+ MSG_error_t rv;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv)\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+}\r
+\r
+void Process::sendTask(const Task& rTask, double timeout) \r
+throw(NativeException)\r
+{\r
+ MSG_error_t rv;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv)\r
+ {\r
+ // TODO throw NativeException\r
+ }\r
+}\r
+\r
+Task& Process::receiveTask(const char* alias) \r
+throw(NativeException)\r
+{\r
+ \r
+ m_task_t nativeTask = NULL;\r
+ \r
+ if (MSG_OK != MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL)) \r
+ {\r
+ // TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(void) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(const char* alias, double timeout) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(double timeout) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, NULL);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(double timeout, const Host& rHost) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+\r
+Task& Process::receiveTask(const char* alias, const Host& rHost) \r
+throw(NativeException)\r
+{\r
+ \r
+ m_task_t nativeTask = NULL;\r
+ \r
+\r
+ if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+Task& Process::receiveTask(const Host& rHost) \r
+throw(NativeException)\r
+{\r
+ m_task_t nativeTask = NULL;\r
+ \r
+ char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);\r
+ sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);\r
+ \r
+ MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost);\r
+ \r
+ free(alias);\r
+ \r
+ if(MSG_OK != rv) \r
+ {\r
+ //TODO throw NativeException\r
+ return NULL;\r
+ }\r
+\r
+ return (*((Task*)nativeTask->data));\r
+}\r
+\r
+const char* Process::getName(void) const\r
+{\r
+ return nativeProcess->name;\r
+}\r
+\r
+\r
+}
\ No newline at end of file