summaryrefslogtreecommitdiff
path: root/matlab/lib/+IceInternal/EncapsEncoder11.m
blob: f0c3849b29c6a7a0aba3f8cf1a508f16175bb53c (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
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
%{
**********************************************************************

Copyright (c) 2003-present 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.

**********************************************************************
%}

classdef EncapsEncoder11 < IceInternal.EncapsEncoder
    methods
        function obj = EncapsEncoder11(os, encaps)
            obj = obj@IceInternal.EncapsEncoder(os, encaps);
            obj.current = [];
            obj.valueIdIndex = 1;
            obj.marshaledMap = containers.Map('KeyType', 'int32', 'ValueType', 'int32');
        end

        function writeValue(obj, v)
            if isempty(v)
                obj.os.writeSize(0);
            else
                if ~isempty(obj.current) && obj.encaps.format == Ice.FormatType.SlicedFormat
                    %
                    % If writing an instance within a slice and using the sliced
                    % format, write an index from the instance indirection
                    % table. The indirect instance table is encoded at the end of
                    % each slice and is always read (even if the Slice is
                    % unknown).
                    %
                    if ~obj.current.indirectionMap.isKey(v.iceInternal_)
                        obj.current.indirectionTable{end + 1} = v;
                        idx = length(obj.current.indirectionTable); % 0 is reserved for nil
                        obj.current.indirectionMap(v.iceInternal_) = idx;
                        obj.os.writeSize(idx);
                    else
                        obj.os.writeSize(obj.current.indirectionMap(v.iceInternal_));
                    end
                else
                    obj.writeInstance(v); % Write the instance or a reference if already marshaled.
                end
            end
        end

        function startInstance(obj, sliceType, slicedData)
            if isempty(obj.current)
                obj.current = IceInternal.EncapsEncoder11_InstanceData([]);
            else
                if isempty(obj.current.next)
                    obj.current = IceInternal.EncapsEncoder11_InstanceData(obj.current);
                else
                    obj.current = obj.current.next;
                end
            end
            obj.current.sliceType = sliceType;
            obj.current.firstSlice = true;

            if ~isempty(slicedData)
                obj.writeSlicedData(slicedData);
            end
        end

        function endInstance(obj)
            obj.current = obj.current.previous;
        end

        function startSlice(obj, typeId, compactId, last)
            import IceInternal.Protocol;
            assert(isempty(obj.current.indirectionTable) && isempty(obj.current.indirectionMap));

            obj.current.sliceFlagsPos = obj.os.getPos() + 1;

            obj.current.sliceFlags = uint8(0);
            if obj.encaps.format == Ice.FormatType.SlicedFormat
                % Encode the slice size if using the sliced format.
                obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_SLICE_SIZE);
            end
            if last
                % This is the last slice.
                obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_IS_LAST_SLICE);
            end

            %obj.os.writeByte(0); % Placeholder for the slice flags
            obj.os.buf.resize(obj.os.buf.size + 1);

            %
            % For instance slices, encode the flag and the type ID either as a
            % string or index. For exception slices, always encode the type ID
            % as a string.
            %
            if obj.current.sliceType == IceInternal.SliceType.ValueSlice
                %
                % Encode the type ID (only in the first slice for the compact encoding).
                %
                if obj.encaps.format == Ice.FormatType.SlicedFormat || obj.current.firstSlice
                    if compactId >= 0
                        obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_TYPE_ID_COMPACT);
                        obj.os.writeSize(compactId);
                    else
                        index = obj.registerTypeId(typeId);
                        if index < 0
                            obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_TYPE_ID_STRING);
                            obj.os.writeString(typeId);
                        else
                            obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_TYPE_ID_INDEX);
                            obj.os.writeSize(index);
                        end
                    end
                end
            else
                obj.os.writeString(typeId);
            end

            if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_SLICE_SIZE)
                %obj.os.writeInt(0); % Placeholder for the slice length.
                obj.os.buf.resize(obj.os.buf.size + 4);
            end

            obj.current.writeSlice = obj.os.getPos() + 1;
            obj.current.firstSlice = false;
        end

        function endSlice(obj)
            import IceInternal.Protocol;
            %
            % Write the optional member end marker if some optional members
            % were encoded. Note that the optional members are encoded before
            % the indirection table and are included in the slice size.
            %
            if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS)
                obj.os.writeByte(Protocol.OPTIONAL_END_MARKER);
            end

            %
            % Write the slice length if necessary.
            %
            if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_SLICE_SIZE)
                sz = obj.os.getPos() - obj.current.writeSlice + 4 + 1;
                obj.os.rewriteInt(sz, obj.current.writeSlice - 4);
            end

            %
            % Only write the indirection table if it contains entries.
            %
            if ~isempty(obj.current.indirectionTable)
                assert(obj.encaps.format == Ice.FormatType.SlicedFormat);
                obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_INDIRECTION_TABLE);

                %
                % Write the indirection instance table.
                %
                obj.os.writeSize(length(obj.current.indirectionTable));
                for i = 1:length(obj.current.indirectionTable)
                    obj.writeInstance(obj.current.indirectionTable{i});
                end
                obj.current.indirectionTable = {};
                obj.current.indirectionMap = containers.Map('KeyType', 'int32', 'ValueType', 'int32');
            end

            %
            % Finally, update the slice flags.
            %
            obj.os.rewriteByte(obj.current.sliceFlags, obj.current.sliceFlagsPos);
        end

        function r = writeOptional(obj, tag, format)
            import IceInternal.Protocol;
            if isempty(obj.current)
                r = obj.os.writeOptionalImpl(tag, format);
            else
                if obj.os.writeOptionalImpl(tag, format)
                    obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS);
                    r = true;
                else
                    r = false;
                end
            end
        end
    end
    methods(Access=private)
        function writeSlicedData(obj, slicedData)
            import IceInternal.Protocol;
            assert(~isempty(slicedData));

            %
            % We only remarshal preserved slices if we are using the sliced
            % format. Otherwise, we ignore the preserved slices, which
            % essentially "slices" the instance into the most-derived type
            % known by the sender.
            %
            if obj.encaps.format ~= Ice.FormatType.SlicedFormat
                return;
            end

            for i = 1:length(slicedData.slices)
                info = slicedData.slices{i};

                obj.startSlice(info.typeId, info.compactId, info.isLastSlice);

                %
                % Write the bytes associated with this slice.
                %
                obj.os.writeBlob(info.bytes);

                if info.hasOptionalMembers
                    obj.current.sliceFlags = bitor(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS);
                end

                %
                % Make sure to also re-write the instance indirection table.
                %
                if ~isempty(info.instances)
                    for j = 1:length(info.instances)
                        v = info.instances{j};
                        obj.current.indirectionTable{end + 1} = v;
                    end
                end

                obj.endSlice();
            end
        end

        function writeInstance(obj, v)
            assert(~isempty(v));

            %
            % If the instance was already marshaled, just write its ID.
            %
            if obj.marshaledMap.isKey(v.iceInternal_)
                obj.os.writeSize(obj.marshaledMap(v.iceInternal_));
                return;
            end

            %
            % We haven't seen this instance previously, create a new ID,
            % insert it into the marshaled map, and write the instance.
            %
            obj.valueIdIndex = obj.valueIdIndex + 1;
            obj.marshaledMap(v.iceInternal_) = obj.valueIdIndex;

            try
                v.ice_preMarshal();
            catch ex
                msg = sprintf('exception raised by ice_preMarshal:\n%s', getReport(ex, 'extended'));
                obj.os.getCommunicator().getLogger().warning(msg);
            end

            obj.os.writeSize(1); % Class instance marker.
            v.iceWrite(obj.os);
        end
    end
    properties(Access=private)
        current
        valueIdIndex
        marshaledMap
    end
end