Menu

[r19]: / trunk / php-java-bridge / client.c  Maximize  Restore  History

Download this file

322 lines (281 with data), 9.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*-*- mode: C; tab-width:4 -*-*/
/* socket */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* select */
#include <sys/select.h>
/* execve */
#include <unistd.h>
#include <sys/types.h>
/* dynamic loader */
#include <dlfcn.h>
/* strings */
#include <string.h>
/* setenv */
#include <stdlib.h>
/* posix threads implementation */
#include <pthread.h>
/* wait */
#include <sys/types.h>
#include <sys/wait.h>
/* miscellaneous */
#include <stdio.h>
#include <assert.h>
#include <errno.h>
/* jni */
#include <jni.h>
/* php */
#include "php.h"
#include "protocol.h"
#include "java_bridge.h"
#include "php_java.h"
ZEND_DECLARE_MODULE_GLOBALS(java)
static int abort_on_error(proxyenv *jenv, int nr TSRMLS_DC) {
jthrowable error = (*(jenv))->ExceptionOccurred(jenv);
if(!error) return 0;
jclass errClass;
jmethodID toString;
jobject errString;
const char *errAsUTF;
jboolean isCopy;
(*jenv)->ExceptionClear(jenv);
errClass = (*jenv)->GetObjectClass(jenv, error);
toString = (*jenv)->GetMethodID(jenv, errClass, "toString", "()Ljava/lang/String;");
errString = (*jenv)->CallObjectMethod(0, jenv, error, toString);
errAsUTF = (*jenv)->GetStringUTFChars(jenv, errString, &isCopy);
fprintf(stdout, "php_mod_java(%d): %s",nr, errAsUTF);
php_error(E_ERROR, "php_mod_java(%d): %s",nr, errAsUTF);
if(isCopy) (*jenv)->ReleaseStringUTFChars(jenv, errString, errAsUTF);
return 1;
}
static void swrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
int n = fwrite(ptr, size, nmemb, stream);
//printf("write char:::%d\n", (unsigned int) ((char*)ptr)[0]);
assert(n==nmemb);
}
static void sread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
int n = fread(ptr, size, nmemb, stream);
//printf("read char:::%d\n", (unsigned int) ((char*)ptr)[0]);
assert(n==nmemb);
}
static void id(proxyenv *env, char id) {
swrite(&id, sizeof id, 1, (*env)->peer);
}
static void setResultFromString (proxyenv *jenv, pval *presult, jbyteArray jvalue){
jboolean isCopy;
jbyte *value = (*jenv)->GetByteArrayElements(jenv, jvalue, &isCopy);
Z_TYPE_P(presult)=IS_STRING;
Z_STRLEN_P(presult)=(*jenv)->GetArrayLength(jenv, jvalue);
Z_STRVAL_P(presult)=emalloc(Z_STRLEN_P(presult)+1);
memcpy(Z_STRVAL_P(presult), value, Z_STRLEN_P(presult));
Z_STRVAL_P(presult)[Z_STRLEN_P(presult)]=0;
if (isCopy) (*jenv)->ReleaseByteArrayElements(jenv, jvalue, value, 0);
}
static void setResultFromLong (proxyenv *jenv, pval *presult, jlong value) {
Z_TYPE_P(presult)=IS_LONG;
Z_LVAL_P(presult)=(long)value;
}
static void setResultFromDouble (proxyenv *jenv, pval *presult, jdouble value) {
Z_TYPE_P(presult)=IS_DOUBLE;
Z_DVAL_P(presult)=value;
}
static void setResultFromBoolean (proxyenv *jenv, pval *presult, jboolean value) {
Z_TYPE_P(presult)=IS_BOOL;
Z_LVAL_P(presult)=value;
}
static void setResultFromObject (proxyenv *jenv, pval *presult, jobject value) {
/* wrap the java object in a pval object */
pval *handle;
TSRMLS_FETCH();
if (Z_TYPE_P(presult) != IS_OBJECT) {
object_init_ex(presult, &php_java_class_entry);
presult->is_ref=1;
presult->refcount=1;
}
ALLOC_ZVAL(handle);
Z_TYPE_P(handle) = IS_LONG;
jobject _ob= (*jenv)->NewGlobalRef(jenv, value);
Z_LVAL_P(handle) = zend_list_insert(_ob, le_jobject);
pval_copy_constructor(handle);
INIT_PZVAL(handle);
zval_add_ref(&handle);
zend_hash_index_update(Z_OBJPROP_P(presult), 0, &handle, sizeof(pval *), NULL);
}
static void setResultFromArray (proxyenv *jenv, pval *presult) {
array_init( presult );
INIT_PZVAL( presult );
}
static pval*nextElement (proxyenv *jenv, pval *handle) {
pval *result;
zval_add_ref(&handle);
ALLOC_ZVAL(result);
zval_add_ref(&result);
zend_hash_next_index_insert(Z_ARRVAL_P(handle), &result, sizeof(zval *), NULL);
return result;
}
static pval*hashIndexUpdate (proxyenv *jenv, pval *handle, jlong key) {
pval *result;
zval_add_ref(&handle);
ALLOC_ZVAL(result);
zval_add_ref(&result);
zend_hash_index_update(Z_ARRVAL_P(handle), (unsigned long)key, &result, sizeof(zval *), NULL);
return result;
}
static pval*hashUpdate (proxyenv *jenv, pval *handle, jbyteArray key) {
pval *result;
pval pkey;
zval_add_ref(&handle);
ALLOC_ZVAL(result);
setResultFromString(jenv, &pkey, key);
assert(key);
zval_add_ref(&result);
zend_hash_update(Z_ARRVAL_P(handle), Z_STRVAL(pkey), Z_STRLEN(pkey)+1, &result, sizeof(zval *), NULL);
return result;
}
static void setException (proxyenv *jenv, pval *presult, jbyteArray value) {
setResultFromString(jenv, presult, value);
Z_TYPE_P(presult)=IS_EXCEPTION;
}
static int handle_request(proxyenv *env) {
jlong result;
char c;
FILE *peer=(*env)->peer;
sread(&c, 1, 1, peer);
switch(c) {
case PROTOCOL_END: {
return 0;
}
case SETRESULTFROMSTRING: {
jbyteArray jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setResultFromString(env, (pval*)(long)result, jvalue);
break;
}
case SETRESULTFROMLONG: {
jlong jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setResultFromLong(env, (pval*)(long)result, jvalue);
break;
}
case SETRESULTFROMDOUBLE: {
jdouble jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setResultFromDouble(env, (pval*)(long)result, jvalue);
break;
}
case SETRESULTFROMBOOLEAN: {
jboolean jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setResultFromBoolean(env, (pval*)(long)result, jvalue);
break;
}
case SETRESULTFROMOBJECT: {
jobject jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setResultFromObject(env, (pval*)(long)result, jvalue);
break;
}
case SETRESULTFROMARRAY: {
sread(&result, sizeof result, 1, peer);
setResultFromArray(env, (pval*)(long)result);
break;
}
case NEXTELEMENT: {
sread(&result, sizeof result, 1, peer);
result=(jlong)(long)nextElement(env, (pval*)(long)result);
id(env, 0);
swrite(&result, sizeof result, 1, peer);
break;
}
case HASHINDEXUPDATE: {
jlong jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
result=(jlong)(long)hashIndexUpdate(env, (pval*)(long)result, jvalue);
id(env, 0);
swrite(&result, sizeof result, 1, peer);
break;
}
case HASHUPDATE: {
jbyteArray jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
result=(jlong)(long)hashUpdate(env, (pval*)(long)result, jvalue);
id(env, 0);
swrite(&result, sizeof result, 1, peer);
break;
}
case SETEXCEPTION: {
jbyteArray jvalue;
sread(&result, sizeof result, 1, peer);
sread(&jvalue, sizeof jvalue, 1, peer);
setException(env, (pval*)(long)result, jvalue);
break;
}
default: {
php_error(E_ERROR, "php_mod_java(%d): %s, %i",61, "protocol error: ", (unsigned int)c);
id(env, 0);
return 0;
}
}
// acknowledge
id(env, 0);
return 1;
}
static int handle_requests(proxyenv *env) {
// continue to handle server requests until the server says the
// packet is finished (one of the three main methods has sent 0)
while(handle_request(env));
return 0;
}
static int java_do_test_server(struct cfg*cfg TSRMLS_DC) {
char term=0;
int sock;
int n, c;
sock = socket (PF_UNIX, SOCK_STREAM, 0);
assert(sock);
if(!sock) return FAILURE;
n = connect(sock,(struct sockaddr*)&cfg->saddr, sizeof cfg->saddr);
if(n!=-1) c = write(sock, &term, sizeof term);
close(sock);
return (n!=-1 && c==1)?SUCCESS:FAILURE;
}
int java_test_server(struct cfg*cfg TSRMLS_DC) {
int count=15;
if(java_do_test_server(cfg TSRMLS_CC)==SUCCESS) return SUCCESS;
/* wait for the server that has just started */
while(cfg->cid && (java_do_test_server(cfg TSRMLS_CC)==FAILURE) && count--) {
php_error(E_NOTICE, "php_mod_java(%d): waiting for server another %d seconds",57, count);
sleep(1);
if(waitpid(cfg->cid, NULL, WNOHANG)) cfg->cid=0; // child died
}
return (cfg->cid && count)?SUCCESS:FAILURE;
}
int java_connect_to_server(struct cfg*cfg TSRMLS_DC) {
int sock, s, i, n, len;
FILE *fd;
sock = socket (PF_UNIX, SOCK_STREAM, 0);
assert(sock);
n = connect(sock,(struct sockaddr*)&cfg->saddr, sizeof cfg->saddr);
if(n==-1) {
php_error(E_WARNING, "php_mod_java(%d): Could not connect to server: %s -- Have you started the java bridge?",52, strerror(errno));
return FAILURE;
}
FILE *peer = fdopen(sock, "r+");
assert(peer);
JG(jenv)=java_createSecureEnvironment(peer, handle_requests);
JG(reflect_class) = (*JG(jenv))->FindClass(JG(jenv), "JavaBridge");
if(abort_on_error(JG(jenv), 3 TSRMLS_CC)) return FAILURE;
jobject local_php_reflect = (*JG(jenv))->AllocObject(JG(jenv), JG(reflect_class));
if(abort_on_error(JG(jenv), 4 TSRMLS_CC)) return FAILURE;
JG(php_reflect) = (*JG(jenv))->NewGlobalRef(JG(jenv), local_php_reflect);
if(abort_on_error(JG(jenv), 5 TSRMLS_CC)) return FAILURE;
return SUCCESS;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.