Пример #1
0
int main(int argc, char **args){
    char* SENDER = NULL;

    if (argc > 1 ) {
        SENDER = args[1];
    }
    printf("Handler ID : %s\n", SENDER);
    bstring pull_addr = bfromcstr("tcp://127.0.0.1:9999");
    bstring pub_addr  = bfromcstr("tcp://127.0.0.1:9998");

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
    if (SENDER != NULL) {
        mongrel2_set_identity(pub_socket, SENDER);
    }

    const bstring headers = bfromcstr("HTTP/1.1 200 OK\r\nDate: Fri, 07 Jan 2011 01:15:42 GMT\r\nStatus: 200 OK\r\nConnection: close");
    mongrel2_request *request;

    while(1){
        request = mongrel2_recv(pull_socket);
        btoupper(request->body);
        mongrel2_reply_http(pub_socket, request, headers, request->body);
        mongrel2_disconnect(pub_socket, request);
        mongrel2_request_finalize(request);
    }

    bdestroy(headers);

    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    return 0;
}
Пример #2
0
int main(int argc, char **args){
    if(argc != 3){
        fprintf(stderr,"%s RECV SEND\n",args[0]);
        exit(1);
    }
    signal(SIGINT,&call_for_stop);
    
    bstring pull_addr = bfromcstr(args[1]);
    bstring pub_addr  = bfromcstr(args[2]);

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
mongrel2_set_identity(pub_socket, bdata(&SENDER) );

    mongrel2_request *request;

    // Polling is done to show how to do a clean shutdown
    int poll_response;
    zmq_pollitem_t socket_tracker;
    socket_tracker.socket = pull_socket->zmq_socket;
    socket_tracker.events = ZMQ_POLLIN;

    // Let's try out some ADT goodness
    dict_t* dict = dict_create(DICTCOUNT_T_MAX, compare_session);
    dict_set_allocator(dict, alloc_dict, free_dict, NULL);

    dnode_t* tempnode = NULL;
    m2_ws_session_data *counter = NULL;
    int retval = 0;
    while(shutdown != 1){
        poll_response = zmq_poll(&socket_tracker,1,500*1000);
        if(poll_response > 0){
            request = mongrel2_recv(pull_socket);
            fprintf(stdout,"got something...\n");

            if(request != NULL && mongrel2_request_for_disconnect(request) != 1){
                m2_ws_session_id* incoming = calloc(1,sizeof(m2_ws_session_id));
                incoming->req = request;
                printf("Looking at incoming->conn_id = %d\n",incoming->req->conn_id);
                tempnode = dict_lookup(dict,incoming);

                if(tempnode == NULL){
                    mongrel2_ws_reply_upgrade(request,pub_socket);
                    counter = calloc(1,sizeof(m2_ws_session_data));
                    counter->times_seen = 0;
                    retval = dict_alloc_insert(dict,incoming,counter);
                    assert(retval == 1);
                } else {
                    free(incoming);
                    counter = dnode_get(tempnode);
                    counter->times_seen += 1;
                }

                if(blength(request->body) > 0){
                    if(tempnode && mongrel2_ws_frame_get_fin(blength(request->body),
                                                             (uint8_t*)bdata(request->body))){
                        printf("Hey, it's a close\n");
                        dict_delete_free(dict,tempnode);
                        mongrel2_disconnect(pub_socket,request);
                    }
                } else {
                    bstring randmsg = genrandmsg();
                    mongrel2_ws_reply(pub_socket,request,randmsg);
                }
                

                printf("FYI: we've got %ld entries\n",dict_count(dict));
            } else {
                fprintf(stdout,"Connection %d disconnected\n", request->conn_id);
            }
        } else if (poll_response < 0){
            fprintf(stdout, "Error on poll!");
            shutdown = 1;
        }
    }
    // bstring msg = bformat("{\"msg\" : \"hi there %d\"}", request->conn_id);
    // fprintf(stdout,"Sending new msg: '%*s'",blength(msg),bdata(msg));
    // mongrel2_ws_reply(pub_socket,request,msg);
    // bdestroy(msg);
    // mongrel2_request_finalize(request);
    // mongrel2_reply(pub_socket,request,bfromcstr(""));
    
    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    fprintf(stdout,"\nClean shutdown done! Thanks for playing!\n");
    return 0;
}
Пример #3
0
int main(int argc, char **args){
    signal(SIGINT,&call_for_stop);

    if(argc != 3){
        fprintf(stderr,"%s RECV SEND\n",args[0]);
        exit(1);
    }
    bstring pull_addr = bfromcstr(args[1]);
    bstring pub_addr  = bfromcstr(args[2]);

    mongrel2_ctx *ctx = mongrel2_init(1);
    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));
    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
mongrel2_set_identity(pub_socket, bdata(&SENDER) );
    
    // Polling is done to show how to do a clean shutdown
    int poll_response;
    zmq_pollitem_t socket_tracker;
    socket_tracker.socket = pull_socket->zmq_socket;
    socket_tracker.events = ZMQ_POLLIN;

    // Let's try out some C-style ADT goodness
    m2_ws_sessions_state sessions;
    mongrel2_ws_sessions_state_init(&sessions);

    mongrel2_ws_sessions_state_unlock(&sessions);
    mongrel2_request *request = NULL;
    while(shutdown != 1){
        poll_response = zmq_poll(&socket_tracker,1,500*1000);
        if(poll_response > 0){
            request = mongrel2_recv(pull_socket);

            if(request != NULL && mongrel2_request_for_disconnect(request) != 1){
                if(mongrel2_ws_sessions_state_contains(&sessions,request)){
                    printf("We had it already\n");
                } else {
                    mongrel2_ws_reply_upgrade(request,pub_socket);
                    mongrel2_ws_sessions_state_add(&sessions,request);
                }

                if(blength(request->body) > 0){
                    // mongrel2_ws_frame_debug(blength(request->body),(uint8_t*)bdata(request->body));
                    if(OP_CLOSE == mongrel2_ws_frame_get_opcode(blength(request->body),
                                                                (uint8_t*)bdata(request->body))){
                        printf("Hey, it's a close\n");
                    } else {
                        printf("Not a OP_CLOSE. bstring length: '%d'\n",blength(request->body));
#ifndef NDEBUG
                        mongrel2_ws_frame_debug(blength(request->body), (uint8_t*)bdata(request->body));
#endif
                    }
                } else {
                    // mongrel2_ws_reply(pub_socket,request,(const bstring)&HELLO);
                    mongrel2_ws_broadcast(pub_socket,&sessions,(const bstring)&HELLO);
                }
            } else {
                fprintf(stdout,"Connection %d disconnected\n", request->conn_id);
                mongrel2_ws_sessions_state_remove(&sessions,request);
                mongrel2_disconnect(pub_socket,request);
            }

        } else if(poll_response < 0) {
            printf("ZMQ poller had an error");
        }
    }

    printf("\nThanks for playing\n");

    return 0;
}
Пример #4
0
int main(int argc, char **args){
    bstring pull_addr = bfromcstr("tcp://127.0.0.1:8999");
    bstring pub_addr  = bfromcstr("tcp://127.0.0.1:8998");

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx,bdata(&SENDER));
    mongrel2_connect(pull_socket, bdata(pull_addr));

    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));

    const bstring headers = bfromcstr("HTTP/1.1 200 OK\r\nDate: Fri, 07 Jan 2011 01:15:42 GMT\r\nStatus: 200 OK\r\nConnection: close");
    mongrel2_request *request;

    // Need to set umask to allow file creation... then restore? Not sure!
    mode_t process_mask = umask(0);
    int retval;
    retval = mkfifo("handler_pipe", S_IRUSR);
    umask(process_mask);
    if(retval != 0){
        switch(errno){
            case EACCES :
                fprintf(stderr,"Insufficient permissions to create fifo handler_pipe");
            case EEXIST :
                //fprintf(stderr,"handler_pipe already exists. That's cool tho.");
                goto no_mkfifo_error;
            case ENAMETOOLONG :
                fprintf(stderr,"handler_pipe is too long of a name. Unlikely!");
                break;
            case ENOENT :
                fprintf(stderr,"path for handler_pipe does not exist");
                break;
            case ENOSPC :
                fprintf(stderr,"no more room for handler_pipe");
                break;
            case ENOTDIR :
                fprintf(stderr,"path for handler_pipe is not a directory");
                break;
            case EROFS :
                fprintf(stderr,"handler_pipe cannot be created on read only fs");
                break;
            default:
                fprintf(stderr,"Error creating fifo. Not sure what though. Sorry! %d",errno);
                break;
        }
        exit(EXIT_FAILURE);
    }
    no_mkfifo_error:
    fprintf(stdout,"Created handler_pipe AOK\n");
    
    FILE *fifofd = fopen("handler_pipe","r");
    if(fifofd == NULL){
        fprintf(stderr,"Could not open handler_pipe");
        exit(EXIT_FAILURE);
    }

    size_t read_size;
    request = mongrel2_recv(pull_socket);
    // A 1k buffer
    void* fifo_buffer = calloc(1024,1);
    //while(1){
        read_size = fread(fifo_buffer, 1024, 1, fifofd);
        fprintf(stdout,"read_size from fifo: %zd", read_size); // z is for size_t's... weird!
        bdestroy(request->body);
        request->body = bfromcstralloc(1024,(const char*)fifo_buffer);
        mongrel2_reply_http(pub_socket, request, headers, request->body);
        mongrel2_disconnect(pub_socket, request);
    //}
    mongrel2_request_finalize(request);

    bdestroy(headers);

    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    return 0;
}