-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlightmain2.c
More file actions
145 lines (128 loc) · 4.06 KB
/
Copy pathlightmain2.c
File metadata and controls
145 lines (128 loc) · 4.06 KB
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
135
136
137
138
139
140
141
142
143
144
145
/* This is an "interactive" test command.
*
* Use q|Q to quit, d|D to signal dawn, s|S to signal sunset. Press enter!
*
* The result will run through the Petri net, signal states between runs,
* and can be made to stop at yellow between dawn and sunset events. Note
* how any active run of red..green...yellow continues to yellow when dawn
* has been entered, just as the Petri net prescribes.
*
* Sorry for the horrible interaction, it's just a demo...
*
* From: Rick van Rein <rick@openfortress.nl>
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include "traffic_light_nightly.h"
#include <perpetuum/api.h>
trans_retcode_t trans_action_stop (PARMDEF_COMMA (pnc) transref_t tr, time_t *nowp, void *opt_evdata) {
if (random () & 0x00000001) {
printf ("Stop\n");
return TRANS_SUCCESS;
} else {
return TRANS_FAILURE;
}
}
trans_retcode_t trans_action_go (PARMDEF_COMMA (pnc) transref_t tr, time_t *nowp, void *opt_evdata) {
if (random () & 0x00000001) {
printf ("Go\n");
return TRANS_SUCCESS;
} else {
return TRANS_FAILURE;
}
}
trans_retcode_t trans_action_caution (PARMDEF_COMMA (pnc) transref_t tr, time_t *nowp, void *opt_evdata) {
if (random () & 0x00000001) {
printf ("Caution\n");
return TRANS_SUCCESS;
} else {
return TRANS_FAILURE;
}
}
transref_t trlist_dawn [] = { 1, TRANS_INDEX_dawn };
trans_retcode_t trans_action_dawn (PARMDEF_COMMA (pnc) transref_t tr, time_t *nowp, void *opt_evdata) {
if (opt_evdata != NULL) {
printf ("Event-triggered Dawn\n");
return TRANS_SUCCESS;
} else {
return TRANS_FAILURE;
}
}
transref_t trlist_sunset [] = { 1, TRANS_INDEX_sunset };
trans_retcode_t trans_action_sunset (PARMDEF_COMMA (pnc) transref_t tr, time_t *nowp, void *opt_evdata) {
if (opt_evdata != NULL) {
printf ("Event-triggered Sunset\n");
return TRANS_SUCCESS;
} else {
return TRANS_FAILURE;
}
}
int main (int argc, char *argv []) {
// flat_schedule_run (PARMARG (&the_traffic_light_nightly));
int pr;
int quit = 0;
char ch;
int flags = fcntl (0, F_GETFL);
if (flags < 0) {
perror ("Failed to fcntl (F_GETFL)\n");
exit (1);
}
flags |= O_NONBLOCK;
if (fcntl (0, F_SETFL, flags) < 0) {
perror ("Failed to fcntl (F_SETFL)\n");
exit (1);
}
printf ("Enter q|Q to quit, d|D to signal dawn, and s|S to signal sunset...\n");
printf ("Prior to scheduler with %s=%d, %s=%d, %s=%d\n",
the_traffic_light_nightly.topology.place_ary [1].name,
the_traffic_light_nightly. place_ary [1].available,
the_traffic_light_nightly.topology.place_ary [2].name,
the_traffic_light_nightly. place_ary [2].available,
the_traffic_light_nightly.topology.place_ary [3].name,
the_traffic_light_nightly. place_ary [3].available);
sleep (3);
fflush (stdout);
while (!quit) {
// int i = 10;
// while (i-- > 0) {
// for (pr=1; pr<=3; pr++) {
// for (pr=3; pr>=1; pr--) {
// printf ("Injecting a token into place %d\n", pr);
// inject_tokens (PARMARG_COMMA (&the_traffic_light_nightly) pr, 1);
printf ("Entering scheduler with %s=%d, %s=%d, %s=%d, %s=%d\n",
REF2PLACE_TOPO(&the_traffic_light_nightly,1).name,
REF2PLACE (&the_traffic_light_nightly,1).available,
REF2PLACE_TOPO(&the_traffic_light_nightly,2).name,
REF2PLACE (&the_traffic_light_nightly,2).available,
REF2PLACE_TOPO(&the_traffic_light_nightly,3).name,
REF2PLACE (&the_traffic_light_nightly,3).available,
REF2PLACE_TOPO(&the_traffic_light_nightly,4).name,
REF2PLACE (&the_traffic_light_nightly,4).available);
fflush (stdout);
flat_schedule_run (PARMARG (&the_traffic_light_nightly));
sleep (1);
while (read (0, &ch, 1) == 1) {
switch (ch) {
case 'd':
case 'D':
printf ("Processing dawn event\n");
process_event (PARMARG_COMMA (&the_traffic_light_nightly) trlist_dawn, "DAWN-non-NULL");
break;
case 's':
case 'S':
printf ("Processing sunset event\n");
process_event (PARMARG_COMMA (&the_traffic_light_nightly) trlist_sunset, "SUNSET-non-NULL");
break;
case 'q':
case 'Q':
quit = 1;
break;
default:
break;
}
}
}
exit (0);
}