aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/transport_rta/core/rta_Connection.c
blob: 455fed8d1987ee4d5c7bfd0ef03f64bc92353e71 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * 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.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_Memory.h>
#include <ccnx/transport/common/transport_Message.h>

#include <ccnx/transport/transport_rta/core/rta_Framework_Commands.h>
#include <ccnx/transport/transport_rta/core/rta_ProtocolStack.h>
#include <ccnx/transport/transport_rta/core/rta_Connection.h>
#include <ccnx/transport/transport_rta/core/rta_Component.h>

#include <ccnx/api/notify/notify_Status.h>
#include <ccnx/api/control/cpi_ControlFacade.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#ifdef DEBUG_OUTPUT
#undef DEBUG_OUTPUT
#endif

#define DEBUG_OUTPUT 0

// SPEW will dump stack traces on reference count events
#define SPEW 0

struct rta_connection {
    RtaProtocolStack        *stack;
    RtaFramework            *framework;

    // unique id for this connection
    unsigned connid;

    // opaque component-specific data and their closers
    void                   *component_data[LAST_COMPONENT];
    RtaComponentStats         *component_stats[LAST_COMPONENT];

    RtaConnectionStateType connState;

    unsigned messages_in_queue;
    unsigned refcount;

    PARCJSON                *params;

    // api_fd is used in status messages up to the user
    // transport_fd is used by the API connector to talk w/ API.
    int api_fd;
    int transport_fd;

    // is the connection blocked in the given direction?
    bool blocked_down;
    bool blocked_up;
};

RtaComponentStats *
rtaConnection_GetStats(RtaConnection *conn, RtaComponents component)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->component_stats[component];
}

RtaConnection *
rtaConnection_Create(RtaProtocolStack *stack, const RtaCommandOpenConnection *cmdOpen)
{
    int i;
    RtaConnection *conn = parcMemory_AllocateAndClear(sizeof(RtaConnection));
    assertNotNull(conn, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(RtaConnection));

    conn->stack = stack;
    conn->framework = rtaProtocolStack_GetFramework(stack);
    conn->connid = rtaProtocolStack_GetNextConnectionId(stack);
    conn->connState = CONN_OPEN;
    conn->api_fd = rtaCommandOpenConnection_GetApiNotifierFd(cmdOpen);
    conn->transport_fd = rtaCommandOpenConnection_GetTransportNotifierFd(cmdOpen);

    conn->params = parcJSON_Copy(rtaCommandOpenConnection_GetConfig(cmdOpen));
    conn->refcount = 1;

    conn->blocked_down = false;
    conn->blocked_up = false;

    for (i = 0; i < LAST_COMPONENT; i++) {
        conn->component_stats[i] = rtaComponentStats_Create(stack, i);
    }

    if (DEBUG_OUTPUT) {
        fprintf(stderr, "%9" PRIu64 " %s connection %p refcount %d\n",
                rtaFramework_GetTicks(conn->framework), __func__, (void *) conn, conn->refcount);
        if (SPEW) {
            longBowRuntime_StackTrace(STDERR_FILENO);
        }

        char *p = parcJSON_ToString(conn->params);
        printf("Connection configuration: %s\n", p);
        parcMemory_Deallocate((void **) &p);
    }

    return conn;
}

RtaConnection *
rtaConnection_Copy(RtaConnection *original)
{
    assertNotNull(original, "Called with null parameter");
    original->refcount++;
    if (DEBUG_OUTPUT) {
        fprintf(stderr, "%9" PRIu64 " %s connection %p refcount %d\n",
                rtaFramework_GetTicks(original->framework), __func__, (void *) original, original->refcount);
        if (SPEW) {
            longBowRuntime_StackTrace(STDERR_FILENO);
        }
    }

    return original;
}

void
rtaConnection_FreeFunc(void **voidPtr)
{
    rtaConnection_Destroy((RtaConnection **) voidPtr);
}

void
rtaConnection_Destroy(RtaConnection **connPtr)
{
    int i;
    RtaConnection *conn;
    assertNotNull(connPtr, "called with null connection pointer\n");
    conn = *connPtr;
    assertNotNull(conn, "called with null connection\n");
    assertTrue(conn->refcount > 0, "Called with 0 refcount, invalid state");

    conn->refcount--;
    if (conn->refcount > 0) {
        if (DEBUG_OUTPUT) {
            fprintf(stderr, "%9" PRIu64 "  %s connection %p skipped, refcount %u\n",
                    rtaFramework_GetTicks(conn->framework), __func__, (void *) conn, conn->refcount);
            if (SPEW) {
                longBowRuntime_StackTrace(STDERR_FILENO);
            }
        }
        return;
    }

    assertTrue(conn->messages_in_queue == 0, "called when messages are still queued\n");

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s connection %p\n", rtaFramework_GetTicks(conn->framework), __func__, (void *) conn);
        if (SPEW) {
            longBowRuntime_StackTrace(STDERR_FILENO);
        }
    }

    // Ok, at this point there's nothing left in queue, so we can
    // get rid of the container now

    for (i = 0; i < LAST_COMPONENT; i++) {
        rtaComponentStats_Destroy(&conn->component_stats[i]);
    }

    rtaFramework_RemoveConnection(conn->framework, conn);
    parcJSON_Release(&conn->params);
    parcMemory_Deallocate((void **) &conn);
    *connPtr = NULL;
}

RtaProtocolStack *
rtaConnection_GetStack(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->stack;
}

/*
 * Used to store per-connection state from Open.
 * Should be freed in Close, but you don't need to set it NULL.
 */
void
rtaConnection_SetPrivateData(RtaConnection *conn,
                             RtaComponents component,
                             void *private)
{
    assertNotNull(conn, "called with null connection\n");
    conn->component_data[component] = private;
}

/*
 * Used to store per-connection state from Open
 */
void *
rtaConnection_GetPrivateData(RtaConnection *conn,
                             RtaComponents component)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->component_data[component];
}

RtaConnectionStateType
rtaConnection_GetState(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->connState;
}

void
rtaConnection_SetState(RtaConnection *conn, RtaConnectionStateType connState)
{
    assertNotNull(conn, "called with null connection\n");
    conn->connState = connState;
    rtaProtocolStack_ConnectionStateChange(conn->stack, conn);
}

/*
 * returns number in queue, including this one
 */
unsigned
rtaConnection_IncrementMessagesInQueue(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    assertTrue(conn->connState != CONN_CLOSED, "%s called when connection closed\n", __func__);
    conn->messages_in_queue++;
    return conn->messages_in_queue;
}

unsigned
rtaConnection_DecrementMessagesInQueue(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    assertTrue(conn->messages_in_queue > 0, "Trying to decrement a queue with 0 messages already");

    conn->messages_in_queue--;
    return conn->messages_in_queue;
}

int
rtaConnection_GetApiFd(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->api_fd;
}


int
rtaConnection_GetTransportFd(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->transport_fd;
}

int
rtaConnection_GetStackId(RtaConnection *conn)
{
    return rtaProtocolStack_GetStackId(conn->stack);
}

unsigned
rtaConnection_MessagesInQueue(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->messages_in_queue;
}

unsigned
rtaConnection_GetConnectionId(const RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection\n");
    return conn->connid;
}

void
rtaConnection_SendNotifyStatus(RtaConnection *conn, RtaComponents component, RtaDirection direction, const NotifyStatus *status)
{
    PARCJSON *json = notifyStatus_ToJSON(status);

    CCNxTlvDictionary *notification = ccnxControlFacade_CreateNotification(json);
    parcJSON_Release(&json);

    TransportMessage *tm = transportMessage_CreateFromDictionary(notification);
    ccnxTlvDictionary_Release(&notification);

    PARCEventQueue *out = rtaComponent_GetOutputQueue(conn, component, direction);

    transportMessage_SetInfo(tm, rtaConnection_Copy(conn), rtaConnection_FreeFunc);
    rtaComponent_PutMessage(out, tm);
}

void
rtaConnection_SendStatus(RtaConnection *conn,
                         RtaComponents component,
                         RtaDirection direction,
                         NotifyStatusCode code,
                         CCNxName *optionalName,
                         const char *optionalMessage)
{
    NotifyStatus *status = notifyStatus_Create(conn->api_fd, code, optionalName, optionalMessage);
    rtaConnection_SendNotifyStatus(conn, component, direction, status);
    notifyStatus_Release(&status);
}

RtaConnection *
rtaConnection_GetFromTransport(TransportMessage *tm)
{
    return (RtaConnection *) transportMessage_GetInfo(tm);
}

RtaFramework *
rtaConnection_GetFramework(const RtaConnection *connection)
{
    assertNotNull(connection, "called with null connection");
    return connection->framework;
}

PARCJSON *
rtaConnection_GetParameters(RtaConnection *conn)
{
    assertNotNull(conn, "called with null connection");
    return conn->params;
}

bool
rtaConnection_BlockedDown(const RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    return (connection->connState != CONN_OPEN) || connection->blocked_down;
}

bool
rtaConnection_BlockedUp(const RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    return (connection->connState != CONN_OPEN) || connection->blocked_up;
}

void
rtaConnection_SetBlockedDown(RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    connection->blocked_down = true;
    rtaProtocolStack_ConnectionStateChange(connection->stack, connection);
}

void
rtaConnection_ClearBlockedDown(RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    connection->blocked_down = false;
    rtaProtocolStack_ConnectionStateChange(connection->stack, connection);
}

void
rtaConnection_SetBlockedUp(RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    connection->blocked_up = true;
    rtaProtocolStack_ConnectionStateChange(connection->stack, connection);
}

void
rtaConnection_ClearBlockedUp(RtaConnection *connection)
{
    assertNotNull(connection, "Parameter connection must be non-null");
    connection->blocked_up = false;
    rtaProtocolStack_ConnectionStateChange(connection->stack, connection);
}