load balance forward, and route backward, specified your load_balance algo by sp_setopt() API

one-to-many

images/sp-reqrep-patterns-1.png

1 client

int eid = sp_endpoint(SP_REQREP, SP_REQ);

for (int i = 0; i < 10; i++)
    sp_add(eid, connfds[i]);

for (int i = 0; i < 10; i++) {
    req = "hello" + i;
    sp_send(eid, req);
    sp_recv(eid, &result);
    assert (req == result);
}

10 server

int eid = sp_endpoint(SP_REQREP, SP_REP);
sp_add(eid, listenfd);
for (;;) {
    sp_recv(eid, &msg);
    sp_send(eid, msg);
}

many-to-one

images/sp-reqrep-patterns-2.png

10 client

int eid = sp_endpoint(SP_REQREP, SP_REQ);
sp_add(eid, connfd);
sp_send(eid, req);
sp_recv(eid, &result);
assert(req == result);

1 server

int eid = sp_endpoint(SP_REQREP, SP_REP);
sp_add(eid, listenfd);
for (;;) {
    sp_recv(eid, &msg);
    sp_send(eid, msg);
}

many-to-many

images/sp-reqrep-patterns-3.png

10 client

int eid = sp_endpoint(SP_REQREP, SP_REQ);

for (i = 0; i < 10; i++)
    sp_add(eid, connfds[i]);

for (i = 0; i < 10; i++) {
    sp_send(eid, req);
    sp_recv(eid, &result);
    assert(req == result);
}

10 server

int eid = sp_endpoint(SP_REQREP, SP_REP);
sp_add(eid, listenfd);

for (;;) {
    sp_recv(eid, &msg);
    sp_send(eid, msg);
}

proxy

images/sp-reqrep-patterns-4.png

1 proxy

int frontend = sp_endpoint(SP_REQREP, SP_REQ);
int backend = sp_endpoint(SP_REQREP, SP_REP);
rc = sp_setopt(frontend, SP_PROXY, &backend, sizeof(backend));
assert (rc == 0);

10 client

int eid = sp_endpoint(SP_REQREP, SP_REQ);
sp_add(eid, connfd);

for (i = 0; i < 10; i++) {
    sp_send(eid, req);
    sp_recv(eid, &result);
    assert(req == result);
}

10 server

int eid = sp_endpoint(SP_REQREP, SP_REP);
sp_add(eid, connfd);

for (i = 0; i < 10; i++) {
    sp_recv(eid, &msg);
    sp_send(eid, msg);
}

pipelines

images/sp-reqrep-patterns-5.png

10 client

int eid = sp_endpoint(SP_REQREP, SP_REQ);
sp_add(eid, connfd);

for (i = 0; i < 10; i++) {
    sp_send(eid, req);
    sp_recv(eid, &result);
    assert(req == result);
}

1 server

int eid = sp_endpoint(SP_REQREP, SP_REP);
sp_add(eid, connfd);

queue waiting_process_msg;
queue done_msg;

for (;;) {
    sp_recv(eid, &msg);
    waiting_process_msg.push(msg);
    while (!done_msg.empty())
        sp_send(eid, done_msg.pop());
}

AUTHORS

Dong Fang <yp.fangdong@gmail.com>
Martin Sustrik <sustrik@250bpm.com>