The DejaVU Framework -- hush 3.0
[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?

source: clockserver.c hush-3.0b4/auxiliary/net/examples/cs/cs-clock


[.] - [up] [top] - index make source scripts configure
  // clockserver.cc
  // waits for a connect and then sends a variable number of messages per second 
  // to a client.
  
  include <net/cs/tcp/server.h>
  include <net/cs/dataconn.h>
  
  include <signal.h>
  include <stdio.h>
  include <string.h>
  include <unistd.h>
  include <sys/time.h>
  include <stream.h>
  
  tcp_server* server = NULL;
  int timing_counter = 0;
  int timer_speed = 120;
  
  //-------------------------------------------------------------------------
  
  void getalarm(int /* signr */)
  {
      static int busy = 0;
      char buf[16];
  
      busy++;
      timing_counter++;
  
      if (busy > 1)
          return;
  
      if (timer_speed <= 0)
          return;
  
      do
      {
          sprintf(buf, "%06d", timing_counter);
          server -> broadcastmsg(buf, strlen(buf));
          busy--;
      }
      while (busy > 0);
  }
  
  void set_timer_speed(int bpm)
  {
      struct itimerval t1;
      float dt;
   
      printf("bpm = %d\n", bpm);
      timer_speed = bpm;
  
      if (timer_speed <= 0)
          return;
  
      dt = (60.0 / (float)bpm) / 24.0;
  
      printf("dt = %f\n", dt);
  
      t1.it_interval.tv_sec = (int)dt;
      t1.it_interval.tv_usec = ((int)(dt * 1000000.0)) % 1000000;
  
      t1.it_value.tv_sec = (int)dt;
      t1.it_value.tv_usec = ((int)(dt * 1000000.0)) % 1000000;
  ifdef i386
      t1.it_interval.tv_usec -= 100;
  endif
   
      printf("setting timer at %ld s, %ld us\n", t1.it_interval.tv_sec, 
             t1.it_interval.tv_usec);
  
      setitimer(ITIMER_REAL, &t1, 0);
  }
  
  void install_timer(int bpm)
  {
      struct sigaction a;
  
      signal(SIGALRM, getalarm);
      sigaction(SIGALRM, 0, &a);
  ifdef i386
      a.sa_flags &= ~SA_ONESHOT;
      a.sa_flags |= SA_RESTART;
  else
      a.sa_flags &= ~SA_RESETHAND;
      a.sa_flags |= SA_RESTART;
  endif
      sigaction(SIGALRM, &a, 0);
      set_timer_speed(bpm);
  }
  
  int main()
  {
      char buf[128];
  
      setvbuf(stdout, NULL, _IONBF, 0);       // unbuffer stdout
      server = new tcp_server();
      cout << "Server is at port " << server -> portnr() << endl;
      printf("server is at port %d\n", server -> portnr());
      install_timer(timer_speed);
  
      while (1)
      {
          data_connection* dataconn = server -> select();
          if (dataconn != NULL)
          {
              if (server -> read(dataconn, buf, sizeof(buf)) > 0)
                  printf("unexpected message: %s\n", buf);
              else
                  printf("client has closed connection\n");
          }
      }
  }
  

[.] Papers Tutorials Examples Manuals Interfaces Sources Packages Resources ?
Hush Online Technology
hush@cs.vu.nl
09/09/98