summaryrefslogtreecommitdiff
path: root/matlab/src/IceMatlab/Endpoint.cpp
blob: 7868846926240bb306cac6d0f67ba5557b219e78 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#define EXPORT_FCNS

#include <Ice/Ice.h>
#include "icematlab.h"
#include "Endpoint.h"
#include "Util.h"

#define DEREF(x) (*(reinterpret_cast<shared_ptr<Ice::Endpoint>*>(x)))
#define SELF DEREF(self)

using namespace std;
using namespace IceMatlab;

void*
IceMatlab::createEndpoint(shared_ptr<Ice::Endpoint> p)
{
    return new shared_ptr<Ice::Endpoint>(p);
}

shared_ptr<Ice::Endpoint>
IceMatlab::getEndpoint(void* p)
{
    return DEREF(p);
}

namespace
{

enum Field
{
    Type = 0,
    Datagram,
    Secure,
    Underlying,
    Timeout,
    Compress,
    Host,
    Port,
    SourceAddress,
    McastInterface,
    McastTtl,
    Resource,
    RawEncoding,
    RawBytes,
    NumFields // Number of fields in structure, must be last
};

static const char* infoFields[] =
{
    "type",
    "datagram",
    "secure",
    "underlying",
    "timeout",
    "compress",
    "host",
    "port",
    "sourceAddress",
    "mcastInterface",
    "mcastTtl",
    "resource",
    "rawEncoding",
    "rawBytes"
};

mxArray*
createInfo(shared_ptr<Ice::EndpointInfo> info)
{
    //
    // Create and return a struct array containing the fields that describe the EndpointInfo object.
    // Some fields will be left unused.
    //

    mwSize dims[2] = {1, 1};
    auto r = mxCreateStructArray(2, dims, Field::NumFields, infoFields);
    mxSetFieldByNumber(r, 0, Field::Type, createInt(info->type())); // May be overridden below.
    mxSetFieldByNumber(r, 0, Field::Datagram, createBool(info->datagram()));
    mxSetFieldByNumber(r, 0, Field::Secure, createBool(info->secure()));
    mxSetFieldByNumber(r, 0, Field::Timeout, createInt(info->timeout));
    mxSetFieldByNumber(r, 0, Field::Compress, createBool(info->compress));

    if(info->underlying)
    {
        auto u = createInfo(info->underlying);
        mxSetFieldByNumber(r, 0, Field::Underlying, u);
    }

    //
    // Don't use info->type() to determine the type of the EndpointInfo object. When an endpoint is the
    // underlying endpoint of a parent, the child's value for type() is the same as its parent. We have
    // to use type casts instead.
    //

    shared_ptr<Ice::IPEndpointInfo> ipInfo = dynamic_pointer_cast<Ice::IPEndpointInfo>(info);
    if(ipInfo)
    {
        mxSetFieldByNumber(r, 0, Field::Host, createStringFromUTF8(ipInfo->host));
        mxSetFieldByNumber(r, 0, Field::Port, createInt(ipInfo->port));
        mxSetFieldByNumber(r, 0, Field::SourceAddress, createStringFromUTF8(ipInfo->sourceAddress));
    }

    shared_ptr<Ice::OpaqueEndpointInfo> opaqueInfo = dynamic_pointer_cast<Ice::OpaqueEndpointInfo>(info);
    if(opaqueInfo)
    {
        mxSetFieldByNumber(r, 0, Field::RawEncoding, createEncodingVersion(opaqueInfo->rawEncoding));
        Ice::Byte* p = &opaqueInfo->rawBytes[0];
        mxSetFieldByNumber(r, 0, Field::RawBytes, createByteArray(p, p + opaqueInfo->rawBytes.size()));
    }

    shared_ptr<Ice::UDPEndpointInfo> udpInfo = dynamic_pointer_cast<Ice::UDPEndpointInfo>(info);
    if(udpInfo)
    {
        mxSetFieldByNumber(r, 0, Field::Type, createInt(Ice::UDPEndpointType));
        mxSetFieldByNumber(r, 0, Field::McastInterface, createStringFromUTF8(udpInfo->mcastInterface));
        mxSetFieldByNumber(r, 0, Field::McastTtl, createInt(udpInfo->mcastTtl));
    }

    if(dynamic_pointer_cast<Ice::TCPEndpointInfo>(info))
    {
        mxSetFieldByNumber(r, 0, Field::Type, createInt(Ice::TCPEndpointType));
    }

    shared_ptr<Ice::WSEndpointInfo> wsInfo = dynamic_pointer_cast<Ice::WSEndpointInfo>(info);
    if(wsInfo)
    {
        mxSetFieldByNumber(r, 0, Field::Resource, createStringFromUTF8(wsInfo->resource));
    }

    /* TODO: If we link with IceSSL
    if(dynamic_pointer_cast<IceSSL::EndpointInfo>(info))
    {
        mxSetFieldByNumber(r, 0, Field::Type, createInt(Ice::SSLEndpointType));
    }
    */

    return r;
}

}

extern "C"
{

EXPORTED_FUNCTION mxArray*
Ice_Endpoint__release(void* self)
{
    delete &SELF;
    return 0;
}

EXPORTED_FUNCTION mxArray*
Ice_Endpoint_equals(void* self, void* other)
{
    assert(other); // Wrapper only calls this function for non-nil arguments.
    try
    {
        return createResultValue(createBool(Ice::targetEqualTo(SELF, DEREF(other))));
    }
    catch(const std::exception& ex)
    {
        return createResultException(convertException(ex));
    }
}

EXPORTED_FUNCTION mxArray*
Ice_Endpoint_toString(void* self)
{
    try
    {
        return createResultValue(createStringFromUTF8(SELF->toString()));
    }
    catch(const std::exception& ex)
    {
        return createResultException(convertException(ex));
    }
}

EXPORTED_FUNCTION mxArray*
Ice_Endpoint_getInfo(void* self)
{
    try
    {
        shared_ptr<Ice::EndpointInfo> info = SELF->getInfo();
        return createResultValue(createInfo(info));
    }
    catch(const std::exception& ex)
    {
        return createResultException(convertException(ex));
    }
}

}