Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try to fixup the context mess
[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   jclass cls = jxbt_get_class(env,"simgrid/msg/Host");  
21   jmethodID constructor = jxbt_get_jmethod(env,cls,"<init>", "()V");
22         
23   if(!constructor) 
24     return NULL;
25         
26   return (*env)->NewObject(env,cls,constructor);
27 }
28
29 jobject jhost_ref(JNIEnv* env,jobject jhost) {
30   return (*env)->NewGlobalRef(env,jhost);
31 }
32
33 void jhost_unref(JNIEnv* env,jobject jhost) {
34   (*env)->DeleteGlobalRef(env,jhost);
35 }
36
37 void jhost_bind(jobject jhost,m_host_t host,JNIEnv* env) {
38   jfieldID id  = jxbt_get_sfield(env,"simgrid/msg/Host","bind", "J");
39
40   if (!id)
41     return;
42         
43   (*env)->SetLongField(env,jhost,id,(jlong)(long)(host));
44 }
45
46 m_host_t jhost_get_native(JNIEnv* env, jobject jhost) {
47   jfieldID id  = jxbt_get_sfield(env,"simgrid/msg/Host","bind", "J");
48
49   if(!id)
50     return NULL;
51
52   return (m_host_t)(long)(*env)->GetLongField(env,jhost,id);
53 }
54
55 const char*  jhost_get_name(jobject jhost,JNIEnv* env) {
56   m_host_t host = jhost_get_native(env,jhost);
57   return (const char*)host->name;               
58 }
59
60 void jhost_set_name(jobject jhost,jstring jname,JNIEnv* env) {
61   const char* name;
62   m_host_t host = jhost_get_native(env,jhost);
63
64   name = (*env)->GetStringUTFChars(env, jname, 0);
65         
66   if(host->name)
67     free(host->name);
68         
69   host->name = xbt_strdup(name);
70   (*env)->ReleaseStringUTFChars(env, jname, name);      
71 }
72
73 jboolean jhost_is_valid(jobject jhost,JNIEnv* env) {
74   jfieldID id  = jxbt_get_sfield(env,"simgrid/msg/Host","bind", "J");
75
76   if(!id)
77     return 0;
78
79   if ((*env)->GetLongField(env,jhost,id)) {
80     return JNI_TRUE;
81   } else {
82     return JNI_FALSE;
83   }
84 }
85
86