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-hush


[.] - [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 <hush/session.h>
  include <hush/event.h>
  include <hush/handler.h>
  include <hush/kit.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;
  
  //---------------------------- definitions ----------------------
  
  

datahandler:


  class datahandler: public handler
  {
  public:
      int operator()();
  };

slide: datahandler:

  
  

app:


  class app: public session
  {
  public:
      app(int argc, char* argv[]);
      int main();
  
      int operator()();
  
  protected:
      datahandler* h;
  };

slide: app:

  
  //--------------------------- implementations --------------------
  
  //----------------------- misc functions ----------------------
  
  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);
  }
  
  //------------ app -----------
  
  app::app(int argc, char* argv[])
     :session(argc, argv)
  {
  }
  
  int app::main()
  {
      server = new tcp_server();
      cout << "Server is at port " << server -> portnr() << endl;
      server -> bind(this);
      h = new datahandler();
      install_timer(timer_speed);
  
      return OK;
  }
  
  int app::operator()()
  {
      cout << "new connection..." << endl;
      data_connection* dataconn = server -> newconnection();
      if (dataconn != NULL)
          thekit() -> bind(dataconn -> fd(), h);
      cout << "    made" << endl;
  
      return OK;
  }
  
  //------------ datahandler -----------
  
  int datahandler::operator()()
  {
      char buf[128];
  
      cout << "datahandler::operator()()" << endl;
  
      data_connection* dataconn = server -> conn(_event -> fd());
  
      if (dataconn == NULL)
      {
          cerr << "datahandler::operator() : connection = NULL" << endl;
          thekit() -> unbind(_event -> fd());
          return -1;
      }
      else
      {
          if (server -> read(dataconn, buf, sizeof(buf)) > 0)
              cout << "unexpected message: " << buf << endl;
          else
          {
              cout << "client has closed connection" << endl;
              thekit() -> unbind(_event -> fd());
          }
      }
  
      return OK;
  }
  
  //------------------- main --------------
  
  int main(int argc, char* argv[])
  {
      app a(argc, argv);
      a.run();
  
      return 0;
  }
  

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