~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to php/src/IcePHP/Endpoint.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// **********************************************************************
 
2
//
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
 
4
//
 
5
// This copy of Ice is licensed to you under the terms described in the
 
6
// ICE_LICENSE file included in this distribution.
 
7
//
 
8
// **********************************************************************
 
9
 
 
10
#include <Endpoint.h>
 
11
#include <Util.h>
 
12
 
 
13
using namespace std;
 
14
using namespace IcePHP;
 
15
 
 
16
ZEND_EXTERN_MODULE_GLOBALS(ice)
 
17
 
 
18
//
 
19
// Class entries represent the PHP class implementations we have registered.
 
20
//
 
21
static zend_class_entry* endpointClassEntry = 0;
 
22
 
 
23
static zend_class_entry* endpointInfoClassEntry = 0;
 
24
static zend_class_entry* ipEndpointInfoClassEntry = 0;
 
25
static zend_class_entry* tcpEndpointInfoClassEntry = 0;
 
26
static zend_class_entry* udpEndpointInfoClassEntry = 0;
 
27
static zend_class_entry* opaqueEndpointInfoClassEntry = 0;
 
28
 
 
29
//
 
30
// Ice::Endpoint support.
 
31
//
 
32
static zend_object_handlers _endpointHandlers;
 
33
static zend_object_handlers _endpointInfoHandlers;
 
34
 
 
35
extern "C"
 
36
{
 
37
static zend_object_value handleEndpointAlloc(zend_class_entry* TSRMLS_DC);
 
38
static void handleEndpointFreeStorage(void* TSRMLS_DC);
 
39
 
 
40
static zend_object_value handleEndpointInfoAlloc(zend_class_entry* TSRMLS_DC);
 
41
static void handleEndpointInfoFreeStorage(void* TSRMLS_DC);
 
42
}
 
43
 
 
44
ZEND_METHOD(Ice_Endpoint, __construct)
 
45
{
 
46
    runtimeError("Endpoint cannot be instantiated" TSRMLS_CC);
 
47
}
 
48
 
 
49
ZEND_METHOD(Ice_Endpoint, __toString)
 
50
{
 
51
    if(ZEND_NUM_ARGS() > 0)
 
52
    {
 
53
        WRONG_PARAM_COUNT;
 
54
    }
 
55
 
 
56
    Ice::EndpointPtr _this = Wrapper<Ice::EndpointPtr>::value(getThis() TSRMLS_CC);
 
57
    assert(_this);
 
58
 
 
59
    try
 
60
    {
 
61
        string str = _this->toString();
 
62
        RETURN_STRINGL(STRCAST(str.c_str()), str.length(), 1);
 
63
    }
 
64
    catch(const IceUtil::Exception& ex)
 
65
    {
 
66
        throwException(ex TSRMLS_CC);
 
67
        RETURN_NULL();
 
68
    }
 
69
}
 
70
 
 
71
ZEND_METHOD(Ice_Endpoint, toString)
 
72
{
 
73
    ZEND_MN(Ice_Endpoint___toString)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
 
74
}
 
75
 
 
76
ZEND_METHOD(Ice_Endpoint, getInfo)
 
77
{
 
78
    if(ZEND_NUM_ARGS() > 0)
 
79
    {
 
80
        WRONG_PARAM_COUNT;
 
81
    }
 
82
 
 
83
    Ice::EndpointPtr _this = Wrapper<Ice::EndpointPtr>::value(getThis() TSRMLS_CC);
 
84
    assert(_this);
 
85
 
 
86
    if(!createEndpointInfo(return_value, _this->getInfo() TSRMLS_CC))
 
87
    {
 
88
        RETURN_NULL();
 
89
    }
 
90
}
 
91
 
 
92
#ifdef _WIN32
 
93
extern "C"
 
94
#endif
 
95
static zend_object_value
 
96
handleEndpointAlloc(zend_class_entry* ce TSRMLS_DC)
 
97
{
 
98
    zend_object_value result;
 
99
 
 
100
    Wrapper<Ice::EndpointPtr>* obj = Wrapper<Ice::EndpointPtr>::create(ce TSRMLS_CC);
 
101
    assert(obj);
 
102
 
 
103
    result.handle = zend_objects_store_put(obj, 0, (zend_objects_free_object_storage_t)handleEndpointFreeStorage,
 
104
                                           0 TSRMLS_CC);
 
105
    result.handlers = &_endpointHandlers;
 
106
 
 
107
    return result;
 
108
}
 
109
 
 
110
#ifdef _WIN32
 
111
extern "C"
 
112
#endif
 
113
static void
 
114
handleEndpointFreeStorage(void* p TSRMLS_DC)
 
115
{
 
116
    Wrapper<Ice::EndpointPtr>* obj = static_cast<Wrapper<Ice::EndpointPtr>*>(p);
 
117
    delete obj->ptr;
 
118
    zend_objects_free_object_storage(static_cast<zend_object*>(p) TSRMLS_CC);
 
119
}
 
120
 
 
121
ZEND_METHOD(Ice_EndpointInfo, __construct)
 
122
{
 
123
    runtimeError("EndpointInfo cannot be instantiated" TSRMLS_CC);
 
124
}
 
125
 
 
126
ZEND_METHOD(Ice_EndpointInfo, type)
 
127
{
 
128
    if(ZEND_NUM_ARGS() > 0)
 
129
    {
 
130
        WRONG_PARAM_COUNT;
 
131
    }
 
132
 
 
133
    Ice::EndpointInfoPtr _this = Wrapper<Ice::EndpointInfoPtr>::value(getThis() TSRMLS_CC);
 
134
    assert(_this);
 
135
 
 
136
    try
 
137
    {
 
138
        short type = static_cast<short>(_this->type());
 
139
        RETURN_LONG(type);
 
140
    }
 
141
    catch(const IceUtil::Exception& ex)
 
142
    {
 
143
        throwException(ex TSRMLS_CC);
 
144
        RETURN_NULL();
 
145
    }
 
146
}
 
147
 
 
148
ZEND_METHOD(Ice_EndpointInfo, datagram)
 
149
{
 
150
    if(ZEND_NUM_ARGS() > 0)
 
151
    {
 
152
        WRONG_PARAM_COUNT;
 
153
    }
 
154
 
 
155
    Ice::EndpointInfoPtr _this = Wrapper<Ice::EndpointInfoPtr>::value(getThis() TSRMLS_CC);
 
156
    assert(_this);
 
157
 
 
158
    try
 
159
    {
 
160
        RETURN_BOOL(_this->datagram() ? 1 : 0);
 
161
    }
 
162
    catch(const IceUtil::Exception& ex)
 
163
    {
 
164
        throwException(ex TSRMLS_CC);
 
165
        RETURN_NULL();
 
166
    }
 
167
}
 
168
 
 
169
ZEND_METHOD(Ice_EndpointInfo, secure)
 
170
{
 
171
    if(ZEND_NUM_ARGS() > 0)
 
172
    {
 
173
        WRONG_PARAM_COUNT;
 
174
    }
 
175
 
 
176
    Ice::EndpointInfoPtr _this = Wrapper<Ice::EndpointInfoPtr>::value(getThis() TSRMLS_CC);
 
177
    assert(_this);
 
178
 
 
179
    try
 
180
    {
 
181
        RETURN_BOOL(_this->secure() ? 1 : 0);
 
182
    }
 
183
    catch(const IceUtil::Exception& ex)
 
184
    {
 
185
        throwException(ex TSRMLS_CC);
 
186
        RETURN_NULL();
 
187
    }
 
188
}
 
189
 
 
190
#ifdef _WIN32
 
191
extern "C"
 
192
#endif
 
193
static zend_object_value
 
194
handleEndpointInfoAlloc(zend_class_entry* ce TSRMLS_DC)
 
195
{
 
196
    zend_object_value result;
 
197
 
 
198
    Wrapper<Ice::EndpointPtr>* obj = Wrapper<Ice::EndpointPtr>::create(ce TSRMLS_CC);
 
199
    assert(obj);
 
200
 
 
201
    result.handle = zend_objects_store_put(obj, 0, (zend_objects_free_object_storage_t)handleEndpointInfoFreeStorage,
 
202
                                           0 TSRMLS_CC);
 
203
    result.handlers = &_endpointInfoHandlers;
 
204
 
 
205
    return result;
 
206
}
 
207
 
 
208
#ifdef _WIN32
 
209
extern "C"
 
210
#endif
 
211
static void
 
212
handleEndpointInfoFreeStorage(void* p TSRMLS_DC)
 
213
{
 
214
    Wrapper<Ice::EndpointInfoPtr>* obj = static_cast<Wrapper<Ice::EndpointInfoPtr>*>(p);
 
215
    delete obj->ptr;
 
216
    zend_objects_free_object_storage(static_cast<zend_object*>(p) TSRMLS_CC);
 
217
}
 
218
 
 
219
static function_entry _interfaceMethods[] =
 
220
{
 
221
    {0, 0, 0}
 
222
};
 
223
 
 
224
//
 
225
// Predefined methods for Endpoint.
 
226
//
 
227
static function_entry _endpointMethods[] =
 
228
{
 
229
    ZEND_ME(Ice_Endpoint, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
 
230
    ZEND_ME(Ice_Endpoint, __toString, NULL, ZEND_ACC_PUBLIC)
 
231
    ZEND_ME(Ice_Endpoint, toString, NULL, ZEND_ACC_PUBLIC)
 
232
    ZEND_ME(Ice_Endpoint, getInfo, NULL, ZEND_ACC_PUBLIC)
 
233
    {0, 0, 0}
 
234
};
 
235
 
 
236
//
 
237
// Predefined methods for EndpointInfo.
 
238
//
 
239
static function_entry _endpointInfoMethods[] =
 
240
{
 
241
    ZEND_ME(Ice_EndpointInfo, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
 
242
    ZEND_ME(Ice_EndpointInfo, type, NULL, ZEND_ACC_PUBLIC)
 
243
    ZEND_ME(Ice_EndpointInfo, datagram, NULL, ZEND_ACC_PUBLIC)
 
244
    ZEND_ME(Ice_EndpointInfo, secure, NULL, ZEND_ACC_PUBLIC)
 
245
    {0, 0, 0}
 
246
};
 
247
 
 
248
bool
 
249
IcePHP::endpointInit(TSRMLS_D)
 
250
{
 
251
    //
 
252
    // We register an interface and a class that implements the interface. This allows
 
253
    // applications to safely include the Slice-generated code for the type.
 
254
    //
 
255
 
 
256
    //
 
257
    // Register the Endpoint interface.
 
258
    //
 
259
    zend_class_entry ce;
 
260
#ifdef ICEPHP_USE_NAMESPACES
 
261
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("Endpoint"), _interfaceMethods);
 
262
#else
 
263
    INIT_CLASS_ENTRY(ce, "Ice_Endpoint", _interfaceMethods);
 
264
#endif
 
265
    zend_class_entry* endpointInterface = zend_register_internal_interface(&ce TSRMLS_CC);
 
266
 
 
267
    //
 
268
    // Register the Endpoint class.
 
269
    //
 
270
    INIT_CLASS_ENTRY(ce, "IcePHP_Endpoint", _endpointMethods);
 
271
    ce.create_object = handleEndpointAlloc;
 
272
    endpointClassEntry = zend_register_internal_class(&ce TSRMLS_CC);
 
273
    memcpy(&_endpointHandlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 
274
    zend_class_implements(endpointClassEntry TSRMLS_CC, 1, endpointInterface);
 
275
 
 
276
    //
 
277
    // Register the EndpointInfo class.
 
278
    //
 
279
#ifdef ICEPHP_USE_NAMESPACES
 
280
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("EndpointInfo"), _endpointInfoMethods);
 
281
#else
 
282
    INIT_CLASS_ENTRY(ce, "Ice_EndpointInfo", _endpointInfoMethods);
 
283
#endif
 
284
    ce.create_object = handleEndpointInfoAlloc;
 
285
    endpointInfoClassEntry = zend_register_internal_class(&ce TSRMLS_CC);
 
286
    memcpy(&_endpointInfoHandlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 
287
    zend_declare_property_long(endpointInfoClassEntry, STRCAST("timeout"), sizeof("timeout") - 1, 0,
 
288
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
289
    zend_declare_property_bool(endpointInfoClassEntry, STRCAST("compress"), sizeof("compress") - 1, 0,
 
290
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
291
 
 
292
    //
 
293
    // Register the IPEndpointInfo class.
 
294
    //
 
295
#ifdef ICEPHP_USE_NAMESPACES
 
296
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("IPEndpointInfo"), NULL);
 
297
#else
 
298
    INIT_CLASS_ENTRY(ce, "Ice_IPEndpointInfo", NULL);
 
299
#endif
 
300
    ce.create_object = handleEndpointInfoAlloc;
 
301
    ipEndpointInfoClassEntry = zend_register_internal_class_ex(&ce, endpointInfoClassEntry, NULL TSRMLS_CC);
 
302
    zend_declare_property_string(ipEndpointInfoClassEntry, STRCAST("host"), sizeof("host") - 1, STRCAST(""),
 
303
                                 ZEND_ACC_PUBLIC TSRMLS_CC);
 
304
    zend_declare_property_long(ipEndpointInfoClassEntry, STRCAST("port"), sizeof("port") - 1, 0,
 
305
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
306
 
 
307
    //
 
308
    // Register the TCPEndpointInfo class.
 
309
    //
 
310
#ifdef ICEPHP_USE_NAMESPACES
 
311
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("TCPEndpointInfo"), NULL);
 
312
#else
 
313
    INIT_CLASS_ENTRY(ce, "Ice_TCPEndpointInfo", NULL);
 
314
#endif
 
315
    ce.create_object = handleEndpointInfoAlloc;
 
316
    tcpEndpointInfoClassEntry = zend_register_internal_class_ex(&ce, ipEndpointInfoClassEntry, NULL TSRMLS_CC);
 
317
 
 
318
    //
 
319
    // Register the UDPEndpointInfo class.
 
320
    //
 
321
#ifdef ICEPHP_USE_NAMESPACES
 
322
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("UDPEndpointInfo"), NULL);
 
323
#else
 
324
    INIT_CLASS_ENTRY(ce, "Ice_UDPEndpointInfo", NULL);
 
325
#endif
 
326
    ce.create_object = handleEndpointInfoAlloc;
 
327
    udpEndpointInfoClassEntry = zend_register_internal_class_ex(&ce, ipEndpointInfoClassEntry, NULL TSRMLS_CC);
 
328
    zend_declare_property_long(udpEndpointInfoClassEntry, STRCAST("protocolMajor"), sizeof("protocolMajor") - 1, 0,
 
329
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
330
    zend_declare_property_long(udpEndpointInfoClassEntry, STRCAST("protocolMinor"), sizeof("protocolMinor") - 1, 0,
 
331
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
332
    zend_declare_property_long(udpEndpointInfoClassEntry, STRCAST("encodingMajor"), sizeof("encodingMajor") - 1, 0,
 
333
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
334
    zend_declare_property_long(udpEndpointInfoClassEntry, STRCAST("encodingMinor"), sizeof("encodingMinor") - 1, 0,
 
335
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
336
    zend_declare_property_string(udpEndpointInfoClassEntry, STRCAST("mcastInterface"), sizeof("mcastInterface") - 1,
 
337
                                 STRCAST(""), ZEND_ACC_PUBLIC TSRMLS_CC);
 
338
    zend_declare_property_long(udpEndpointInfoClassEntry, STRCAST("mcastTtl"), sizeof("mcastTtl") - 1, 0,
 
339
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
340
 
 
341
    //
 
342
    // Register the OpaqueEndpointInfo class.
 
343
    //
 
344
#ifdef ICEPHP_USE_NAMESPACES
 
345
    INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("OpaqueEndpointInfo"), NULL);
 
346
#else
 
347
    INIT_CLASS_ENTRY(ce, "Ice_OpaqueEndpointInfo", NULL);
 
348
#endif
 
349
    ce.create_object = handleEndpointInfoAlloc;
 
350
    opaqueEndpointInfoClassEntry = zend_register_internal_class_ex(&ce, endpointInfoClassEntry, NULL TSRMLS_CC);
 
351
    zend_declare_property_null(opaqueEndpointInfoClassEntry, STRCAST("rawBytes"), sizeof("rawBytes") - 1,
 
352
                               ZEND_ACC_PUBLIC TSRMLS_CC);
 
353
 
 
354
    return true;
 
355
}
 
356
 
 
357
bool
 
358
IcePHP::createEndpoint(zval* zv, const Ice::EndpointPtr& p TSRMLS_DC)
 
359
{
 
360
    if(object_init_ex(zv, endpointClassEntry) != SUCCESS)
 
361
    {
 
362
        runtimeError("unable to initialize endpoint" TSRMLS_CC);
 
363
        return false;
 
364
    }
 
365
 
 
366
    Wrapper<Ice::EndpointPtr>* obj = Wrapper<Ice::EndpointPtr>::extract(zv TSRMLS_CC);
 
367
    assert(obj);
 
368
    assert(!obj->ptr);
 
369
    obj->ptr = new Ice::EndpointPtr(p);
 
370
 
 
371
    return true;
 
372
}
 
373
 
 
374
bool
 
375
IcePHP::fetchEndpoint(zval* zv, Ice::EndpointPtr& endpoint TSRMLS_DC)
 
376
{
 
377
    if(ZVAL_IS_NULL(zv))
 
378
    {
 
379
        endpoint = 0;
 
380
    }
 
381
    else
 
382
    {
 
383
        if(Z_TYPE_P(zv) != IS_OBJECT || !checkClass(Z_OBJCE_P(zv), endpointClassEntry))
 
384
        {
 
385
            invalidArgument("value is not an endpoint" TSRMLS_CC);
 
386
            return false;
 
387
        }
 
388
        Wrapper<Ice::EndpointPtr>* obj = Wrapper<Ice::EndpointPtr>::extract(zv TSRMLS_CC);
 
389
        if(!obj)
 
390
        {
 
391
            return false;
 
392
        }
 
393
        endpoint = *obj->ptr;
 
394
    }
 
395
    return true;
 
396
}
 
397
 
 
398
bool
 
399
IcePHP::createEndpointInfo(zval* zv, const Ice::EndpointInfoPtr& p TSRMLS_DC)
 
400
{
 
401
    int status;
 
402
    if(Ice::TCPEndpointInfoPtr::dynamicCast(p))
 
403
    {
 
404
        status = object_init_ex(zv, tcpEndpointInfoClassEntry);
 
405
    }
 
406
    else if(Ice::UDPEndpointInfoPtr::dynamicCast(p))
 
407
    {
 
408
        Ice::UDPEndpointInfoPtr info = Ice::UDPEndpointInfoPtr::dynamicCast(p);
 
409
        if((status = object_init_ex(zv, udpEndpointInfoClassEntry)) == SUCCESS)
 
410
        {
 
411
            add_property_long(zv, STRCAST("protocolMajor"), static_cast<long>(info->protocolMajor));
 
412
            add_property_long(zv, STRCAST("protocolMinor"), static_cast<long>(info->protocolMinor));
 
413
            add_property_long(zv, STRCAST("encodingMajor"), static_cast<long>(info->encodingMajor));
 
414
            add_property_long(zv, STRCAST("encodingMinor"), static_cast<long>(info->encodingMinor));
 
415
            add_property_string(zv, STRCAST("mcastInterface"), const_cast<char*>(info->mcastInterface.c_str()), 1);
 
416
            add_property_long(zv, STRCAST("mcastTtl"), static_cast<long>(info->mcastTtl));
 
417
        }
 
418
    }
 
419
    else if(Ice::OpaqueEndpointInfoPtr::dynamicCast(p))
 
420
    {
 
421
        Ice::OpaqueEndpointInfoPtr info = Ice::OpaqueEndpointInfoPtr::dynamicCast(p);
 
422
        if((status = object_init_ex(zv, opaqueEndpointInfoClassEntry)) == SUCCESS)
 
423
        {
 
424
            zval* rawBytes;
 
425
            MAKE_STD_ZVAL(rawBytes);
 
426
            array_init(rawBytes);
 
427
            for(Ice::ByteSeq::iterator i = info->rawBytes.begin(); i != info->rawBytes.end(); ++i)
 
428
            {
 
429
                add_next_index_long(rawBytes, *i & 0xff);
 
430
            }
 
431
            add_property_zval(zv, STRCAST("rawBytes"), rawBytes);
 
432
            zval_ptr_dtor(&rawBytes); // add_property_zval increased the refcount of rawBytes
 
433
        }
 
434
    }
 
435
    else if(Ice::IPEndpointInfoPtr::dynamicCast(p))
 
436
    {
 
437
        status = object_init_ex(zv, ipEndpointInfoClassEntry);
 
438
    }
 
439
    else
 
440
    {
 
441
        status = object_init_ex(zv, endpointInfoClassEntry);
 
442
    }
 
443
 
 
444
    if(status != SUCCESS)
 
445
    {
 
446
        runtimeError("unable to initialize endpoint info" TSRMLS_CC);
 
447
        return false;
 
448
    }
 
449
 
 
450
    if(Ice::IPEndpointInfoPtr::dynamicCast(p))
 
451
    {
 
452
        Ice::IPEndpointInfoPtr info = Ice::IPEndpointInfoPtr::dynamicCast(p);
 
453
        add_property_string(zv, STRCAST("host"), const_cast<char*>(info->host.c_str()), 1);
 
454
        add_property_long(zv, STRCAST("port"), static_cast<long>(info->port));
 
455
    }
 
456
 
 
457
    add_property_long(zv, STRCAST("timeout"), static_cast<long>(p->timeout));
 
458
    add_property_bool(zv, STRCAST("compress"), static_cast<long>(p->compress));
 
459
 
 
460
    Wrapper<Ice::EndpointInfoPtr>* obj = Wrapper<Ice::EndpointInfoPtr>::extract(zv TSRMLS_CC);
 
461
    assert(obj);
 
462
    assert(!obj->ptr);
 
463
    obj->ptr = new Ice::EndpointInfoPtr(p);
 
464
 
 
465
    return true;
 
466
}