File: schedule.c

package info (click to toggle)
trickle 1.08%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 588 kB
  • sloc: ansic: 4,923; makefile: 46
file content (134 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * schedule.c
 *
 * Copyright (c) 2009-2015 Matthew Blythe <mblythester@gmail.com>
 * All rights reserved.
 *
 */

#include <sys/types.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#if defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME)
#include <time.h>
#endif /* defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME) */
#include <stdlib.h>

#include "trickle.h"
#include "schedule.h"

void
schedString(char* sched, uint* bwList, const char* updown, print_func print)
{
	int i,j;
	int day[8];
	char* place=NULL;
	uint defaultBw = atoi(sched)*1024;
	print(1,"Default %s Bandwidth: %u bps",updown,defaultBw);
	for(i=0; i<(7*24*DIVS_PER_HOUR); i++){
		bwList[i] = defaultBw;
	}
	while(1){
		for(i=0;i<7;i++)
			day[i]=0;
		day[7]=1;
		while(sched[0]!='\0' && sched[0]!=':')
			sched++;
		if(sched[0]=='\0')
			return;
		sched++;
		while(place != sched){
			place = sched;
			if(sched[0]=='S' && sched[1]=='u'){
				day[0]=1;
				day[7]=0;
				sched += 2;
			}
			if(sched[0]=='M'){
				day[1]=1;
				day[7]=0;
				sched++;
			}
			if(sched[0]=='T'){
				day[2]=1;
				day[7]=0;
				sched++;
			}
			if(sched[0]=='W'){
				day[3]=1;
				day[7]=0;
				sched++;
			}
			if(sched[0]=='T' && sched[1]=='h'){
				day[4]=1;
				day[7]=0;
				sched += 2;
			}
			if(sched[0]=='F'){
				day[5]=1;
				day[7]=0;
				sched++;
			}
			if(sched[0]=='S' && sched[1]=='a'){
				day[6]=1;
				day[7]=0;
				sched += 2;
			}
		}
		uint time1,hr1,div1,time2,hr2,div2,bw;
		time1 = atoi(sched);
		hr1 = (time1/100)%24;
		div1 = ((time1%100)/(60/DIVS_PER_HOUR))%DIVS_PER_HOUR;
		while(sched[0]!='\0' && sched[0]!=',')
			sched++;
		if(sched[0]=='\0')
			return;
		sched++;
		time2 = atoi(sched);
		hr2 = (time2/100)%24;
		div2 = ((time2%100)/(60/DIVS_PER_HOUR))%DIVS_PER_HOUR;
		while(sched[0]!='\0' && sched[0]!=',')
			sched++;
		if(sched[0]=='\0')
			return;
		sched++;
		bw = atoi(sched);
		bw *= 1024;
		for(i=0; i<7; i++){
			if(day[i]||day[7]){
				 print(1,"changing %s bandwidth on day %u, between %u:%02u and %u:%02u to %u bps",
						 updown, i, hr1, div1*(60/DIVS_PER_HOUR), hr2, div2*(60/DIVS_PER_HOUR), bw);
				 for(j = i*(24*DIVS_PER_HOUR) + hr1*DIVS_PER_HOUR + div1;
						 j < i*(24*DIVS_PER_HOUR) + hr2*DIVS_PER_HOUR + div2; j++){
					 bwList[j] = bw;
				 }
			}
		}
	}
}

uint
getSchedIndex()
{
	time_t rawtime;
	struct tm * timeinfo;
	uint index;

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	index = (timeinfo->tm_wday*24*DIVS_PER_HOUR)
		+ (timeinfo->tm_hour*DIVS_PER_HOUR)
		+ (timeinfo->tm_min/(60/DIVS_PER_HOUR));

	return index;
}