Ignore:
Timestamp:
Nov 27, 2009, 1:46:25 PM (14 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Server's mainloop now repeatedly calls server_iteration()
File:
1 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/server.c

    r4051 r4052  
    4242static int handle_attach(struct screen_list *screen_list, char *buf);
    4343
    44 static int send_to_client(const char *msg, int size, struct screen_list *screen_list)
     44static int send_to_client(const char *msg, int size,
     45                          struct screen_list *screen_list)
    4546{
    4647    int ret;
     
    149150            written = 0;
    150151            sprintf(buf2, "UPDATE %10d %10d", x, y);
    151             ret = send_to_client(buf2, strlen(buf2)+1, screen_list);
     152            ret = send_to_client(buf2, strlen(buf2) + 1, screen_list);
    152153            if (ret < 29 && errno != EAGAIN)
    153154            {
     
    161162                /* Block to write the end of the message */
    162163                fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0);
    163                 n = send_to_client((char*) buf+written, towrite > bufsize ? bufsize : towrite, screen_list);
     164                n = send_to_client((char *)buf + written,
     165                                   towrite > bufsize ? bufsize : towrite,
     166                                   screen_list);
    164167                if (n < 0)
    165168                {
     
    178181                caca_get_cursor_y(screen_list->cv));
    179182        /* FIXME check value of r */
    180             int r = send_to_client(buf2, strlen(buf2) + 1,screen_list);
     183        int r = send_to_client(buf2, strlen(buf2) + 1, screen_list);
    181184        (void)r;
    182185#if defined HAVE_CACA_DIRTY_RECTANGLES
     
    201204}
    202205
     206static int server_iteration(struct screen_list *screen_list)
     207{
     208    int i;
     209    int eof = 0, refresh = 0;
     210
     211    int quit = 0;
     212    ssize_t n;
     213    char buf[128];
     214   
     215    /* Read program output */
     216    refresh |= update_screens_contents(screen_list);
     217   
     218    /* Check if we got something from the client */
     219    while (screen_list->socket[SOCK_SERVER]
     220           && (n =
     221               read(screen_list->socket[SOCK_SERVER], buf,
     222                    sizeof(buf) - 1)) > 0)
     223    {
     224        buf[n] = 0;
     225        debug("Received command %s", buf);
     226        if (!strncmp("ATTACH ", buf, 7))
     227        {
     228            refresh |= handle_attach(screen_list, buf);
     229        }
     230        else if (!strncmp("QUIT", buf, 4))
     231        {
     232            quit = 1;
     233        }
     234        else if (!strncmp("DELAY ", buf, 6))
     235        {
     236            /* FIXME check the length before calling atoi */
     237            screen_list->delay = atoi(buf + 6);
     238        }
     239        else if (!strncmp("RESIZE ", buf, 7))
     240        {
     241            caca_free_canvas(screen_list->cv);
     242            /* FIXME check the length before calling atoi */
     243            screen_list->cv =
     244            caca_create_canvas(atoi(buf + 7), atoi(buf + 18));
     245            screen_list->changed = 1;
     246            refresh = 1;
     247        }
     248        else if (!strncmp("KEY ", buf, 4))
     249        {
     250            unsigned int c = atoi(buf + 4);
     251            refresh |= handle_key(screen_list, c, refresh);
     252        }
     253        else
     254        {
     255            fprintf(stderr, "Unknown command received: %s\n", buf);
     256        }
     257    }
     258   
     259    /* No more screens, exit */
     260    if (!screen_list->count)
     261        return -1;
     262   
     263    /* User requested to exit */
     264    if (quit)
     265        return -2;
     266   
     267    /* Update each screen canvas */
     268    refresh |= update_terms(screen_list);
     269   
     270    /* Launch recurrents if any */
     271    refresh |= handle_recurrents(screen_list);
     272   
     273    /* Refresh screen */
     274    refresh |= refresh_screen(screen_list, refresh);
     275   
     276    eof = 1;
     277    for (i = 0; i < screen_list->count; i++)
     278        if (screen_list->screen[i]->fd >= 0)
     279            eof = 0;
     280    if (eof)
     281        return -3;
     282
     283}
     284
    203285static void server_main(struct screen_list *screen_list)
    204286{
    205     int i;
    206     int eof = 0, refresh = 1;
    207 
    208 
    209 
    210287    screen_list->last_key_time = 0;
    211288    screen_list->attached = 0;
     
    221298    for (;;)
    222299    {
    223         int quit = 0;
    224         ssize_t n;
    225         char buf[128];
    226 
    227         /* Read program output */
    228         refresh |= update_screens_contents(screen_list);
    229 
    230         /* Check if we got something from the client */
    231         while (screen_list->socket[SOCK_SERVER]
    232                && (n =
    233                    read(screen_list->socket[SOCK_SERVER], buf,
    234                         sizeof(buf) - 1)) > 0)
    235         {
    236             buf[n] = 0;
    237             debug("Received command %s", buf);
    238             if (!strncmp("ATTACH ", buf, 7))
    239             {
    240                 refresh |= handle_attach(screen_list, buf);
    241             }
    242             else if (!strncmp("QUIT", buf, 4))
    243             {
    244                 quit = 1;
    245             }
    246             else if (!strncmp("DELAY ", buf, 6))
    247             {
    248                 /* FIXME check the length before calling atoi */
    249                 screen_list->delay = atoi(buf + 6);
    250             }
    251             else if (!strncmp("RESIZE ", buf, 7))
    252             {
    253                 caca_free_canvas(screen_list->cv);
    254                 /* FIXME check the length before calling atoi */
    255                 screen_list->cv =
    256                     caca_create_canvas(atoi(buf + 7), atoi(buf + 18));
    257                 screen_list->changed = 1;
    258                 refresh = 1;
    259             }
    260             else if (!strncmp("KEY ", buf, 4))
    261             {
    262                 unsigned int c = atoi(buf + 4);
    263                 refresh |= handle_key(screen_list, c, refresh);
    264             }
    265             else
    266             {
    267                 fprintf(stderr, "Unknown command received: %s\n", buf);
    268             }
    269         }
    270 
    271         /* No more screens, exit */
    272         if (!screen_list->count)
    273             break;
    274 
    275         /* User requested to exit */
    276         if (quit)
    277             break;
    278 
    279         /* Update each screen canvas */
    280         refresh |= update_terms(screen_list);
    281 
    282         /* Launch recurrents if any */
    283         refresh |= handle_recurrents(screen_list);
    284 
    285         /* Refresh screen */
    286         refresh |= refresh_screen(screen_list, refresh);
    287 
    288         eof = 1;
    289         for (i = 0; i < screen_list->count; i++)
    290             if (screen_list->screen[i]->fd >= 0)
    291                 eof = 0;
    292         if (eof)
    293             break;
     300                if(server_iteration(screen_list)) break;
    294301    }
    295302
     
    301308    python_close(screen_list);
    302309#endif
    303    
     310
    304311    exit(0);
    305312}
     
    318325        long long int tdiff =
    319326            (current_time - screen_list->last_refresh_time) / 1000;
    320        
     327
    321328        if (screen_list->force_refresh)
    322329        {
     
    380387    int i;
    381388    debug("Screen list at %p\n", screen_list);
    382    
     389
    383390    /* Create socket and bind it */
    384391    create_socket(screen_list, SOCK_SERVER);
     
    560567        close(screen_list->outfd);
    561568#endif
    562         return 0;
     569    return 0;
    563570}
    564571
     
    576583    {
    577584        int r = install_fds(screen_list);
    578         if(r) return r;
     585        if (r)
     586            return r;
    579587        setsid();
    580588
Note: See TracChangeset for help on using the changeset viewer.