Line data Source code
1 : /* Copyright 2017 noseglasses <shinynoseglasses@gmail.com>
2 : *
3 : * This program is free software: you can redistribute it and/or modify
4 : * it under the terms of the GNU Lesser General Public License as published by
5 : * the Free Software Foundation, either version 3 of the License, or
6 : * (at your option) any later version.
7 : *
8 : * This program is distributed in the hope that it will be useful,
9 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 : * GNU Lesser General Public License for more details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 : */
16 :
17 : #include "ppg_timeout.h"
18 : #include "ppg_debug.h"
19 : #include "detail/ppg_context_detail.h"
20 : #include "detail/ppg_global_detail.h"
21 : #include "detail/ppg_signal_detail.h"
22 : #include "detail/ppg_event_buffer_detail.h"
23 : #include "detail/ppg_pattern_matching_detail.h"
24 :
25 15 : static void ppg_on_timeout(void)
26 : {
27 15 : if(ppg_event_buffer_size() == 0) { return; }
28 :
29 : // Check if fallback is possible
30 : //
31 15 : bool action_processed
32 15 : = ppg_recurse_and_process_actions(ppg_context->current_token);
33 :
34 15 : if(action_processed) {
35 :
36 : // PPG_LOG("Fallback success\n");
37 :
38 : // Fallback was possible
39 :
40 : // If an action was processed, we consider the processing as a match
41 : //
42 5 : ppg_event_buffer_on_match_success();
43 :
44 : // Prevent the timeout signal handler from processig events
45 : //
46 5 : ppg_delete_stored_events();
47 : }
48 : else {
49 :
50 : // PPG_LOG("Trying to find match after timeout\n");
51 :
52 : // If no action fallback is possible,
53 : // we try to find a match...
54 :
55 : // In the moment of timeout, there may be an unfinished token
56 : // that is waiting for more events to come to either lead to
57 : // a match or a match failure.
58 : //
59 : // Moreover, it may be possible that there are other tokens on the
60 : // same level with lower token precedence, that would allow for a
61 : // match with respect to the current content of the event queue.
62 : //
63 : // In such a case it is necessary that we check all remaining
64 : // branches of the search tree for a pattern match.
65 : //
66 10 : ppg_pattern_matching_process_remaining_branch_options();
67 : }
68 :
69 15 : ppg_signal(PPG_On_Timeout);
70 :
71 15 : ppg_delete_stored_events();
72 :
73 15 : ppg_reset_pattern_matching_engine();
74 : }
75 :
76 889 : bool ppg_timeout_check(void)
77 : {
78 : // PPG_LOG("Checking timeout\n");
79 :
80 889 : if(!ppg_context->properties.timeout_enabled) {
81 :
82 : // PPG_LOG("Timeout disabled\n");
83 7 : return false;
84 : }
85 :
86 882 : if(ppg_event_buffer_size() == 0) {
87 : // PPG_LOG("No current token\n");
88 440 : return false;
89 : }
90 :
91 : // PPG_LOG("Chk t.out\n");
92 :
93 442 : PPG_ASSERT(ppg_context->time_manager.time);
94 442 : PPG_ASSERT(ppg_context->time_manager.time_difference);
95 442 : PPG_ASSERT(ppg_context->time_manager.compare_times);
96 :
97 : PPG_Time cur_time;
98 :
99 442 : ppg_context->time_manager.time(&cur_time);
100 :
101 : PPG_Time delta;
102 884 : ppg_context->time_manager.time_difference(
103 442 : ppg_context->time_last_event,
104 : cur_time,
105 : &delta);
106 :
107 442 : bool timeout_hit = false;
108 :
109 : // PPG_LOG("\tdelta: %ld\n", delta);
110 :
111 442 : if( (ppg_event_buffer_size() != 0)
112 884 : && (ppg_context->time_manager.compare_times(
113 : delta,
114 442 : ppg_context->event_timeout
115 : ) > 0)
116 : ) {
117 :
118 15 : PPG_LOG("T.out hit\n");
119 :
120 15 : PPG_LOG("Events in queue: %d\n",
121 : ppg_event_buffer_size());
122 :
123 : // Too late...
124 : //
125 15 : ppg_on_timeout();
126 :
127 15 : timeout_hit = true;
128 : }
129 :
130 442 : return timeout_hit;
131 : }
|