示例#1
0
int main(int argc, char** argv) {

    //*****DEALING WITH ARGUEMENTS!!
    if(argc < 3){
        printf("wrong number of arguments! (%i)\n", (argc-1));
        exit(1);
    }

    int port = atoi(argv[1]);

    if(port < 1 || port > 0xffff){
        perror("port is invalid. i.e. > 0xffff or < 1 or not a number");
        exit(1);
    }

    //do something with the musics
    int num_stations = argc - 2;
    char* stations[num_stations];
    pthread_mutex_t thrmutex[num_stations];

    fd_set master;
    fd_set readfds;
    int maxfd;

    FD_ZERO(&master);
    FD_ZERO(&readfds);


    struct sockaddr_in temp;

    struct client *root = (struct client *) malloc(sizeof(struct client));
    root->cfd = -1;
    memcpy(&root->sockaddr_cli, &temp, sizeof(temp));
    root->station = -1;
    root->udpport = -1;
    root->c = NULL;
    
    struct threadArgs thread_args[num_stations];

    int i;
    for(i = 0; i<num_stations; i++){
        stations[i] = argv[i+2];
        pthread_mutex_init(&thrmutex[i], NULL);
        thread_args[i].station_number = i;
        thread_args[i].num_stations = num_stations;
        thread_args[i].master = &master;
        thread_args[i].file = stations[i];
        thread_args[i].root = root;
        thread_args[i].mutex = &thrmutex[i];
    }



    //*****INITIATION OF THREADS FOR STATIONS!
    
    
    pthread_t threads[num_stations];

    for(i=0; i<num_stations; i++){
        int t = pthread_create(&threads[i], NULL, station_thread, &thread_args[i]);
        if(t != 0){
            perror("failed to create threads for the stations!");
            exit(1);
        }
    }



    //*****SOCKET CONNECTION!
    //socket part
    int fd = tcpsocket(port, INADDR_ANY);
    FD_SET(0, &master);
    FD_SET(fd, &master);

    if(listen (fd, 5) < 0){
        close(fd);
        perror("could not listen!");
        exit(1);
    }

    struct sockaddr_in sin;
    bzero (&sin, sizeof (sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons (port);
    sin.sin_addr.s_addr = htonl (INADDR_ANY);
    socklen_t sinlen;

    struct client helloList = {-1, temp, -1, -1, NULL};
    fd_set hello;

    FD_ZERO(&hello);

    maxfd = fd;

    for(;;){

        readfds = master;

        if(select(maxfd+1, &readfds, NULL, NULL, NULL) < 0){
            perror("select error");
            exit(1);
        }


        int i;
        for(i = 0; i <= maxfd; i++){
            if(FD_ISSET(i, &readfds)){

                //if the server requests from stdin
                if(i == 0){
                    char in[1];
                    if(scanf("%s", in) == EOF){
                        write(1, "**failed to read the input.\n", sizeof("**failed to read the input.\n"));
                    }

                    //quit the server
                    if(in[0] == 'q'){
                        close(fd);
                        exit(1);
                    }

                    //print out the station list;
                    if(in[0] == 'p'){
                        printStations(num_stations, root);
                    }
                }

                //if new client connects to the server.
                else if(i == fd){
                    
                    struct client *newcli = (struct client*) malloc(sizeof(struct client));
                    bzero(newcli, sizeof(struct client));
                    sinlen = sizeof(newcli->sockaddr_cli);

                    int newfd = accept(fd, (struct sockaddr *) &newcli->sockaddr_cli, &sinlen);
                    if(newfd < 0){
                        free(newcli);
                        perror("accept error");
                    }else{
                        FD_SET(newfd, &master);
                        if(newfd > maxfd)
                            maxfd = newfd;

                        //add to hello list
                        FD_SET(newfd, &hello);

                        
                        newcli->cfd = newfd;
                        newcli->udpport = -1;
                        newcli->station = -1;
                        newcli->c = NULL;
                        
                        addToCliList(&helloList, newcli, NULL, 0);

                        printToSTD(i, "connection accepted. Expecting hello.\n");
                    }
                }

                //if the client has not sent a hello yet
                else if(FD_ISSET(i, &hello)){

                    FD_CLR(i, &hello);

                    char hello[3];
                    int receive_hello = recv(i, hello, sizeof(hello), 0);
                    if(receive_hello < 0){

                        printToSTD(i, "");
                        perror("Failed to receive the request!");

                        //closing connection
                        FD_CLR(i, &master);
                        free(popCli(root, i, thrmutex, num_stations));
                        close(i);
                        printToSTD(i, "closed the connection.\n");
                    }else if(receive_hello == 0){
                        //closing connection
                        FD_CLR(i, &master);
                        free(popCli(root, i, thrmutex, num_stations));
                        close(i);

                        printToSTD(i, "the client closed the connection.\n");
                    }else{

                        if((uint8_t)hello[0] != 0){

                            sendInvalidMessage(i, "wrong type of request. hello expected.", &master, root, thrmutex, num_stations);
                            printToSTD(i, "wrong type of request requested.\n");


                            //closing connection
                            FD_CLR(i, &master);
                            free(popCli(root, i, thrmutex, num_stations));
                            close(i);
                            printToSTD(i, "closed the connection.\n");
                        }else{
                            uint16_t cport = (uint16_t)hello[1] << 8;
                            cport += (uint8_t)hello[2];

                            //update the client's port
                            struct client *cli = popCli(&helloList, i, NULL, 0);
                            
                            if(cli == NULL){
                                printToSTD(i, "error with connection, the client could not be found.\n");

                                FD_CLR(i, &master);
                                close(i);
                            }else{
                                cli->udpport = cport;
                                cli->sockaddr_cli.sin_port = htons(cport);
                                cli->sockaddr_cli.sin_family = AF_INET;


                                addToCliList(root, cli, &thrmutex, num_stations);

                                //send welcome
                                char welcome[3];
                                welcome[0] = 0;
                                welcome[1] = (uint8_t)(num_stations >> 8);
                                welcome[2] = (uint8_t)num_stations;

                                send(i, welcome, sizeof(welcome), 0);

                                printToSTD(i, "hello received. welcome sent. expecting station.\n");
                            }
                        }
                    }
                }

                //if the client has completed the handshake
                else{
                    
                    char message[3];
                    int received = recv(i, message, sizeof(message), 0);
                    if(received < 0){
                        printToSTD(i, "");
                        perror("Failed to receive the request!");

                        //closing connection
                        FD_CLR(i, &master);
                        free(popCli(root, i, thrmutex, num_stations));
                        close(i);
                        
                        printToSTD(i, "closed the connection.\n");
                    }else if(received == 0){
                        //closing connection
                        FD_CLR(i, &master);
                        free(popCli(root, i, thrmutex, num_stations));
                        close(i);
                        
                        printToSTD(i, "the client closed the connection.\n");
                    }else{
                        if((uint8_t)message[0] != 1){

                            sendInvalidMessage(i, "wrong type of request. setstation request expected.", &master, root, thrmutex, num_stations);
                            printToSTD(i, "invalid commmand received. corresponding message sent.\n");

                            //closing connection
                            FD_CLR(i, &master);
                            free(popCli(root, i, thrmutex, num_stations));
                            close(i);
                            printToSTD(i, "closed the connection.\n");
                        }else{

                            uint16_t station = (uint16_t) message[1] << 8;
                            station += (uint8_t)message[2];
                            station = station;

                            //check to see if the station exists.
                            if(station >= num_stations){
                                char *out;
                                int size = sprintf(out, "the station %d does not exist.", station);
                                sendInvalidMessage(i, out, &master, root, thrmutex, num_stations);

                                //closing connection
                                FD_CLR(i, &master);
                                free(popCli(root, i, thrmutex, num_stations));
                                close(i);
                                printToSTD(i, "closed the connection.\n");
                            }else{
                                if(addToStation(station, stations[station], getCli(root, i),
                                                &master, root, thrmutex, num_stations) == 0){
                                    printToSTD(i, "the client's information could not be found in the root list.\n");

                                    FD_CLR(i, &master);
                                    close(i);
                                    printToSTD(i, "closed the connection.\n");
                                }
                            }
                        }
                    }
                }
            }
        }      
示例#2
0
文件: map.cpp 项目: pjwolfe41/MackOil
void map (int argc, char *argv[], HWND hWnd, char *dirpath, char *map_filename)
{
    char *datatitle;
    char *maptitle;
    char *zname;
    DATATYPE data_type;
    double hscale, vscale;
    double xmin, xmax, ymin, ymax;
    double *x, *y, *z;
    double *fit, *resid;
    int fit_degree;
    int i, k;
    int npoint;
    int zdigits;
    unsigned long *code;

    /*	get data to plot	*/

    npoint = prepdata (argc, argv,
                       &datatitle, &data_type, &code, &x, &y, &z,
                       &fit_degree, &fit, &resid);

    /*	use select conditions to select points	*/

    k = 0;
    for (i = 0; i < npoint; i++)
    {
        double fitval, residval;
        if (fit_degree > 0) {
            fitval = fit[i];
            residval = resid[i];
        }
        else {
            fitval = residval = 0.0;
        }
        if (opt_select (code[i], x[i], y[i], z[i], fit_degree, fitval, residval))
        {
            code[k] = code[i];
            x[k] = x[i];
            y[k] = y[i];
            z[k] = z[i];
            if (fit_degree)
            {
                fit[k] = fit[i];
                resid[k] = resid[i];
            }
            ++k;
        }
    }
    npoint = k;
    if (npoint == 0)
        error_stop ("no points remain after selections in output.opt","");

    /*	prepare map title	*/

    opt_zvalue (&zname, &zdigits);
    if (strlen (datatitle) > 100 || strlen (zname) > 100)
        error_stop ("data title or observed value name too long", "");
    maptitle = (char *) malloc (250);
    if (maptitle == NULL)
        error_stop ("cannot allocate vector for map title", "");
    switch (data_type)
    {
    case CODE:
        sprintf_s (maptitle, 250, "%s - Station Codes", datatitle);
        break;
    case OBS:
        sprintf_s (maptitle, 250, "%s - %s Values", datatitle, zname);
        break;
    case FIT:
        sprintf_s (maptitle, 250, "%s - Degree %d Fit of %s Values",
                   datatitle, fit_degree, zname);
        break;
    case RESID:
        sprintf_s (maptitle, 250, "%s - Degree %d Fit Residual of %s Values",
                   datatitle, fit_degree, zname);
        break;
    }

    /*	determine range of coordinate values	*/

    xmin = ymin = 1.e+30;
    xmax = ymax = -xmin;
    for (i = 0; i < npoint; i++)
    {
        xmin = x[i] < xmin ? x[i] : xmin;
        ymin = y[i] < ymin ? y[i] : ymin;
        xmax = x[i] > xmax ? x[i] : xmax;
        ymax = y[i] > ymax ? y[i] : ymax;
    }

    /*	convert coordinates to map scale inches, assuming that the	*/
    /*	x and y values are longitude and latitude, respectively		*/

    {
        double center_latitude = (ymin + ymax) / 2;
        double cos_cent;
        double degrees_per_radian = M_PI / 180.;
        double s, t;
        long ratio;

        opt_scale (&ratio);

        cos_cent = cos (center_latitude * degrees_per_radian);
        t = 1 - cos_cent * cos_cent * 0.006693422;
        s = (cos_cent * 2.5026656e+8) / sqrt (t);
        hscale = ratio / (s * degrees_per_radian);
        t = (6.305541e+16 - 0.006693422 * s * s);
        t = t * sqrt (t) / 6.284403e+16;
        vscale = ratio / (t * degrees_per_radian);

        for (i = 0; i < npoint; i++)
        {
            x[i] = - x[i] / hscale;	/* switch sign on longitude for */
            /* correct map orientation	*/
            y[i] = y[i] / vscale;
        }

        {   /* must also switch sign on	*/
            double hold = xmin;		/* xmin and xmax		*/
            xmin = - xmax / hscale;
            xmax = - hold / hscale;
        }

        ymin = ymin / vscale;
        ymax = ymax / vscale;
    }

    /*	create pdf map file */

    startPrint(hWnd, dirpath, map_filename, maptitle);
    printHeader(maptitle);
    printStations(data_type, npoint, code, x, y, z, fit, resid,
                  hscale, vscale, xmin, xmax, ymin, ymax, zdigits);
    endPrint();
    return;
}