Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it right to pass channel as channel message ? #177

Open
mryvz opened this issue Oct 16, 2017 · 1 comment
Open

is it right to pass channel as channel message ? #177

mryvz opened this issue Oct 16, 2017 · 1 comment

Comments

@mryvz
Copy link

mryvz commented Oct 16, 2017

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
Copy link
Owner

sustrik commented Oct 17, 2017

chan is just a pointer. You can pass it through a channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants