formatter_type create_formatter(logging::attribute_name const& name, args_map const& args)
 {
     args_map::const_iterator it = args.find("format");
     if (it != args.end())
         return boost::phoenix::bind(point_formatter(it->second), expr::stream, expr::attr< point >(name));
     else
         return expr::stream << expr::attr< point >(name);
 }
Beispiel #2
0
void body(const args_map &args){
    const char *buf1 = "html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\" /><title>Network Programming Homework 3</title></head><body bgcolor=#336699><font face=\"Courier New\" size=2 color=#FFFF99><table width=\"800\" border=\"1\"><tr>";
    printf("%s", buf1);
    for(int i=1;i<=5;i++){
        if(args.find("h" + to_string(i)) != args.end() && args.at("h" + to_string(i)) != ""){
            printf("<td>%s</td>\n", args.at("h" + to_string(i)).c_str());
        }
    }
    printf("</tr><tr>\n");
    for(int i=1;i<=5;i++){
        if(args.find("h" + to_string(i)) != args.end() && args.at("h" + to_string(i)) != ""){
            printf("<td valign=\"top\" id=\"m%d\"></td>\n", i);
        }
    }
    const char *buf2 = "</tr></table></body></html>";
    printf("%s", buf2);
    fflush(stdout);
}
Beispiel #3
0
void run(const args_map &args){
    fd_set rfds, wfds, n_rfds, n_wfds;
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    FD_ZERO(&n_rfds);
    FD_ZERO(&n_wfds);
    int max_cli_fd = 0;
    int clients_fd[10];
    memset(clients_fd, -1, sizeof(clients_fd));
    int clients_status[10];
    string batch[10];
    FILE *f_batch[10];
    printf("start\n");
    int conn = 0;
    for(int i=1;i<=5;i++){
        if(args.find("h" + to_string(i)) != args.end() && args.at("h" + to_string(i)) != ""){
            batch[i] = args.at("f" + to_string(i));
            f_batch[i] = fopen(args.at("f" + to_string(i)).c_str(), "r");
            printf("fd %p\n", f_batch[i]);
            if(f_batch[i] == NULL){
                continue;
            }
            hostent *host = gethostbyname(args.at("sh" + to_string(i)).c_str());
            sockaddr_in addr;
            int port = stoi(args.at("sp" + to_string(i)));
            int cli_fd = socket(AF_INET, SOCK_STREAM, 0);
            set_nonblock(cli_fd);
            bzero(&addr, sizeof(addr));
            addr.sin_family = AF_INET;
            addr.sin_addr = *(in_addr*)host->h_addr;
            addr.sin_port = htons(port);
            connect(cli_fd, (sockaddr*)&addr, sizeof(addr));
            //sleep(1);
            printf("cli %d\n", cli_fd);
            max_cli_fd = max(max_cli_fd, cli_fd);
            clients_fd[i] = cli_fd;
            clients_status[i] = C_CONNECTING1;
            FD_SET(cli_fd, &n_rfds);
            FD_SET(cli_fd, &n_wfds);
            conn ++;
        }
    }
    while(conn){
        memcpy(&rfds, &n_rfds, sizeof(n_rfds));
        memcpy(&wfds, &n_wfds, sizeof(n_wfds));
        if(select(max_cli_fd + 1, &rfds, &wfds, NULL, NULL) < 0)
            exit(1);
        for(int i=0;i<=5;i++){
            if(clients_fd[i] != -1){
                if(FD_ISSET(clients_fd[i], &rfds)){
                    char t_buf[BUF_SIZE] = {0};
                    if(clients_status[i] == C_CONNECTING2){
                        read(clients_fd[i], t_buf, BUF_SIZE);
                        if(t_buf[1] == 0x5A){
                            printf("acc\n");
                            clients_status[i]  = C_READING;
                        }else{
                            printf("den\n");
                            close(clients_fd[i]);
                            FD_CLR(clients_fd[i], &n_wfds);
                            FD_CLR(clients_fd[i], &n_rfds);
                            clients_fd[i] = -1;
                            conn --;
                        }
                    }else{
                        if(read(clients_fd[i], t_buf, BUF_SIZE) == 0){
                            printf("read cli close %d %d\n", i, clients_fd[i]);
                            close(clients_fd[i]);
                            FD_CLR(clients_fd[i], &n_wfds);
                            FD_CLR(clients_fd[i], &n_rfds);
                            clients_fd[i] = -1;
                            conn --;
                            continue;
                        }
                        clients_status[i] = has_prompt(t_buf) ? C_WRITING : C_READING;
                        string buf = html_encode(t_buf);
                        echo_msg(i, buf);
                    }
                }else if (f_batch[i] && clients_status[i] == C_WRITING && FD_ISSET(clients_fd[i], &wfds)){
                    char buf[BUF_SIZE] = {0};
                    if(fgets(buf, BUF_SIZE, f_batch[i]) == NULL){
                        f_batch[i] = NULL;
                        printf("fgets cli close %d %d\n", i, clients_fd[i]);
                        //close(clients_fd[i]);
                        FD_CLR(clients_fd[i], &n_wfds);
                       //FD_CLR(clients_fd[i], &n_rfds);
                        //clients_fd[i] = -1;
                        //conn --;
                        continue;
                    }
                    echo_msg(i, "<b>" + html_encode(buf) + "</b>");
                    if(write(clients_fd[i], buf, strlen(buf)) <= 0){
                        printf("write cli close %d %d\n", i, clients_fd[i]);
                        close(clients_fd[i]);
                        FD_CLR(clients_fd[i], &n_wfds);
                        FD_CLR(clients_fd[i], &n_rfds);
                        clients_fd[i] = -1;
                        conn --;
                        continue;
                    }
                    clients_status[i] = C_READING;
                }else if(clients_status[i] == C_CONNECTING1 && FD_ISSET(clients_fd[i], &wfds)){
                    char buf[10];
                    hostent *s_host = gethostbyname(args.at("h" + to_string(i)).c_str());
                    sockaddr_in addr;
                    addr.sin_addr = *(in_addr*)s_host->h_addr;
                    int port;
                    port = stoi(args.at("p" + to_string(i)));
                    buf[0] = 4;
                    buf[1] = 0x01;
                    buf[2] = port / 256;
                    buf[3] = port % 256;
                    buf[4] = addr.sin_addr.s_addr & 255;
                    buf[5] = addr.sin_addr.s_addr >> 8 & 255;
                    buf[6] = addr.sin_addr.s_addr >> 16 & 255;
                    buf[7] = addr.sin_addr.s_addr >> 24 & 255;
                    int n = write(clients_fd[i], buf, 8);
                    printf("n = %d\n", n);
                    clients_status[i] = C_CONNECTING2;
                }
            }
        }
    }