1290001Sglebius/* ntpsim.h
2290001Sglebius *
3290001Sglebius * The header file for the ntp discrete event simulator.
4290001Sglebius *
5290001Sglebius * Written By:	Sachin Kamboj
6290001Sglebius *		University of Delaware
7290001Sglebius *		Newark, DE 19711
8290001Sglebius * Copyright (c) 2006
9132451Sroberto */
10132451Sroberto
11290001Sglebius#ifndef NTPSIM_H
12290001Sglebius#define NTPSIM_H
13132451Sroberto
14132451Sroberto#include <stdio.h>
15132451Sroberto#include <math.h>
16290001Sglebius#ifdef HAVE_SYS_SOCKET_H
17132451Sroberto#include <sys/socket.h>
18290001Sglebius#endif
19132451Sroberto#include <arpa/inet.h>
20132451Sroberto#include "ntp_syslog.h"
21132451Sroberto#include "ntp_fp.h"
22132451Sroberto#include "ntp.h"
23132451Sroberto#include "ntp_select.h"
24132451Sroberto#include "ntp_malloc.h"
25132451Sroberto#include "ntp_refclock.h"
26132451Sroberto#include "recvbuff.h"
27132451Sroberto#include "ntp_io.h"
28132451Sroberto#include "ntp_stdlib.h"
29290001Sglebius#include "ntp_prio_q.h"
30132451Sroberto
31290001Sglebius/* CONSTANTS */
32132451Sroberto
33290001Sglebius#ifdef PI
34290001Sglebius# undef PI
35290001Sglebius#endif
36290001Sglebius#define PI 3.1415926535         /* The world's most famous constant */
37290001Sglebius#define SIM_TIME 86400		/* end simulation time */
38290001Sglebius#define NET_DLY .001            /* network delay */
39290001Sglebius#define PROC_DLY .001		/* processing delay */
40290001Sglebius#define BEEP_DLY 3600           /* beep interval (s) */
41290001Sglebius
42290001Sglebius
43290001Sglebius/* Discrete Event Queue
44290001Sglebius * --------------------
45290001Sglebius * The NTP simulator is a discrete event simulator.
46290001Sglebius *
47290001Sglebius * Central to this simulator is an event queue which is a priority queue
48290001Sglebius * in which the "priority" is given by the time of arrival of the event.
49290001Sglebius *
50290001Sglebius * A discrete set of events can happen and are stored in the queue to arrive
51290001Sglebius * at a particular time.
52132451Sroberto */
53290001Sglebius
54290001Sglebius/* Possible Discrete Events */
55290001Sglebius
56132451Srobertotypedef enum {
57290001Sglebius    BEEP,          /* Event to record simulator stats */
58290001Sglebius    CLOCK,         /* Event to advance the clock to the specified time */
59290001Sglebius    TIMER,         /* Event that designates a timer interrupt. */
60290001Sglebius    PACKET         /* Event that designates arrival of a packet */
61132451Sroberto} funcTkn;
62132451Sroberto
63290001Sglebius
64290001Sglebius/* Event information */
65290001Sglebius
66132451Srobertotypedef struct {
67290001Sglebius    double time;       /* Time at which event occurred */
68290001Sglebius    funcTkn function;  /* Type of event that occured */
69290001Sglebius    union {
70290001Sglebius        struct pkt evnt_pkt;
71290001Sglebius        struct recvbuf evnt_buf;
72290001Sglebius    } buffer;          /* Other data associated with the event */
73132451Sroberto#define ntp_pkt buffer.evnt_pkt
74132451Sroberto#define rcv_buf buffer.evnt_buf
75132451Sroberto} Event;
76132451Sroberto
77132451Sroberto
78290001Sglebius/* Server Script Information */
79290001Sglebiustypedef struct script_info_tag script_info;
80290001Sglebiusstruct script_info_tag {
81290001Sglebius	script_info *	link;
82290001Sglebius	double		duration;
83290001Sglebius	double		freq_offset;
84290001Sglebius	double		wander;
85290001Sglebius	double		jitter;
86290001Sglebius	double		prop_delay;
87290001Sglebius	double		proc_delay;
88290001Sglebius};
89132451Sroberto
90290001Sglebiustypedef DECL_FIFO_ANCHOR(script_info) script_info_fifo;
91132451Sroberto
92132451Sroberto
93290001Sglebius/* Server Structures */
94132451Sroberto
95290001Sglebiustypedef struct server_info_tag server_info;
96290001Sglebiusstruct server_info_tag {
97290001Sglebius	server_info *		link;
98290001Sglebius	double			server_time;
99290001Sglebius	sockaddr_u *		addr;
100290001Sglebius	script_info_fifo *	script;
101290001Sglebius	script_info *		curr_script;
102290001Sglebius};
103132451Sroberto
104290001Sglebiustypedef DECL_FIFO_ANCHOR(server_info) server_info_fifo;
105132451Sroberto
106290001Sglebius
107290001Sglebius/* Simulation control information */
108290001Sglebius
109290001Sglebiustypedef struct Sim_Info {
110290001Sglebius    double sim_time;      /* Time in the simulation */
111290001Sglebius    double end_time;      /* Time at which simulation needs to be ended */
112290001Sglebius    double beep_delay;    /* Delay between simulation "beeps" at which
113290001Sglebius                             simulation  stats are recorded. */
114290001Sglebius    int num_of_servers;   /* Number of servers in the simulation */
115290001Sglebius    server_info *servers; /* Pointer to array of servers */
116290001Sglebius} sim_info;
117290001Sglebius
118290001Sglebius
119290001Sglebius/* Local Clock (Client) Variables */
120290001Sglebius
121290001Sglebiustypedef struct Local_Clock_Info {
122290001Sglebius    double local_time;		/* Client disciplined time */
123290001Sglebius    double adj;			/* Remaining time correction */
124290001Sglebius    double slew;		/* Correction Slew Rate */
125290001Sglebius    double last_read_time;	/* Last time the clock was read */
126290001Sglebius} local_clock_info;
127290001Sglebius
128290001Sglebiusextern local_clock_info simclock; /* Local Clock Variables */
129290001Sglebiusextern sim_info simulation;	  /* Simulation Control Variables */
130290001Sglebius
131290001Sglebius/* Function Prototypes */
132290001Sglebius
133290001Sglebiusint	 ntpsim			(int argc, char *argv[]);
134290001SglebiusEvent    *event			(double t, funcTkn f);
135290001Sglebiusvoid     sim_event_timer	(Event *e);
136290001Sglebiusint      simulate_server	(sockaddr_u *serv_addr, endpt *inter,
137290001Sglebius				 struct pkt *rpkt);
138290001Sglebiusvoid     sim_update_clocks	(Event *e);
139290001Sglebiusvoid     sim_event_recv_packet	(Event *e);
140290001Sglebiusvoid     sim_event_beep		(Event *e);
141290001Sglebiusvoid     abortsim		(char *errmsg);
142290001Sglebiusdouble	 gauss			(double, double);
143290001Sglebiusdouble	 poisson		(double, double);
144290001Sglebiusvoid     create_server_associations(void);
145290001Sglebius
146290001Sglebius#endif	/* NTPSIM_H */
147