include/pmcp/pmcp_bus.h

filter: Statementstmt 96.7% 270 / 279 Branchbran 67.8% 152 / 224 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.9% 422 / 503 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_otel.h ↑ Summary include/pmcp/pmcp_prompt.h »
1 /* pmcp_bus.h - the GET stream, resource subscriptions, and the worker bus.
2 *
3 * Needs: pmcp_sess.h, pmcp_res.h.
4 *
5 * With sessions on, GET on the MCP path opens a session's server stream: an
6 * `sse` route whose guard does the checks POST does and the session's. A
7 * session has one stream: a second GET closes the first, wherever it is.
8 *
9 * Each worker keeps its streams in PMCP_STREAMS, session id -> [stream,
10 * context, server]. What happens on one worker reaches the stream on another
11 * over Punk's bus, on the topic pmcp:<path>, as a payload of
12 *
13 * [u8 kind][u32 origin pid][u8 sid length][sid][uri]
14 *
15 * kind 1, updated: a resource changed, tell every stream whose session
16 * subscribed to it. kind 2, close: end this session's stream.
17 *
18 * The bus does deliver to the worker that published, but late: a publisher
19 * does not wake itself, so its own subscriber runs on its next dispatch (t/70
20 * sees it within a timer's wait), and without Hyperman there is no bus at all.
21 * So the publisher delivers to its own streams first, then publishes, and a
22 * subscriber drops what carries its own pid, or a worker's own streams would
23 * hear everything twice (t/70 counts).
24 */
25  
26 #ifndef PMCP_BUS_H
27 #define PMCP_BUS_H
28  
29 #define PMCP_BUS_UPDATED 1
30 #define PMCP_BUS_CLOSE 2
31 #define PMCP_BUS_SLOT 2048 /* a bus slot, and the most a payload may be */
32  
33 static HV *PMCP_STREAMS = NULL;
34  
35 /* Defined in pmcp_stdio.h: a stdio process is a subscriber too (13). */
36 static void pmcp_stdio_updated(pTHX_ SV *uri);
37 static void pmcp_stdio_subscribe(pTHX_ SV *uri, int on);
38 static int pmcp_stdio_active(void);
39  
40 /* ---- the payload -------------------------------------------------------------- */
41  
42 /* +1, or NULL when it would not fit a bus slot. */
43 33 static SV *pmcp_bus_encode(pTHX_ int kind, U32 pid, SV *sid, SV *uri)
44 {
45 33 STRLEN sl = 0, ul = 0;
46 54 ▶ const char *sp = sid ? SvPV_const(sid, sl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *sp = sid ? SvPV_const(sid, sl) : ""; 21 12 33
47 49 ▶ const char *up = uri ? SvPV_const(uri, ul) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *up = uri ? SvPV_const(uri, ul) : ""; 16 17 33
48 unsigned char head[6];
49 SV *out;
50 33 ▶ if (sl > 255 || 6 + sl + ul > PMCP_BUS_SLOT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (sl > 255 || 6 + sl + ul > PMCP_BUS_SLOT) return NULL; 33 0 33
if (sl > 255 || 6 + sl + ul > PMCP_BUS_SLOT) return NULL; 2 31 33
51 31 head[0] = (unsigned char)kind;
52 31 head[1] = (unsigned char)(pid >> 24);
53 31 head[2] = (unsigned char)(pid >> 16);
54 31 head[3] = (unsigned char)(pid >> 8);
55 31 head[4] = (unsigned char)pid;
56 31 head[5] = (unsigned char)sl;
57 31 out = newSVpvn((const char *)head, 6);
58 31 sv_catpvn(out, sp, sl);
59 31 sv_catpvn(out, up, ul);
60 31 return out;
61 }
62  
63 /* 1 with the parts set (sid and uri mortal, uri UTF-8 if it decodes), or 0
64 * for a payload that is not one of ours. */
65 6 static int pmcp_bus_decode(pTHX_ SV *payload, int *kind, U32 *pid, SV **sid, SV **uri)
66 {
67 STRLEN l;
68 const unsigned char *p;
69 STRLEN sl;
70 6 ▶ if (!payload || !SvOK(payload)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!payload || !SvOK(payload)) return 0; 6 0 6
if (!payload || !SvOK(payload)) return 0; 0 6 6
71 6 p = (const unsigned char *)SvPV_const(payload, l);
72 6 ▶ if (l < 6) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (l < 6) return 0; 0 6 6
73 6 sl = p[5];
74 6 ▶ if (6 + sl > l || (p[0] != PMCP_BUS_UPDATED && p[0] != PMCP_BUS_CLOSE)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (6 + sl > l || (p[0] != PMCP_BUS_UPDATED && p[0] != PMCP_BUS_CLOSE)) return 0; 5 1 6
if (6 + sl > l || (p[0] != PMCP_BUS_UPDATED && p[0] != PMCP_BUS_CLOSE)) return 0; 2 3 5
if (6 + sl > l || (p[0] != PMCP_BUS_UPDATED && p[0] != PMCP_BUS_CLOSE)) return 0; 1 1 2
75 4 *kind = p[0];
76 4 *pid = ((U32)p[1] << 24) | ((U32)p[2] << 16) | ((U32)p[3] << 8) | p[4];
77 4 *sid = sv_2mortal(newSVpvn((const char *)p + 6, sl));
78 4 *uri = sv_2mortal(newSVpvn((const char *)p + 6 + sl, l - 6 - sl));
79 4 ▶ if (is_utf8_string((U8 *)SvPVX(*uri), SvCUR(*uri))) SvUTF8_on(*uri);
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8_string((U8 *)SvPVX(*uri), SvCUR(*uri))) SvUTF8_on(*uri); 3 1 4
80 4 return 1;
81 }
82  
83 41 static SV *pmcp_bus_topic(pTHX_ pmcp_server *ps)
84 {
85 41 return sv_2mortal(newSVpvf("pmcp:%" SVf, SVfARG(pmcp_hget(aTHX_ ps->opts, "path"))));
86 }
87  
88 /* ---- the streams on this worker ------------------------------------------------ */
89  
90 29 static AV *pmcp_stream_entry(pTHX_ SV *sid)
91 {
92 29 ▶ HE *he = PMCP_STREAMS ? hv_fetch_ent(PMCP_STREAMS, sid, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = PMCP_STREAMS ? hv_fetch_ent(PMCP_STREAMS, sid, 0, 0) : NULL; 29 0 29
93 29 ▶ return (he && pmcp_is_array(HeVAL(he))) ? (AV *)SvRV(HeVAL(he)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (he && pmcp_is_array(HeVAL(he))) ? (AV *)SvRV(HeVAL(he)) : NULL; 21 8 29
return (he && pmcp_is_array(HeVAL(he))) ? (AV *)SvRV(HeVAL(he)) : NULL; 21 0 21
94 }
95  
96 13 static int pmcp_stream_open(pTHX_ SV *stream)
97 {
98 13 SV *r = pmcp_call_method(aTHX_ stream, "is_open", NULL, 0, G_SCALAR, NULL);
99 13 ▶ int open = r && SvTRUE(r);
Branch: 2 branches
BranchTrueFalseTotal
int open = r && SvTRUE(r); 13 0 13
int open = r && SvTRUE(r); 12 1 13
100 13 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 13 0 13
101 13 return open;
102 }
103  
104 9 static void pmcp_stream_close(pTHX_ SV *sid)
105 {
106 9 AV *e = pmcp_stream_entry(aTHX_ sid);
107 9 ▶ if (!e) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return; 5 4 9
108 {
109 4 SV *stream = sv_2mortal(newSVsv(*av_fetch(e, 0, 0)));
110 4 SV *err = NULL, *r;
111 4 (void)hv_delete_ent(PMCP_STREAMS, sid, G_DISCARD, 0);
112 4 r = pmcp_call_method(aTHX_ stream, "close", NULL, 0, G_SCALAR, &err);
113 4 ▶ if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err); 4 0 4
114 }
115 }
116  
117 /* One notification down one stream: an SSE `message` event. */
118 8 static void pmcp_stream_send(pTHX_ SV *stream, const char *method, SV *params)
119 {
120 8 HV *m = newHV();
121 8 SV *rv, *bytes, *err = NULL, *argv[2], *r;
122 8 (void)hv_stores(m, "jsonrpc", newSVpvs("2.0"));
123 8 (void)hv_stores(m, "method", newSVpv(method, 0));
124 8 (void)hv_stores(m, "params", params);
125 8 rv = sv_2mortal(newRV_noinc((SV *)m));
126 8 bytes = pmcp_json_trapped(aTHX_ rv, &err);
127 8 ▶ if (!bytes) { SvREFCNT_dec(err); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { SvREFCNT_dec(err); return; } 0 8 8
128 8 argv[0] = sv_2mortal(newSVpvs("message"));
129 8 argv[1] = sv_2mortal(bytes);
130 8 r = pmcp_call_method(aTHX_ stream, "event", argv, 2, G_SCALAR, &err);
131 8 ▶ if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err); 8 0 8
132 }
133  
134 /* Does this session subscribe to `uri`? Asked of its record, which is the
135 * truth wherever the subscribe arrived. */
136 12 static int pmcp_session_subscribes(pTHX_ SV *c, pmcp_server *ps, SV *sid, SV *uri)
137 {
138 12 SV *store = pmcp_sess_store(aTHX_ c, ps->opts);
139 12 ▶ HV *rec = store ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *rec = store ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL; 12 0 12
140 12 ▶ SV *subs = rec ? pmcp_hget(aTHX_ rec, "subs") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *subs = rec ? pmcp_hget(aTHX_ rec, "subs") : NULL; 12 0 12
141 SSize_t i, n;
142 12 ▶ if (!pmcp_is_array(subs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(subs)) return 0; 0 12 12
143 12 n = av_len((AV *)SvRV(subs)) + 1;
144 13 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 4 13
145 9 SV **e = av_fetch((AV *)SvRV(subs), i, 0);
146 9 ▶ if (e && sv_eq(*e, uri)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (e && sv_eq(*e, uri)) return 1; 9 0 9
if (e && sv_eq(*e, uri)) return 1; 8 1 9
147 }
148 4 return 0;
149 }
150  
151 /* What a payload asks of this worker's streams. Prunes the closed ones as it
152 * goes: nothing else would, and a table that only grows is a leak. */
153 29 static void pmcp_bus_deliver(pTHX_ pmcp_server *ps, int kind, SV *sid, SV *uri)
154 {
155 29 ▶ if (kind == PMCP_BUS_UPDATED) pmcp_stdio_updated(aTHX_ uri);
Branch: 1 branch
BranchTrueFalseTotal
if (kind == PMCP_BUS_UPDATED) pmcp_stdio_updated(aTHX_ uri); 13 16 29
156 29 ▶ if (!PMCP_STREAMS) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_STREAMS) return; 10 19 29
157 19 ▶ if (kind == PMCP_BUS_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind == PMCP_BUS_CLOSE) { 9 10 19
158 9 pmcp_stream_close(aTHX_ sid);
159 9 return;
160 }
161 {
162 10 AV *sids = (AV *)sv_2mortal((SV *)newAV());
163 HE *he;
164 SSize_t i, n;
165 10 hv_iterinit(PMCP_STREAMS);
166 23 ▶ while ((he = hv_iternext(PMCP_STREAMS)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(PMCP_STREAMS))) 13 10 23
167 13 av_push(sids, newSVsv(hv_iterkeysv(he)));
168 10 n = av_len(sids) + 1;
169 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 10 23
170 13 SV *s = *av_fetch(sids, i, 0);
171 13 AV *e = pmcp_stream_entry(aTHX_ s);
172 SV *stream, *c;
173 13 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 13 13
174 13 ▶ if (pmcp_server_of(aTHX_ *av_fetch(e, 2, 0)) != ps) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_server_of(aTHX_ *av_fetch(e, 2, 0)) != ps) continue; 0 13 13
175 13 stream = *av_fetch(e, 0, 0);
176 13 c = *av_fetch(e, 1, 0);
177 13 ▶ if (!pmcp_stream_open(aTHX_ stream)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_stream_open(aTHX_ stream)) { 1 12 13
178 1 (void)hv_delete_ent(PMCP_STREAMS, s, G_DISCARD, 0);
179 1 continue;
180 }
181 12 ▶ if (pmcp_session_subscribes(aTHX_ c, ps, s, uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_session_subscribes(aTHX_ c, ps, s, uri)) { 8 4 12
182 8 HV *p = newHV();
183 8 (void)hv_stores(p, "uri", newSVsv(uri));
184 8 pmcp_stream_send(aTHX_ stream, "notifications/resources/updated",
185 newRV_noinc((SV *)p));
186 }
187 }
188 }
189 }
190  
191 /* Deliver here, then tell the pool. `via` is a context or the app, whichever
192 * can publish. Returns what publish said: 1 on the ring, 0 local only, -1 not
193 * sent (too big). */
194 27 static int pmcp_bus_send(pTHX_ pmcp_server *ps, SV *via, int kind, SV *sid, SV *uri)
195 {
196 SV *payload, *argv[2], *r;
197 27 int said = 0;
198 27 pmcp_bus_deliver(aTHX_ ps, kind, sid, uri);
199 27 payload = pmcp_bus_encode(aTHX_ kind, (U32)getpid(), sid, uri);
200 27 ▶ if (!payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (!payload) { 1 26 27
201 1 ▶ warn("%s: a %s for %" SVf " does not fit a bus slot, so only this worker "
Branch: 3 branches
BranchTrueFalseTotal
warn("%s: a %s for %" SVf " does not fit a bus slot, so only this worker " 1 0 1
warn("%s: a %s for %" SVf " does not fit a bus slot, so only this worker " 1 0 1
warn("%s: a %s for %" SVf " does not fit a bus slot, so only this worker " 0 1 1
202 "saw it\n", PMCP_WHO, kind == PMCP_BUS_CLOSE ? "close" : "resource update",
203 SVfARG(uri && SvCUR(uri) ? uri : sid));
204 1 return -1;
205 }
206 26 argv[0] = pmcp_bus_topic(aTHX_ ps);
207 26 argv[1] = sv_2mortal(payload);
208 26 r = pmcp_call_method(aTHX_ via, "publish", argv, 2, G_SCALAR, NULL);
209 26 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 26 0 26
210 26 ▶ said = SvOK(r) ? (int)SvIV(r) : 0;
Branch: 1 branch
BranchTrueFalseTotal
said = SvOK(r) ? (int)SvIV(r) : 0; 26 0 26
211 26 SvREFCNT_dec(r);
212 }
213 26 return said;
214 }
215  
216 /* The subscriber, registered at boot: ($topic, $payload). cap = [server] */
217 3 XS_INTERNAL(pmcp_bus_cb)
218 {
219 3 dXSARGS;
220 3 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 0));
221 int kind;
222 U32 pid;
223 SV *sid, *uri;
224 3 ▶ if (ps && items > 1 && pmcp_bus_decode(aTHX_ ST(1), &kind, &pid, &sid, &uri)
Branch: 3 branches
BranchTrueFalseTotal
if (ps && items > 1 && pmcp_bus_decode(aTHX_ ST(1), &kind, &pid, &sid, &uri) 3 0 3
if (ps && items > 1 && pmcp_bus_decode(aTHX_ ST(1), &kind, &pid, &sid, &uri) 3 0 3
if (ps && items > 1 && pmcp_bus_decode(aTHX_ ST(1), &kind, &pid, &sid, &uri) 3 0 3
225 3 ▶ && pid != (U32)getpid())
Branch: 1 branch
BranchTrueFalseTotal
&& pid != (U32)getpid()) 2 1 3
226 2 pmcp_bus_deliver(aTHX_ ps, kind, sid, uri);
227 3 XSRETURN_EMPTY;
228 }
229  
230 /* ---- the GET route ---------------------------------------------------------------- */
231  
232 /* The route's guard: POST's checks, then the session's. NULL to open the
233 * stream (the session id stashed), or the refusal. cap = [server, guards] */
234 9 XS_INTERNAL(pmcp_get_guard)
235 {
236 9 dXSARGS;
237 9 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 9 0 9
238 9 pmcp_server *ps = pmcp_server_of(aTHX_ pmcp_cap(aTHX_ cv, 0));
239 9 SV *gslot = pmcp_cap(aTHX_ cv, 1);
240 9 ▶ AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL; 9 0 9
AV *guards = (gslot && pmcp_is_array(gslot)) ? (AV *)SvRV(gslot) : NULL; 9 0 9
241 9 SV *envsv = sv_2mortal(pmcp_call_method(aTHX_ c, "env", NULL, 0, G_SCALAR, NULL));
242 9 HV *env = (HV *)SvRV(envsv);
243 9 int cors = pmcp_env_get(aTHX_ env, "HTTP_ORIGIN") != NULL;
244 9 SV *out = NULL, *sid, *store, *pv, *stash;
245 HV *rec;
246  
247 9 ▶ if (!pmcp_origin_ok(aTHX_ env, ps->opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_origin_ok(aTHX_ env, ps->opts)) { 1 8 9
248 1 out = pmcp_http_refuse(aTHX_ 403, "origin not allowed", cors);
249 1 goto answer;
250 }
251 8 ▶ if (!pmcp_wants_sse(aTHX_ env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_wants_sse(aTHX_ env)) { 1 7 8
252 1 out = pmcp_http_refuse(aTHX_ 406, "Accept must allow text/event-stream", cors);
253 1 goto answer;
254 }
255 7 ▶ if (guards) {
Branch: 1 branch
BranchTrueFalseTotal
if (guards) { 7 0 7
256 7 SSize_t i, n = av_len(guards) + 1;
257 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 6 7
258 1 SV *err = NULL;
259 1 SV *r = pmcp_call_sv(aTHX_ *av_fetch(guards, i, 0), &c, 1, G_SCALAR, &err);
260 1 ▶ if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 1 1
261 1 ▶ if (SvROK(r)) { out = r; pmcp_expose_refusal(aTHX_ out, cors); goto answer; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(r)) { out = r; pmcp_expose_refusal(aTHX_ out, cors); goto answer; } 1 0 1
262 0 SvREFCNT_dec(r);
263 }
264 }
265 6 sid = pmcp_env_get(aTHX_ env, "HTTP_MCP_SESSION_ID");
266 6 ▶ if (!sid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sid) { 2 4 6
267 2 out = pmcp_http_refuse(aTHX_ 400, "a session id is required", cors);
268 2 goto answer;
269 }
270 4 store = pmcp_sess_store(aTHX_ c, ps->opts);
271 4 ▶ rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL; 4 0 4
rec = (store && pmcp_sid_ok(aTHX_ sid)) ? pmcp_sess_load(aTHX_ store, sid, pmcp_now(aTHX)) : NULL; 4 0 4
272 4 ▶ if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) { 3 1 4
if (!rec || !sv_eq(pmcp_hget(aTHX_ rec, "principal"), pmcp_principal(aTHX_ env))) { 1 2 3
273 2 out = pmcp_http_refuse(aTHX_ 404, "unknown session", cors);
274 2 goto answer;
275 }
276 2 pv = pmcp_env_get(aTHX_ env, "HTTP_MCP_PROTOCOL_VERSION");
277 2 ▶ if (pv && !sv_eq(pv, pmcp_hget(aTHX_ rec, "v"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (pv && !sv_eq(pv, pmcp_hget(aTHX_ rec, "v"))) { 0 2 2
if (pv && !sv_eq(pv, pmcp_hget(aTHX_ rec, "v"))) { 0 0 0
278 0 out = pmcp_http_refuse(aTHX_ 400, "MCP-Protocol-Version does not match the session's", cors);
279 0 goto answer;
280 }
281 2 stash = sv_2mortal(pmcp_call_method(aTHX_ c, "stash", NULL, 0, G_SCALAR, NULL));
282 2 ▶ if (pmcp_is_hash(stash)) (void)hv_stores((HV *)SvRV(stash), "mcp.sid", newSVsv(sid));
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(stash)) (void)hv_stores((HV *)SvRV(stash), "mcp.sid", newSVsv(sid)); 0 2 2
283  
284 0 answer:
285 9 SP = PL_stack_base + ax - 1;
286 9 ▶ ST(0) = out ? sv_2mortal(out) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = out ? sv_2mortal(out) : &PL_sv_undef; 7 2 9
287 9 XSRETURN(1);
288 }
289  
290 /* Drops the table entry when its stream ends, unless a newer stream has
291 * taken the session's place. cap = [sid, stream] */
292 7 XS_INTERNAL(pmcp_stream_gone)
293 {
294 7 dXSARGS;
295 7 SV *sid = pmcp_cap(aTHX_ cv, 0), *stream = pmcp_cap(aTHX_ cv, 1);
296 7 ▶ AV *e = sid ? pmcp_stream_entry(aTHX_ sid) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *e = sid ? pmcp_stream_entry(aTHX_ sid) : NULL; 7 0 7
297 PERL_UNUSED_VAR(items);
298 7 ▶ if (e && SvROK(*av_fetch(e, 0, 0)) && stream && SvROK(stream)
Branch: 4 branches
BranchTrueFalseTotal
if (e && SvROK(*av_fetch(e, 0, 0)) && stream && SvROK(stream) 4 3 7
if (e && SvROK(*av_fetch(e, 0, 0)) && stream && SvROK(stream) 4 0 4
if (e && SvROK(*av_fetch(e, 0, 0)) && stream && SvROK(stream) 4 0 4
if (e && SvROK(*av_fetch(e, 0, 0)) && stream && SvROK(stream) 4 0 4
299 4 ▶ && SvRV(*av_fetch(e, 0, 0)) == SvRV(stream))
Branch: 1 branch
BranchTrueFalseTotal
&& SvRV(*av_fetch(e, 0, 0)) == SvRV(stream)) 3 1 4
300 3 (void)hv_delete_ent(PMCP_STREAMS, sid, G_DISCARD, 0);
301 7 XSRETURN_EMPTY;
302 }
303  
304 /* Put a stream in the table for a session, closing whatever held it before:
305 * here directly, elsewhere over the bus. `via` publishes. */
306 9 static void pmcp_stream_register(pTHX_ SV *server, SV *via, SV *sid, SV *stream, SV *c)
307 {
308 9 pmcp_server *ps = pmcp_server_of(aTHX_ server);
309 9 AV *e = newAV(), *cap;
310 9 SV *cb, *argv[2], *r, *err = NULL;
311 9 pmcp_bus_send(aTHX_ ps, via, PMCP_BUS_CLOSE, sid, NULL);
312 9 ▶ if (!PMCP_STREAMS) PMCP_STREAMS = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PMCP_STREAMS) PMCP_STREAMS = newHV(); 2 7 9
313 9 av_push(e, newSVsv(stream));
314 9 av_push(e, newSVsv(c));
315 9 av_push(e, newSVsv(server));
316 9 (void)hv_store_ent(PMCP_STREAMS, sid, newRV_noinc((SV *)e), 0);
317 9 cap = newAV();
318 9 av_push(cap, newSVsv(sid));
319 9 av_push(cap, newSVsv(stream));
320 9 cb = sv_2mortal(pmcp_closure(aTHX_ pmcp_stream_gone, cap));
321 9 argv[0] = sv_2mortal(newSVpvs("close"));
322 9 argv[1] = cb;
323 9 r = pmcp_call_method(aTHX_ stream, "on", argv, 2, G_SCALAR, &err);
324 9 ▶ if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); else SvREFCNT_dec(err); 9 0 9
325 9 }
326  
327 /* The sse handler: ($c, $stream). cap = [server] */
328 2 XS_INTERNAL(pmcp_get_cb)
329 {
330 2 dXSARGS;
331 2 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 2 0 2
332 2 ▶ SV *stream = items > 1 ? ST(1) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *stream = items > 1 ? ST(1) : &PL_sv_undef; 2 0 2
333 2 SV *server = pmcp_cap(aTHX_ cv, 0);
334 2 SV *stash = sv_2mortal(pmcp_call_method(aTHX_ c, "stash", NULL, 0, G_SCALAR, NULL));
335 2 ▶ SV *sid = pmcp_is_hash(stash) ? pmcp_hget(aTHX_ (HV *)SvRV(stash), "mcp.sid") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *sid = pmcp_is_hash(stash) ? pmcp_hget(aTHX_ (HV *)SvRV(stash), "mcp.sid") : NULL; 2 0 2
336 2 ▶ if (sid) {
Branch: 1 branch
BranchTrueFalseTotal
if (sid) { 2 0 2
337 2 sid = sv_2mortal(newSVsv(sid));
338 2 pmcp_stream_register(aTHX_ server, c, sid, stream, c);
339 {
340 /* t/35 drives the live route through this */
341 2 SV *hook = get_sv("Punk::Plugin::MCP::_TEST_GET_HOOK", 0);
342 2 ▶ if (hook && pmcp_is_code(hook)) {
Branch: 2 branches
BranchTrueFalseTotal
if (hook && pmcp_is_code(hook)) { 2 0 2
if (hook && pmcp_is_code(hook)) { 2 0 2
343 SV *argv[3], *r;
344 2 argv[0] = c; argv[1] = stream; argv[2] = sid;
345 2 r = pmcp_call_sv(aTHX_ hook, argv, 3, G_SCALAR, NULL);
346 2 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 2 0 2
347 }
348 }
349 }
350 2 XSRETURN_EMPTY;
351 }
352  
353 /* At on_compile, with sessions on: the GET route and the bus subscriber. */
354 15 static void pmcp_bus_install(pTHX_ SV *app, SV *server, SV *guards)
355 {
356 15 pmcp_server *ps = pmcp_server_of(aTHX_ server);
357 15 AV *gcap = newAV(), *hcap = newAV(), *bcap = newAV(), *glist = newAV();
358 15 HV *opts = newHV();
359 SV *argv[5], *r;
360  
361 15 av_push(gcap, newSVsv(server));
362 15 av_push(gcap, newSVsv(guards));
363 15 av_push(glist, pmcp_closure(aTHX_ pmcp_get_guard, gcap));
364 15 av_push(hcap, newSVsv(server));
365 15 (void)hv_stores(opts, "heartbeat", newSViv(PMCP_HEARTBEAT_SECS));
366 15 argv[0] = pmcp_hget(aTHX_ ps->opts, "path");
367 15 argv[1] = sv_2mortal(pmcp_closure(aTHX_ pmcp_get_cb, hcap));
368 15 argv[2] = sv_2mortal(newRV_noinc((SV *)opts));
369 15 argv[3] = sv_2mortal(newRV_noinc((SV *)glist));
370 15 r = pmcp_call_method(aTHX_ app, "sse", argv, 4, G_SCALAR, NULL);
371 15 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 15 0 15
372  
373 15 av_push(bcap, newSVsv(server));
374 15 argv[0] = pmcp_bus_topic(aTHX_ ps);
375 15 argv[1] = sv_2mortal(pmcp_closure(aTHX_ pmcp_bus_cb, bcap));
376 15 r = pmcp_call_method(aTHX_ app, "subscribe", argv, 2, G_SCALAR, NULL);
377 15 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 15 0 15
378 15 }
379  
380 /* ---- resources/subscribe and unsubscribe -------------------------------------------- */
381  
382 9 static SV *pmcp_subscription(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params, int on)
383 {
384 9 pmcp_server *ps = (pmcp_server *)s; /* pmcp_srv is its first member */
385 9 pmcp_resources *rs = (pmcp_resources *)s->resources;
386 9 ▶ SV **uv = params ? hv_fetchs(params, "uri", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **uv = params ? hv_fetchs(params, "uri", 0) : NULL; 9 0 9
387 SV *store, *subs;
388 9 HV *rec, *caps = NULL;
389 pmcp_resource *res;
390  
391 9 ▶ if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio))
Branch: 5 branches
BranchTrueFalseTotal
if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio)) 9 0 9
if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio)) 9 0 9
if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio)) 7 2 9
if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio)) 0 7 7
if (!rs || !r->c || !((s->can_notify && r->session) || r->stdio)) 1 1 2
392 1 return pmcp_fail(r, PMCP_METHOD, "method not found");
393 8 ▶ if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required");
Branch: 2 branches
BranchTrueFalseTotal
if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required"); 8 0 8
if (!(uv && pmcp_is_str(*uv))) return pmcp_fail(r, PMCP_PARAMS, "a uri is required"); 0 8 8
394 8 ▶ if (on) {
Branch: 1 branch
BranchTrueFalseTotal
if (on) { 7 1 8
395 SV *why;
396 7 res = pmcp_res_find(aTHX_ rs, *uv, &caps);
397 7 ▶ if (caps) SvREFCNT_dec((SV *)caps);
Branch: 1 branch
BranchTrueFalseTotal
if (caps) SvREFCNT_dec((SV *)caps); 5 2 7
398 7 ▶ if (!res) return pmcp_res_not_found(aTHX_ r, *uv);
Branch: 1 branch
BranchTrueFalseTotal
if (!res) return pmcp_res_not_found(aTHX_ r, *uv); 1 6 7
399 6 ▶ if ((why = pmcp_run_guards(aTHX_ res->guards, r->c))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((why = pmcp_run_guards(aTHX_ res->guards, r->c))) { 1 5 6
400 1 SvREFCNT_dec(why);
401 1 return pmcp_res_not_found(aTHX_ r, *uv);
402 }
403 }
404 /* over stdio the process is the session: its subscriptions are its own */
405 6 ▶ if (r->stdio) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->stdio) { 1 5 6
406 1 pmcp_stdio_subscribe(aTHX_ *uv, on);
407 1 return newRV_noinc((SV *)newHV());
408 }
409 5 store = pmcp_sess_store(aTHX_ r->c, ps->opts);
410 5 ▶ rec = store ? pmcp_sess_load(aTHX_ store, r->session, pmcp_now(aTHX)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
rec = store ? pmcp_sess_load(aTHX_ store, r->session, pmcp_now(aTHX)) : NULL; 5 0 5
411 5 ▶ if (!rec) return pmcp_fail(r, PMCP_INTERNAL, "the session is gone");
Branch: 1 branch
BranchTrueFalseTotal
if (!rec) return pmcp_fail(r, PMCP_INTERNAL, "the session is gone"); 0 5 5
412 5 subs = pmcp_hget(aTHX_ rec, "subs");
413 5 ▶ if (!pmcp_is_array(subs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(subs)) { 0 5 5
414 0 subs = sv_2mortal(newRV_noinc((SV *)newAV()));
415 0 (void)hv_stores(rec, "subs", newSVsv(subs));
416 0 subs = pmcp_hget(aTHX_ rec, "subs");
417 }
418 {
419 5 AV *av = (AV *)SvRV(subs), *keep = newAV();
420 5 SSize_t i, n = av_len(av) + 1;
421 5 int had = 0;
422 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 5 6
423 1 SV **e = av_fetch(av, i, 0);
424 1 ▶ if (e && sv_eq(*e, *uv)) { had = 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (e && sv_eq(*e, *uv)) { had = 1; continue; } 1 0 1
if (e && sv_eq(*e, *uv)) { had = 1; continue; } 1 0 1
425 0 ▶ if (e) av_push(keep, newSVsv(*e));
Branch: 1 branch
BranchTrueFalseTotal
if (e) av_push(keep, newSVsv(*e)); 0 0 0
426 }
427 5 ▶ if (on) av_push(keep, newSVsv(*uv));
Branch: 1 branch
BranchTrueFalseTotal
if (on) av_push(keep, newSVsv(*uv)); 4 1 5
428 5 (void)hv_stores(rec, "subs", newRV_noinc((SV *)keep));
429 5 ▶ if (on || had)
Branch: 2 branches
BranchTrueFalseTotal
if (on || had) 1 4 5
if (on || had) 1 0 1
430 5 pmcp_sess_save(aTHX_ store, r->session, rec,
431 SvIV(pmcp_hget(aTHX_ ps->opts, "session_ttl")));
432 }
433 5 return newRV_noinc((SV *)newHV());
434 }
435  
436 8 static SV *pmcp_resources_subscribe(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
437 {
438 8 return pmcp_subscription(aTHX_ s, r, params, 1);
439 }
440  
441 1 static SV *pmcp_resources_unsubscribe(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
442 {
443 1 return pmcp_subscription(aTHX_ s, r, params, 0);
444 }
445  
446 /* ---- telling subscribers ------------------------------------------------------------ */
447  
448 /* $c->mcp_resource_updated($uri): this worker's subscribed streams, then the
449 * pool. Returns what publish said, or 0 with sessions off. cap = [] */
450 6 XS_INTERNAL(pmcp_helper_updated)
451 {
452 6 dXSARGS;
453 6 int said = 0;
454 6 ▶ if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1)))
Branch: 3 branches
BranchTrueFalseTotal
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1))) 6 0 6
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1))) 6 0 6
if (items < 2 || !SvOK(ST(1)) || SvROK(ST(1))) 0 6 6
455 0 croak("%s: mcp_resource_updated takes the resource's URI", PMCP_WHO);
456 {
457 6 SV *app = sv_2mortal(pmcp_call_method(aTHX_ ST(0), "app", NULL, 0, G_SCALAR, NULL));
458 6 ▶ HV *st = pmcp_is_hash(app) ? pmcp_state(aTHX_ app) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *st = pmcp_is_hash(app) ? pmcp_state(aTHX_ app) : NULL; 6 0 6
459 6 ▶ pmcp_server *ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pmcp_server *ps = st ? pmcp_server_of(aTHX_ pmcp_hget(aTHX_ st, "server")) : NULL; 6 0 6
460 6 ▶ if (ps && (pmcp_sessions_on(aTHX_ ps->opts) || pmcp_stdio_active()))
Branch: 3 branches
BranchTrueFalseTotal
if (ps && (pmcp_sessions_on(aTHX_ ps->opts) || pmcp_stdio_active())) 6 0 6
if (ps && (pmcp_sessions_on(aTHX_ ps->opts) || pmcp_stdio_active())) 2 4 6
if (ps && (pmcp_sessions_on(aTHX_ ps->opts) || pmcp_stdio_active())) 2 0 2
461 6 said = pmcp_bus_send(aTHX_ ps, ST(0), PMCP_BUS_UPDATED, NULL, ST(1));
462 }
463 6 SP = PL_stack_base + ax - 1;
464 6 ST(0) = sv_2mortal(newSViv(said));
465 6 XSRETURN(1);
466 }
467  
468 /* DELETE's close, for pmcp_sess.h. */
469 6 static void pmcp_session_closed(pTHX_ pmcp_server *ps, SV *c, SV *sid)
470 {
471 6 (void)pmcp_bus_send(aTHX_ ps, c, PMCP_BUS_CLOSE, sid, NULL);
472 6 }
473  
474 #endif /* PMCP_BUS_H */
« include/pmcp/pmcp_otel.h ↑ Summary include/pmcp/pmcp_prompt.h »