Debian Codegraph @

c/src/messenger/subscription.c

raw
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/messenger.h>
23 #include <proton/object.h>
24 #include <assert.h>
25 #include <string.h>
26
27 #include "messenger.h"
28
29 struct pn_subscription_t {
30 pn_messenger_t *messenger;
31 pn_string_t *scheme;
32 pn_string_t *host;
33 pn_string_t *port;
34 pn_string_t *address;
35 void *context;
36 };
37
38 void pn_subscription_initialize(void *obj)
39 {
40 pn_subscription_t *sub = (pn_subscription_t *) obj;
41 sub->messenger = NULL;
42 sub->scheme = pn_string(NULL);
43 sub->host = pn_string(NULL);
44 sub->port = pn_string(NULL);
45 sub->address = pn_string(NULL);
46 sub->context = NULL;
47 }
48
49 void pn_subscription_finalize(void *obj)
50 {
51 pn_subscription_t *sub = (pn_subscription_t *) obj;
52 pn_free(sub->scheme);
53 pn_free(sub->host);
54 pn_free(sub->port);
55 pn_free(sub->address);
56 }
57
58 #define CID_pn_subscription CID_pn_object
59 #define pn_subscription_hashcode NULL
60 #define pn_subscription_compare NULL
61 #define pn_subscription_inspect NULL
62
63 pn_subscription_t *pn_subscription(pn_messenger_t *messenger,
64 const char *scheme,
65 const char *host,
66 const char *port)
67 {
68 static const pn_class_t clazz = PN_CLASS(pn_subscription);
69 pn_subscription_t *sub = (pn_subscription_t *) pn_class_new(&clazz, sizeof(pn_subscription_t));
70 sub->messenger = messenger;
71 pn_string_set(sub->scheme, scheme);
72 pn_string_set(sub->host, host);
73 pn_string_set(sub->port, port);
74 pni_messenger_add_subscription(messenger, sub);
75 pn_class_decref(PN_OBJECT, sub);
76 return sub;
77 }
78
79 const char *pn_subscription_scheme(pn_subscription_t *sub)
80 {
81 assert(sub);
82 return pn_string_get(sub->scheme);
83 }
84
85 void *pn_subscription_get_context(pn_subscription_t *sub)
86 {
87 assert(sub);
88 return sub->context;
89 }
90
91 void pn_subscription_set_context(pn_subscription_t *sub, void *context)
92 {
93 assert(sub);
94 sub->context = context;
95 }
96
97 int pni_subscription_set_address(pn_subscription_t *sub, const char *address)
98 {
99 assert(sub);
100
101 if (!address) return 0;
102
103 bool absolute = strncmp(address, "amqp:", 5) == 0;
104
105 if (absolute) {
106 return pn_string_set(sub->address, address);
107 } else {
108 pn_string_set(sub->address, "");
109 bool scheme = pn_string_get(sub->scheme);
110 if (scheme) {
111 int e = pn_string_addf(sub->address, "%s:", pn_string_get(sub->scheme));
112 if (e) return e;
113 }
114 if (pn_string_get(sub->host)) {
115 int e = pn_string_addf(sub->address, scheme ? "//%s" : "%s", pn_string_get(sub->host));
116 if (e) return e;
117 }
118 if (pn_string_get(sub->port)) {
119 int e = pn_string_addf(sub->address, ":%s", pn_string_get(sub->port));
120 if (e) return e;
121 }
122 return pn_string_addf(sub->address, "/%s", address);
123 }
124 }
125
126 const char *pn_subscription_address(pn_subscription_t *sub)
127 {
128 assert(sub);
129 while (!pn_string_get(sub->address)) {
130 int err = pni_messenger_work(sub->messenger);
131 if (err < 0) {
132 return NULL;
133 }
134 }
135 return pn_string_get(sub->address);
136 }