summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-02-27 20:21:18 +0100
committerYorhel <git@yorhel.nl>2012-02-27 20:21:18 +0100
commit51e390e648fde3ca0173ba2cb40b798544d07891 (patch)
treeee5fdf79175dbf60bc8fd515dd467af38f7e604d
parentd5e8b2943af6af87fbe910382e1f2ae6feb5bada (diff)
c: Generalize patternreg and tn_returnpath for links and sessions
-rw-r--r--c/tanja.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/c/tanja.c b/c/tanja.c
index d0c77e1..f1ef460 100644
--- a/c/tanja.c
+++ b/c/tanja.c
@@ -366,10 +366,14 @@ int tn_json_fmt(tn_tuple *tup, char *buf, int l) {
struct tn_returnpath {
int ref; // Number of sessions sending to it. May be 0.
- tn_session *ses; // TODO: can also be a link
- // For sessions - links probably just want an integer instead
- tn_tuple_cb cb;
- void *data;
+ int type; // 0 = session, 1 = link
+ union {
+ struct {
+ tn_session *s;
+ tn_tuple_cb cb;
+ void *data;
+ } s;
+ } t;
};
@@ -391,20 +395,22 @@ static inline void tn_returnpath_open(tn_returnpath *rp) {
}
-static inline tn_returnpath *tn_returnpath_create(tn_session *ses, tn_tuple_cb cb, void *dat) {
+static inline tn_returnpath *tn_returnpath_create_ses(tn_session *ses, tn_tuple_cb cb, void *dat) {
tn_returnpath *rp = malloc(sizeof(tn_returnpath));
rp->ref = 0; // No senders yet
- rp->ses = ses;
- rp->cb = cb;
- rp->data = dat;
+ rp->type = 0;
+ rp->t.s.s = ses;
+ rp->t.s.cb = cb;
+ rp->t.s.data = dat;
tn_session_ref(ses);
return rp;
}
-// Called after the 'path closed' notification has been dispatched.
+// Called after the 'path closed' notification has been dispatched/handled.
static inline void tn_returnpath_free(tn_returnpath *rp) {
- tn_session_unref(rp->ses);
+ if(!rp->type)
+ tn_session_unref(rp->t.s.s);
free(rp);
}
@@ -419,21 +425,22 @@ typedef struct {
tn_tuple *pat; // Only accessed by the node, only valid if active=1
int active; // Atomic int, whether the pattern is still registered or not
int ref;
- int willreply;
- tn_session *ses; // TODO: Can also be a link
- // For sessions - links probably just want an integer instead
+ int willreply; // Always 1 for links
+ int type; // 0 = session, 1 = link
+ void *recipient; // either a tn_session or tn_link pointer
+ // For sessions, links don't need any additional data
tn_tuple_cb cb;
void *data;
} patternreg;
-static inline patternreg *patternreg_create(tn_tuple *pat, int willreply, tn_session *ses, tn_tuple_cb cb, void *dat) {
+static inline patternreg *patternreg_create_ses(tn_tuple *pat, int willreply, tn_session *ses, tn_tuple_cb cb, void *dat) {
patternreg *r = malloc(sizeof(patternreg));
r->ref = 1;
r->active = 1;
r->pat = pat;
r->willreply = willreply;
- r->ses = ses; // No need to ref() this, the pattern is unregistered when the session is closed anyway
+ r->recipient = ses; // No need to ref() this, the pattern is unregistered when the session is closed anyway
r->cb = cb;
r->data = dat;
return r;
@@ -620,7 +627,7 @@ tn_session *tn_session_create(tn_node *n, tn_session_context *ctx, void *data) {
void tn_session_send(tn_session *s, tn_tuple *t, tn_tuple_cb cb, void *dat) {
assert(s);
assert(t);
- tn_node_send(s->node, t, cb ? tn_returnpath_create(s, cb, dat) : NULL);
+ tn_node_send(s->node, t, cb ? tn_returnpath_create_ses(s, cb, dat) : NULL);
}
@@ -628,16 +635,18 @@ int tn_session_register(tn_session *s, tn_tuple *pat, int willreply, tn_tuple_cb
assert(s);
assert(pat);
assert(cb);
- return tn_node_register(s->node, patternreg_create(pat, willreply, s, cb, dat));
+ return tn_node_register(s->node, patternreg_create_ses(pat, willreply, s, cb, dat));
}
void tn_session_unregister(tn_session *s, int id) {
+ assert(s);
tn_node_unregister(s->node, id);
}
void tn_session_close(tn_session *s) {
+ assert(s);
// TODO: unregister any patterns
// TODO: free the message queue and close any return-paths
if(s->ctx->close)