Logo AND Algorithmique Numérique Distribuée

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