| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/connection.h> |
| 23 |
#include <proton/session.h> |
| 24 |
#include <proton/link.h> |
| 25 |
#include <proton/handlers.h> |
| 26 |
#include <assert.h> |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
pn_map_t *handlers; |
| 30 |
} pni_handshaker_t; |
| 31 |
|
| 32 |
pni_handshaker_t *pni_handshaker(pn_handler_t *handler) { |
| 33 |
return (pni_handshaker_t *) pn_handler_mem(handler); |
| 34 |
} |
| 35 |
|
| 36 |
static void pn_handshaker_finalize(pn_handler_t *handler) { |
| 37 |
pni_handshaker_t *handshaker = pni_handshaker(handler); |
| 38 |
pn_free(handshaker->handlers); |
| 39 |
} |
| 40 |
|
| 41 |
static void pn_handshaker_dispatch(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) { |
| 42 |
switch (type) { |
| 43 |
case PN_CONNECTION_REMOTE_OPEN: |
| 44 |
{ |
| 45 |
pn_connection_t *conn = pn_event_connection(event); |
| 46 |
if (pn_connection_state(conn) & PN_LOCAL_UNINIT) { |
| 47 |
pn_connection_open(conn); |
| 48 |
} |
| 49 |
} |
| 50 |
break; |
| 51 |
case PN_SESSION_REMOTE_OPEN: |
| 52 |
{ |
| 53 |
pn_session_t *ssn = pn_event_session(event); |
| 54 |
if (pn_session_state(ssn) & PN_LOCAL_UNINIT) { |
| 55 |
pn_session_open(ssn); |
| 56 |
} |
| 57 |
} |
| 58 |
break; |
| 59 |
case PN_LINK_REMOTE_OPEN: |
| 60 |
{ |
| 61 |
pn_link_t *link = pn_event_link(event); |
| 62 |
if (pn_link_state(link) & PN_LOCAL_UNINIT) { |
| 63 |
pn_terminus_copy(pn_link_source(link), pn_link_remote_source(link)); |
| 64 |
pn_terminus_copy(pn_link_target(link), pn_link_remote_target(link)); |
| 65 |
pn_link_open(link); |
| 66 |
} |
| 67 |
} |
| 68 |
break; |
| 69 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 70 |
{ |
| 71 |
pn_connection_t *conn = pn_event_connection(event); |
| 72 |
if (!(pn_connection_state(conn) & PN_LOCAL_CLOSED)) { |
| 73 |
pn_connection_close(conn); |
| 74 |
} |
| 75 |
} |
| 76 |
break; |
| 77 |
case PN_SESSION_REMOTE_CLOSE: |
| 78 |
{ |
| 79 |
pn_session_t *ssn = pn_event_session(event); |
| 80 |
if (!(pn_session_state(ssn) & PN_LOCAL_CLOSED)) { |
| 81 |
pn_session_close(ssn); |
| 82 |
} |
| 83 |
} |
| 84 |
break; |
| 85 |
case PN_LINK_REMOTE_CLOSE: |
| 86 |
{ |
| 87 |
pn_link_t *link = pn_event_link(event); |
| 88 |
if (!(pn_link_state(link) & PN_LOCAL_CLOSED)) { |
| 89 |
pn_link_close(link); |
| 90 |
} |
| 91 |
} |
| 92 |
break; |
| 93 |
default: |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
pn_handshaker_t *pn_handshaker(void) { |
| 99 |
pn_handler_t *handler = pn_handler_new(pn_handshaker_dispatch, sizeof(pni_handshaker_t), pn_handshaker_finalize); |
| 100 |
pni_handshaker_t *handshaker = pni_handshaker(handler); |
| 101 |
handshaker->handlers = NULL; |
| 102 |
return handler; |
| 103 |
} |