is it right to pass channel as channel message ?
mryvz opened this issue · 1 comments
mryvz commented
hi,
i try to create simple actor model with libmill :). i found a way but im not sure is it right way to do. im currently passing response channel as parameter to request listener. Is there something wrong with idea? i check memory is not groving and no message corruption in response...
thanks for advice ^^
//============= types.h ==========
typedef struct{
char *name;
chan resp_ch;
} reqtype;
typedef struct {
int type;
union{
char *strval;
int intval;
}data;
} resptype;
// ========== main.c =========
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libmill-1.18/libmill.h"
#include "types.h"
coroutine void doit(chan ch) {
while (1) {
reqtype req = chr(ch, reqtype);
resptype resp ;
resp.type = 0;
// resp.data.strval = malloc(1024);
char strval[40];
strval[0]='\0';
strcat(strval, "Hello ");
strcat(strval, req.name);
resp.data.strval = strval;
chs(req.resp_ch, resptype, resp);
//chdone(req.resp,resptype,NULL);
}
}
resptype send_req(chan *ch_req,reqtype *req) {
chan ch_resp = chmake(resptype, 0);
req->resp_ch = ch_resp;
chs(*ch_req, reqtype, *req);
resptype resp = chr(ch_resp, resptype);
chclose(ch_resp);
req->resp_ch = NULL;
return resp;
}
int main(int argc, char** argv) {
int port = 5555;
if (argc > 1)
port = atoi(argv[1]);
chan ch_req_listen = chmake(reqtype, 0);
go(doit(ch_req_listen));
for (int i = 0; i < 1000; i++) {
char name[100];
sprintf(name, "%d", i);
reqtype req;
req.name = name;
resptype resp = send_req(&ch_req_listen,&req);
printf("Response For %s = \"%d : %s\"\n", req.name, resp.type, resp.data.strval);
}
getc(stdin);
}
sustrik commented
chan is just a pointer. You can pass it through a channel.