Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename TimeoutFailureException to TimeoutException
[simgrid.git] / src / java / jmsg_host.c
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved. 
5  *
6  * This program is free software; you can redistribute it and/or modify it 
7  * under the terms of the license (GNU LGPL) which comes with this package.
8  *
9  * This contains the implementation of the functions in relation with the java
10  * host instance. 
11  */
12
13 #include "xbt/str.h"
14 #include "jmsg.h"
15 #include "jmsg_host.h"
16 #include "jxbt_utilities.h"
17
18 jobject jhost_new_instance(JNIEnv * env)
19 {
20
21   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
22   jmethodID constructor = jxbt_get_jmethod(env, cls, "<init>", "()V");
23
24   if (!constructor)
25     return NULL;
26
27   return (*env)->NewObject(env, cls, constructor);
28 }
29
30 jobject jhost_ref(JNIEnv * env, jobject jhost)
31 {
32   return (*env)->NewGlobalRef(env, jhost);
33 }
34
35 void jhost_unref(JNIEnv * env, jobject jhost)
36 {
37   (*env)->DeleteGlobalRef(env, jhost);
38 }
39
40 void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env)
41 {
42   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
43
44   if (!id)
45     return;
46
47   (*env)->SetLongField(env, jhost, id, (jlong) (long) (host));
48 }
49
50 m_host_t jhost_get_native(JNIEnv * env, jobject jhost)
51 {
52   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
53
54   if (!id)
55     return NULL;
56
57   return (m_host_t) (long) (*env)->GetLongField(env, jhost, id);
58 }
59
60 const char *jhost_get_name(jobject jhost, JNIEnv * env)
61 {
62   m_host_t host = jhost_get_native(env, jhost);
63   return (const char *) host->name;
64 }
65
66 void jhost_set_name(jobject jhost, jstring jname, JNIEnv * env)
67 {
68   const char *name;
69   m_host_t host = jhost_get_native(env, jhost);
70
71   name = (*env)->GetStringUTFChars(env, jname, 0);
72
73   if (host->name)
74     free(host->name);
75
76   host->name = xbt_strdup(name);
77   (*env)->ReleaseStringUTFChars(env, jname, name);
78 }
79
80 jboolean jhost_is_valid(jobject jhost, JNIEnv * env)
81 {
82   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
83
84   if (!id)
85     return 0;
86
87   if ((*env)->GetLongField(env, jhost, id)) {
88     return JNI_TRUE;
89   } else {
90     return JNI_FALSE;
91   }
92 }