コード例 #1
0
ファイル: client.c プロジェクト: orez-/imgserver
static char *recvline() {
  char *line, *start, *end;
  size_t r;
  size_t remain = BUFFER_SIZE - buffer->used;
  if (buffer->used > 0) {
    start = buffer->data;
    end = (char *)memchr((void *)start, '\n', buffer->used);
    if (end) { /* Found a newline */
      *end = '\0'; /* Replace newline with null to return */
      r = end - start;
      line = estrndup(start, r);
      buffer->used -= r + 1;
      start = end + 1;
      memmove(buffer->data, start, buffer->used);
      trim_in_place(line);
      return line;
    } else if (remain == 0) {
      fprintf(stderr, "Fatal: Line exceeds buffer length\n");
      global_exit(0);
    }
  }
  if (remain > 0) {
    r = recv(sockfd, (buffer->data+buffer->used), remain, 0);
    if (r == -1) {
      perror("recv");
      global_exit(1);
    } else if (r == 0) {
      fprintf(stderr,"Server dropped connection.\n");
      global_exit(1);
    }
    buffer->used += r;
    return recvline();
  }
}
コード例 #2
0
ファイル: fp_fake.c プロジェクト: patrickmarlier/etes603-dev
int main()
{
	global_init();
	/* TODO add other things here? */
	global_exit();

	return 0;
}
コード例 #3
0
ファイル: client.c プロジェクト: orez-/imgserver
static void interrupt(int sig){
  static int called = 0;
  if (!called) {
    called = 1;
    fprintf(stderr,"%c[2D", 27);
		fprintf(stderr,"%c[0Kinterrupt\n", 27);
    global_exit(0);
  }
}
コード例 #4
0
ファイル: gui.c プロジェクト: patrickmarlier/etes603-dev
int main(int argc, char *argv[])
{
  pthread_t thread;
  int screen_number;
  int i;
  XEvent event;
  struct fp_img_dev *dev;

  if (argc >= 2) {
    strFilename = argv[1];
  } else {
    strFilename = strFilenameDefault;
  }
  /* Multi-thread app */
  XInitThreads();
  dis = XOpenDisplay(NULL);
  if (NULL == dis) {
    fprintf(stderr, "unable to open display\n");
    return EXIT_FAILURE;
  }

  screen_number = DefaultScreen (dis);
  //parent = RootWindow (dis, screen_number);
  parent = DefaultRootWindow(dis);

  fg = BlackPixel(dis, screen_number);
  bg = WhitePixel(dis, screen_number);

  mainwin = XCreateSimpleWindow(dis, parent, 0, 0, win_width, win_height, 0, fg, bg);
  if (mainwin == None) {
    fprintf (stderr, "unable to create window\n");
    return EXIT_FAILURE;
  }

  gc = XCreateGC(dis, mainwin, 0, NULL);
  if (gc == NULL) {
    fprintf (stderr, "unable to allocate GC\n");
    return EXIT_FAILURE;
  }

  // set up font
  if ((font_info = XLoadQueryFont(dis, fontname)) == NULL) {
    perror("XLoadQueryFont");
    exit(1);
  }
  XSetFont(dis, gc, font_info->fid);

  // Create Cursor for Buttons
  mycursor = XCreateFontCursor(dis, XC_hand1);

  // Quit Button
  button_quit = XCreateSimpleWindow(dis, mainwin, 300, 10, BUTTON_WIDTH, BUTTON_HEIGHT, 1, fg, bg);
  XChangeWindowAttributes(dis, button_quit, CWBackingStore, &setwinattr);
  XSelectInput(dis, button_quit, BUTTON_MASK);
  XDefineCursor(dis, button_quit, mycursor);
  XMapWindow(dis, button_quit);

  // Mode Buttons
  for (i = 0 ; i < 3; i++) {
    button_modes[i] = XCreateSimpleWindow(dis, mainwin, 300+i*65, 30, BUTTON_WIDTH, BUTTON_HEIGHT, 1, fg, bg);
    XChangeWindowAttributes(dis, button_modes[i], CWBackingStore, &setwinattr);
    XSelectInput(dis, button_modes[i], BUTTON_MASK);
    XDefineCursor(dis, button_modes[i], mycursor);
    XMapWindow(dis, button_modes[i]);
  }

  // Drawing area
  draw_win = XCreateSimpleWindow(dis, mainwin, 0, 0, image_width, image_height, 1, fg, bg);
  XSelectInput(dis, draw_win, BUTTON_MASK);
  XMapWindow(dis, draw_win);
  draw_gc = XCreateGC(dis, draw_win, 0, NULL);
  if (draw_gc == NULL) {
    fprintf (stderr, "unable to allocate Draw_win GC\n");
    return EXIT_FAILURE;
  }

  // Create tuning buttons
  int y = 60, id = 0;
  MakeButton(300, y, "IncReg03", IncReg03, id);
  MakeButton(400, y, "DecReg03", DecReg03, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncReg04", IncReg04, id);
  MakeButton(400, y, "DecReg04", DecReg04, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncReg10", IncReg10, id);
  MakeButton(400, y, "DecReg10", DecReg10, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncReg1A", IncReg1A, id);
  MakeButton(400, y, "DecReg1A", DecReg1A, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncReg93", IncReg93, id);
  MakeButton(400, y, "DecReg93", DecReg93, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncReg94", IncReg94, id);
  MakeButton(400, y, "DecReg94", DecReg94, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncGain", IncGain, id);
  MakeButton(400, y, "DecGain", DecGain, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncVRT", IncVRT, id);
  MakeButton(400, y, "DecVRT", DecVRT, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncVRB", IncVRB, id);
  MakeButton(400, y, "DecVRB", DecVRB, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncDTVRT", IncDTVRT, id);
  MakeButton(400, y, "DecDTVRT", DecDTVRT, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncVCO_C", IncVCO_C, id);
  MakeButton(400, y, "DecVCO_C", DecVCO_C, id+1);
  y += 20; id += 2;
  MakeButton(300, y, "IncDCOff", IncDCOffset, id);
  MakeButton(400, y, "DecDCOff", DecDCOffset, id+1);

  // XSelectInput (dis, mainwin, ExposureMask | KeyPressMask | ButtonPressMask);
  XMapWindow(dis, mainwin);
  XFlush(dis);




  dev = global_init();

  if (dev == NULL || dev->udev == NULL) {
    fprintf(stderr, "Cannot open device\n");
  }

  /* create thread */
  if (dev != NULL && dev->udev != NULL)
    pthread_create(&thread, NULL, thread_entry, (void*)dev);

  while (1)
    {
      if (XCheckWindowEvent(dis, button_quit, BUTTON_MASK, &event)) {
        if (HandleQuitButton(&event))
          goto finishing;
      }
      /* All modes buttons */
      for (i = 0; i < 3; i++) {
        if (XCheckWindowEvent(dis, button_modes[i], BUTTON_MASK, &event))
          HandleModeButtons(&event, i);
      }
      /* All regs buttons */
      for (i = 0; i < 128; i++) {
        if (XCheckWindowEvent(dis, MenuButton[i].window, BUTTON_MASK, &event))
          HandleRegButton(&event, i, dev);
      }
      /* TODO not BUTTON MASK but only? */
      if (XCheckWindowEvent(dis, draw_win, BUTTON_MASK, &event))
        HandleDraw(&event);
#if 0
      XNextEvent (dis, &event);

      switch (event.type)
        {
          case ButtonPress:
            /* The man page for XButtonEvent documents this. */
            printf ("You pressed button %d\n", event.xbutton.button);
            modifyLive (dis, win, gc);
            break;
          case Expose:
            //printf ("Redrawing from Expose.\n");
            draw (dis, win, gc);
            break;
          case MapNotify:
            printf ("MapNotify Event.\n");
            break;
          case KeyPress:
            /* Close the program if q is pressed. */
            printf ("KeyPress event %d key %d\n", event.type, ((XKeyEvent)event.xkey).keycode );
            modifyLive (dis, win, gc);
#define REG_TUNE 0xE6
            if (XK_o == XLookupKeysym (&event.xkey, 0)) {
                unsigned char reg = 0;
                dev_get_regs(dev->udev, 2, REG_TUNE, &reg);
                dev_set_regs(dev->udev, 2, REG_TUNE, ++reg);
                printf("%02x: %02x\n", REG_TUNE, reg);
            }
            if (XK_l == XLookupKeysym (&event.xkey, 0)) {
                int reg;
                dev_get_regs(dev->udev, 2, REG_TUNE, &reg);
                dev_set_regs(dev->udev, 2, REG_TUNE, --reg);
                printf("%02x: %02x\n", REG_TUNE, reg);
            }
            if (XK_q == XLookupKeysym (&event.xkey, 0))
              goto finishing;
            break;
          default:
            printf ("Unknown event %d\n", event.type);
            break;
        }
#endif
    }

finishing:
  stop = 1;
  usleep(500*1000);
  if (dev != NULL && dev->udev != NULL)
    global_exit(dev);

  /* For some reason the event loop stopped before q was pressed. */
  return EXIT_FAILURE;
}
コード例 #5
0
ファイル: client.c プロジェクト: orez-/imgserver
int main(int argc, char **argv) {
  program_name = basename(argv[0]);
  snprintf(doc,DOC_BUFFER_LEN,"%s -- a simple client for COMP-535",program_name);
  struct arguments arguments;
  
  /* Default values. */
  arguments.port = -1;
  arguments.host = NULL;
  arguments.adaptive = 0;
  arguments.verbose = 0;
  arguments.silent = 0;
  arguments.batch = 0;
  arguments.devnull = 0;
  arguments.infile = NULL;
  
  argp_parse (&argp, argc, argv, 0, 0, &arguments);
  
  verbose_f = 0;
  if (arguments.verbose)
    verbose_f = 1;
  if (arguments.silent) {
    freopen("/dev/null", "w", stderr);
    freopen("/dev/null", "w", stdout);
  }
  batch_f = arguments.batch;
  adaptive_f = arguments.adaptive;
  if (arguments.infile) {
    if(freopen(arguments.infile, "r", stdin) == NULL){
      exit(1);
    }
  }
  
  sockfd = -1;
  if (adaptive_f)
    adaptivefd = -1;
  signal(SIGINT, interrupt);
  
  char foldertmp[] = "clientimgXXXXXX";
  char s[INET6_ADDRSTRLEN], *t, *line, linebuf[LINE_SIZE];
  char *filename, *filebuf;
  int cid, l; /* Return values, temp values */
  FILE *file;
  size_t filesize, read, remain, total, chunk;
  
  sockfd = connect_to_host(arguments.host, arguments.port, s, sizeof s);
  if (sockfd == -1) {
    fprintf(stderr, "failed to connect to server.\n");
    global_exit(1);
  }
  fprintf(stdout, "Connected to %s\n", s);
  
  buffer = ALLOC(readbuffer);
  buffer->used = 0;
  
  // HELLO YES THIS IS SERVER
  line = recvline();
  t = strtok(line,DELIM);
  if (strcmp(t, "HELLO") != 0){
    fprintf(stderr,"Unexpected message from server: %s\nExiting.\n", line);
    global_exit(0);
  }
  t = strtok(NULL,DELIM);
  cid = (int)strtol(t,NULL,0);
  if (errno == ERANGE) { /* Not a number? or number too big? */
    fprintf(stderr, "Invalid client ID from server. Exiting");
    global_exit(1);
  }
  fprintf(stdout, "Got client ID: %d\n", cid);
  if (adaptive_f) {
    if ((t = strtok(NULL,DELIM)) == NULL){
      fprintf(stderr, "Server is not in adaptive mode. Exiting\n");
      global_exit(1);
    }
    errno = 0;
    l = (int)strtol(t,NULL,0);
    if (errno == ERANGE) { /* Not a number? or number too big? */
      fprintf(stderr, "Invalid port number from server. Exiting");
      global_exit(1);
    }
    adaptivefd = connect_to_host(arguments.host, l, NULL, 0);
    if (adaptivefd == -1) {
      fprintf(stderr, "failed to connect to adaptive server.\n");
      global_exit(2);
    }
    snprintf(linebuf, LINE_SIZE, "%d\n", cid);
    if (send(adaptivefd, linebuf, strlen(linebuf), 0) == -1){
      perror("send");
      global_exit(1);
    }
  }
  efree(line);
  if (!arguments.devnull){
    mkdtemp(foldertmp);
    fprintf(stdout, "Storing downloaded images in directory %s.\n", foldertmp);
  }
  
  for (;;) {
#ifdef HAS_GNUREADLINE
    if(snreadline(linebuf, LINE_SIZE, "GET> ") == NULL){
#else
    if (!batch_f)
      fprintf(stderr, "GET> ");
    if(fgets(linebuf, LINE_SIZE, stdin) == NULL){
#endif
      printf("exit\n");
      global_exit(0);
    }
#ifndef HAS_GNUREADLINE
    trim_in_place(linebuf);
#endif
    if (strlen(linebuf) == 0) continue;
    /* Hacky hack to transmit panning speed, any 2 or less digit
       number is considered a pan speed */
    if (adaptive_f && strlen(linebuf) < 3 && is_number(linebuf)) {
      add_newline(linebuf, LINE_SIZE);
      if (send(adaptivefd, linebuf, strlen(linebuf), 0) == -1){
        perror("send");
        global_exit(1);
      }
      continue;
    }
    if (add_newline(linebuf, LINE_SIZE)) {
      fprintf(stderr, "Command too long.\n");
      continue;
    }
    if (send(sockfd, linebuf, strlen(linebuf), 0) == -1){
      perror("send");
      global_exit(1);
    }
    remove_newline(linebuf);
    line = recvline();
    t = strtok(line,DELIM);
    if (strcmp(t, "ERROR") == 0){
      t = strtok(NULL,DELIM);
      fprintf(stderr, "Server> Error: %s\n", t);
    } else if (strcmp(t, "FILE") == 0) {
      t = strtok(NULL,DELIM);
      errno = 0;
      size_t filesize = (size_t)strtol(t,NULL,0);
      if (errno == ERANGE) {
        fprintf(stderr,"Fatal Error: Could not parse file size.\n", t);
        global_exit(0);
      }
      if (!arguments.devnull) {
        l = snprintf(NULL, 0, "%s/%s", foldertmp, linebuf);
        filename = (char *)emalloc(l+1);
        snprintf(filename, l+1, "%s/%s", foldertmp, linebuf);
      } else {
        filename = (char *)emalloc(10);
        snprintf(filename, 10, "/dev/null");
      }
      file = fopen(filename,"wb");
      efree(filename);
      chunk = (filesize > MAX_FILE_BUFFER) ? MAX_FILE_BUFFER : filesize;
      filebuf = (char *)emalloc(chunk);
      remain = filesize;
      total = 0;
      if (buffer->used > 0){
        fwrite(buffer->data, 1, buffer->used, file);
        total += buffer->used;
        remain -= buffer->used;
        buffer->used = 0;
      }
      fprintf(stderr,"'%s' [%ld/%ld] (%ld%%)", linebuf, (long)total, (long)filesize, (long)(100*total)/filesize);
      while (remain > 0) {
        read = recv(sockfd, filebuf, chunk, 0);
        if (read == -1) {
          perror("recv");
          global_exit(3);
        } else if (read == 0) {
          fprintf(stderr,"Server dropped connection.\n");
          global_exit(4);
        }
        total += read;
        remain -= read;
        fprintf(stderr,"%c[2K\r", 27);
        fprintf(stderr,"'%s' [%ld/%ld] (%ld%%)", linebuf, (long)total, (long)filesize, (long)(100*total)/filesize);
        fwrite(filebuf, 1, read, file);
      }
      fprintf(stderr,"%c[2K\r", 27);
      printf("'%s' saved. [%ld/%ld]\n", linebuf, (long)total, (long)filesize);
      fclose(file);
      efree(filebuf);
    } else {
      verbose("Ignoring unexpected message from server: %s\n", t);
    }
    efree(line);
  }
}