summaryrefslogtreecommitdiff
path: root/matlab/src/Endpoint.cpp
blob: 9d91be92c8f18744e5b8311a8026f453b4a44e14 (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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/Ice.h>
#include <IceSSL/IceSSL.h>
#include "ice.h"
#include "Util.h"

using namespace std;
using namespace IceMatlab;

namespace
{

enum Field
{
    Type = 0,
    InfoType,
    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",
    "infoType",
    "datagram",
    "secure",
    "underlying",
    "timeout",
    "compress",
    "host",
    "port",
    "sourceAddress",
    "mcastInterface",
    "mcastTtl",
    "resource",
    "rawEncoding",
    "rawBytes"
};

mxArray*
createInfo(const 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()));
    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.
    //

    auto 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));
    }

    auto 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()));
    }

    auto udpInfo = dynamic_pointer_cast<Ice::UDPEndpointInfo>(info);
    if(udpInfo)
    {
        mxSetFieldByNumber(r, 0, Field::InfoType, 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::InfoType, createInt(Ice::TCPEndpointType));
    }

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

    if(dynamic_pointer_cast<IceSSL::EndpointInfo>(info))
    {
        mxSetFieldByNumber(r, 0, Field::InfoType, createInt(Ice::SSLEndpointType));
    }

    return r;
}

}

extern "C"
{

mxArray*
Ice_Endpoint_unref(void* self)
{
    delete reinterpret_cast<shared_ptr<Ice::Endpoint>*>(self);
    return 0;
}

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(deref<Ice::Endpoint>(self),
                                                               deref<Ice::Endpoint>(other))));
    }
    catch(const std::exception& ex)
    {
        return createResultException(convertException(ex));
    }
}

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

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

}