aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/transport_rta/commands/rta_Command.c
blob: 4e0e91d07c5a46ee0d1313062893a9adcb9a01e9 (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implements the single wrapper for commands sent over the command channel
 *
 */

#include <config.h>

#include <LongBow/runtime.h>

#include <stdio.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Object.h>
#include <parc/algol/parc_DisplayIndented.h>

#include <ccnx/transport/transport_rta/commands/rta_Command.h>

/*
 * Internal definition of command types
 */
typedef enum {
    RtaCommandType_Unknown = 0,
    RtaCommandType_CreateProtocolStack,
    RtaCommandType_OpenConnection,
    RtaCommandType_CloseConnection,
    RtaCommandType_DestroyProtocolStack,
    RtaCommandType_ShutdownFramework,
    RtaCommandType_TransmitStatistics,
    RtaCommandType_Last
} _RtaCommandType;

struct rta_command {
    _RtaCommandType type;

    union {
        RtaCommandCloseConnection *closeConnection;
        RtaCommandOpenConnection *openConnection;
        RtaCommandCreateProtocolStack *createStack;
        RtaCommandDestroyProtocolStack *destroyStack;
        RtaCommandTransmitStatistics *transmitStats;

        // shutdown framework has no value it will be NULL
        // Statistics has no value
        void *noValue;
    } value;
};

static struct commandtype_to_string {
    _RtaCommandType type;
    const char *string;
} _RtaCommandTypeToString[] = {
    { .type = RtaCommandType_Unknown,              .string = "Unknown"              },
    { .type = RtaCommandType_CreateProtocolStack,  .string = "CreateProtocolStack"  },
    { .type = RtaCommandType_OpenConnection,       .string = "OpenConnection"       },
    { .type = RtaCommandType_CloseConnection,      .string = "CloseConnection"      },
    { .type = RtaCommandType_DestroyProtocolStack, .string = "DestroyProtocolStack" },
    { .type = RtaCommandType_ShutdownFramework,    .string = "ShutdownFramework"    },
    { .type = RtaCommandType_TransmitStatistics,   .string = "TransmitStatistics"   },
    { .type = RtaCommandType_Last,                 .string = NULL                   },
};

// ===============================
// Internal API

static void
_rtaCommand_Destroy(RtaCommand **commandPtr)
{
    RtaCommand *command = *commandPtr;
    switch (command->type) {
        case RtaCommandType_ShutdownFramework:
            // no inner-release needed
            break;

        case RtaCommandType_CreateProtocolStack:
            rtaCommandCreateProtocolStack_Release(&command->value.createStack);
            break;

        case RtaCommandType_OpenConnection:
            rtaCommandOpenConnection_Release(&command->value.openConnection);
            break;

        case RtaCommandType_CloseConnection:
            rtaCommandCloseConnection_Release(&command->value.closeConnection);
            break;

        case RtaCommandType_DestroyProtocolStack:
            rtaCommandDestroyProtocolStack_Release(&command->value.destroyStack);
            break;

        case RtaCommandType_TransmitStatistics:
            rtaCommandTransmitStatistics_Release(&command->value.transmitStats);
            break;

        default:
            trapIllegalValue(command->type, "Illegal command type %d", command->type);
            break;
    }
}

#ifdef Transport_DISABLE_VALIDATION
#  define _rtaCommand_OptionalAssertValid(_instance_)
#else
#  define _rtaCommand_OptionalAssertValid(_instance_) _rtaCommand_AssertValid(_instance_)
#endif

static void
_rtaCommand_AssertValid(const RtaCommand *command)
{
    assertNotNull(command, "RtaCommand must be non-null");
    assertTrue(RtaCommandType_Unknown < command->type && command->type < RtaCommandType_Last,
               "Invalid RtaCommand type, must be %d < type %d < %d",
               RtaCommandType_Unknown,
               command->type,
               RtaCommandType_Last);

    switch (command->type) {
        case RtaCommandType_ShutdownFramework:
            assertNull(command->value.noValue, "RtaCommand value must be null for ShutdownFramework or Statistics");
            break;

        case RtaCommandType_CreateProtocolStack:
            assertNotNull(command->value.createStack, "RtaCommand createStack member must be non-null");
            break;

        case RtaCommandType_OpenConnection:
            assertNotNull(command->value.openConnection, "RtaCommand openConnection member must be non-null");
            break;

        case RtaCommandType_CloseConnection:
            assertNotNull(command->value.closeConnection, "RtaCommand closeConnection member must be non-null");
            break;

        case RtaCommandType_DestroyProtocolStack:
            assertNotNull(command->value.destroyStack, "RtaCommand destroyStack member must be non-null");
            break;

        case RtaCommandType_TransmitStatistics:
            assertNotNull(command->value.transmitStats, "RtaCommand transmitStats member must be non-null");
            break;

        default:
            trapIllegalValue(command->type, "Illegal command type %d", command->type);
            break;
    }
}

parcObject_ExtendPARCObject(RtaCommand, _rtaCommand_Destroy, NULL, NULL, NULL, NULL, NULL, NULL);

parcObject_ImplementAcquire(rtaCommand, RtaCommand);

parcObject_ImplementRelease(rtaCommand, RtaCommand);

static RtaCommand *
_rtaCommand_Allocate(_RtaCommandType type)
{
    RtaCommand *command = parcObject_CreateInstance(RtaCommand);
    command->type = type;
    return command;
}

static const char *
_rtaCommand_TypeToString(const RtaCommand *command)
{
    for (int i = 0; _RtaCommandTypeToString[i].string != NULL; i++) {
        if (_RtaCommandTypeToString[i].type == command->type) {
            return _RtaCommandTypeToString[i].string;
        }
    }
    trapUnexpectedState("Command is not a valid type: %d", command->type);
}

// ===============================
// Public API

void
rtaCommand_Display(const RtaCommand *command, int indentation)
{
    _rtaCommand_OptionalAssertValid(command);

    parcDisplayIndented_PrintLine(indentation, "RtaCommand type %s (%d) value pointer %p\n",
                                  _rtaCommand_TypeToString(command), command->type, command->value);
}

/*
 * Gets a reference to itself and puts it in the ring buffer
 */
bool
rtaCommand_Write(const RtaCommand *command, PARCRingBuffer1x1 *commandRingBuffer)
{
    _rtaCommand_OptionalAssertValid(command);

    RtaCommand *reference = rtaCommand_Acquire(command);

    bool addedToRingBuffer = parcRingBuffer1x1_Put(commandRingBuffer, reference);

    if (!addedToRingBuffer) {
        // it was not stored in the ring, so we need to be responsible and release it
        rtaCommand_Release(&reference);
    }

    return addedToRingBuffer;
}

RtaCommand *
rtaCommand_Read(PARCRingBuffer1x1 *commandRingBuffer)
{
    RtaCommand *referenceFromRing = NULL;

    bool fetchedReference = parcRingBuffer1x1_Get(commandRingBuffer, (void **) &referenceFromRing);
    if (fetchedReference) {
        return referenceFromRing;
    }
    return NULL;
}

// ======================
// CLOSE CONNECTION

bool
rtaCommand_IsCloseConnection(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_CloseConnection);
}

RtaCommand *
rtaCommand_CreateCloseConnection(const RtaCommandCloseConnection *close)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_CloseConnection);
    command->value.closeConnection = rtaCommandCloseConnection_Acquire(close);
    return command;
}

const RtaCommandCloseConnection *
rtaCommand_GetCloseConnection(const RtaCommand *command)
{
    assertTrue(rtaCommand_IsCloseConnection(command), "Command is not CloseConnection");
    return command->value.closeConnection;
}

// ======================
// OPEN CONNECTION

bool
rtaCommand_IsOpenConnection(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_OpenConnection);
}

RtaCommand *
rtaCommand_CreateOpenConnection(const RtaCommandOpenConnection *open)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_OpenConnection);
    command->value.openConnection = rtaCommandOpenConnection_Acquire(open);
    return command;
}

const RtaCommandOpenConnection *
rtaCommand_GetOpenConnection(const RtaCommand *command)
{
    assertTrue(rtaCommand_IsOpenConnection(command), "Command is not OpenConnection");
    return command->value.openConnection;
}

// ======================
// CREATE STACK

bool
rtaCommand_IsCreateProtocolStack(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_CreateProtocolStack);
}

RtaCommand *
rtaCommand_CreateCreateProtocolStack(const RtaCommandCreateProtocolStack *createStack)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_CreateProtocolStack);
    command->value.createStack = rtaCommandCreateProtocolStack_Acquire(createStack);
    return command;
}

const RtaCommandCreateProtocolStack *
rtaCommand_GetCreateProtocolStack(const RtaCommand *command)
{
    assertTrue(rtaCommand_IsCreateProtocolStack(command), "Command is not CreateProtocolStack");
    return command->value.createStack;
}

bool
rtaCommand_IsDestroyProtocolStack(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_DestroyProtocolStack);
}

RtaCommand *
rtaCommand_CreateDestroyProtocolStack(const RtaCommandDestroyProtocolStack *destroyStack)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_DestroyProtocolStack);
    command->value.destroyStack = rtaCommandDestroyProtocolStack_Acquire(destroyStack);
    return command;
}

const RtaCommandDestroyProtocolStack *
rtaCommand_GetDestroyProtocolStack(const RtaCommand *command)
{
    assertTrue(rtaCommand_IsDestroyProtocolStack(command), "Command is not DestroyProtocolStack");
    return command->value.destroyStack;
}

bool
rtaCommand_IsShutdownFramework(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_ShutdownFramework);
}

RtaCommand *
rtaCommand_CreateShutdownFramework(void)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_ShutdownFramework);
    command->value.noValue = NULL;
    return command;
}

// no getter

bool
rtaCommand_IsTransmitStatistics(const RtaCommand *command)
{
    _rtaCommand_OptionalAssertValid(command);
    return (command->type == RtaCommandType_TransmitStatistics);
}

RtaCommand *
rtaCommand_CreateTransmitStatistics(const RtaCommandTransmitStatistics *transmitStats)
{
    RtaCommand *command = _rtaCommand_Allocate(RtaCommandType_TransmitStatistics);
    command->value.transmitStats = rtaCommandTransmitStatistics_Acquire(transmitStats);
    return command;
}

const RtaCommandTransmitStatistics *
rtaCommand_GetTransmitStatistics(const RtaCommand *command)
{
    assertTrue(rtaCommand_IsTransmitStatistics(command), "Command is not TransmitStatistics");
    return command->value.transmitStats;
}