## modules/mi/mirror.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. pi_controller_new
2. pi_controller_process
3. producer_run
4. MI_init
5. main

    /***************************************
      $Revision: 1.1 $

      Mirror module (mi).

      Status: NOT REVUED, NOT TESTED

      ******************/ /******************
      Filename            : access_control.c
      Author              : ottrey@ripe.net
      OSs Tested          : Solaris
      ******************/ /******************
      Copyright (c) 1999                              RIPE NCC

      All Rights Reserved

      Permission to use, copy, modify, and distribute this software and its
      documentation for any purpose and without fee is hereby granted,
      provided that the above copyright notice appear in all copies and that
      both that copyright notice and this permission notice appear in
      supporting documentation, and that the name of the author not be
      used in advertising or publicity pertaining to distribution of the
      software without specific, written prior permission.

      THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      ***************************************/
    #include <stdio.h>
    #include <sys/timeb.h>
    #include <glib.h>
    #include "thread.h"

    /*+ String sizes +*/
    #define STR_S   63
    #define STR_M   255
    #define STR_L   1023
    #define STR_XL  4095
    #define STR_XXL 16383

    #define BUNDLE_SIZE 10

    /*
      It's been a while, but this is an attempt to implement a Proportional Integral (PI)
      controller to control the sleep time between serial requests, so that the serial
      requests request for BUNDLE_SIZE (which is assumed to be the optimal size).

      A PI controller has the equation:

        CO(t) = Kp*e(t) + Ki*sum(e(t)*dt);

      where:

      CO(t) = controller output at time t,
      Kp = proportional constant
      e(t) = error signal at time t
      Ki = integral constant
    */
    typedef struct _pi_controller_t {
      int Kp;
      int e_integral;
      int Ki;
      struct timeb *t;
    } pi_controller_t;

    static pi_controller_t * pi_controller_new(int Kp, int Ki) {
    /* [<][>][^][v][top][bottom][index][help] */
      pi_controller_t *pic;

      pic = (pi_controller_t *)calloc(1, sizeof(pi_controller_t));
      pic->Kp = Kp;
      pic->Ki = Ki;
      pic->t=(struct timeb *)calloc(1, sizeof(struct timeb)+1);
      ftime(pic->t);

      return pic;
    } /* pi_controller_new() */

    static int pi_controller_process(pi_controller_t *pic, int error) {
    /* [<][>][^][v][top][bottom][index][help] */
      int result;
      int dt;
      struct timeb *now;
      int secs;
      int msecs;

      now=(struct timeb *)calloc(1, sizeof(struct timeb)+1);
      ftime(now);

      secs = (int)(now->time - pic->t->time);
      msecs = (int)(now->millitm - pic->t->millitm);
      if (msecs < 0) {
        msecs += 1000;
        secs--;
      }
      dt = secs*1000 + msecs;

      free(pic->t);
      pic->t = now;

      /*
        CO(t) = Kp*e(t) + Ki*sum(e(t)*dt);
      */
      pic->e_integral += error*dt;
      result = (pic->Kp)*error + (pic->Ki)*(pic->e_integral);

      return result;
    } /* pi_controller_process() */

    static producer_run(void) {
    /* [<][>][^][v][top][bottom][index][help] */
      FILE *f;
      pi_controller_t *pic;
      int serial_curr=0;
      int serial_prev=0;
      int i;
      int error;
      int sleep_time;

      printf("Producer thread started.\n");

      pic = pi_controller_new(100, 1);

      for (i=0; i< 30; i++) {
        error = BUNDLE_SIZE - (serial_curr - serial_prev);
        sleep_time = pi_controller_process(pic, error);
        if (sleep_time < 0) {
          sleep_time = 0 - sleep_time;
        }
        printf("Sleeping for %d usecs.\n", sleep_time);
        usleep(sleep_time);

        serial_prev = serial_curr;

        f = fopen("/ncc/db2/serials/current/RIPE.CURRENTSERIAL", "r");
        fscanf(f, "%d", &serial_curr);
        fclose(f);

        printf("%d\n", serial_curr);
      }
      pthread_exit((void *)0);
    } /* producer_run() */

    /* MI_init() */
    /*++++++++++++++++++++++++++++++++++++++
      Initialize the mirroring.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    void MI_init() {
    /* [<][>][^][v][top][bottom][index][help] */
      pthread_t tid;
      pthread_attr_t attr;

      printf("Initialising mirroring.\n");

      pthread_attr_init(&attr);     /* initialize attr with default attributes */
      pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      pthread_create(&tid, &attr, (void *(*)(void *))producer_run, NULL);
    } /* MI_init() */

    int main(void) {
    /* [<][>][^][v][top][bottom][index][help] */
      MI_init();
      sleep(1000);
      return 0;
    }

/* [<][>][^][v][top][bottom][index][help] */