/*
 * timer_sample.h
 *
 * Copyright (C) 2005, Ranch Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
 *
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "voipTimerP.h"

timer_task_context_t timer_context;

typedef struct _some_struct
{
  int id;
  timer_id_t timer_handle;
} some_struct;

static int tickHandler(void *argptr, size_t arglen);
static int setupTick(timer_task_context_t *context, void *data, unsigned int timeout, timer_id_t *handleP);

static int tickHandler(void *argptr, size_t arglen)
{
  printf("Destroying %d\n", ((some_struct*)argptr)->id);
  fflush(stdout);
  return 0;
}

static int setupTick(timer_task_context_t *context, void *data, unsigned int timeout, timer_id_t *handleP)
{
  sysTimer_t tickTimer;
  memset(&tickTimer, 0, sizeof(tickTimer));
  tickTimer.initialInterval = timeout*1000;
  tickTimer.context = data;
  tickTimer.contextSize = sizeof(data);
  tickTimer.handler = tickHandler;
  tickTimer.cleanup = 0;
  if ((*handleP = sysSetTimer(context, &tickTimer)))
    return 0;
  printf("sysSetTimer failed for %d\n", ((some_struct*)data)->id);
  return -1;
}

int main(int argc, char **argv)
{
  int i;
  some_struct mystruct[100];
  timer_context.task_name = "TIMER";
  timer_context.task_priority = 95;
  timer_context.task_stack_size = 10*1024;
  sysInstallTimerManager(1000, &timer_context);
  for(i = 0; i < 100; i++)
    {
      mystruct[i].id = i;
      printf("Creating %d\n", mystruct[i].id);
      setupTick(&timer_context, (void*)&mystruct[i], 101-i, &(mystruct[i].timer_handle));
    }
  sleep(200);
  sysUninstallTimerManager(&timer_context);
  return 0;
}
