]>
Commit | Line | Data |
---|---|---|
e0edde6f | 1 | /* Copyright (c) 2009, 2010, 2011 Nicira, Inc. |
f85f8ebb BP |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. | |
14 | */ | |
15 | ||
16 | #include <config.h> | |
17 | ||
18 | #include "trigger.h" | |
19 | ||
20 | #include <assert.h> | |
21 | #include <limits.h> | |
22 | ||
23 | #include "json.h" | |
24 | #include "jsonrpc.h" | |
25 | #include "ovsdb.h" | |
26 | #include "poll-loop.h" | |
e317253b | 27 | #include "server.h" |
f85f8ebb | 28 | |
e317253b | 29 | static bool ovsdb_trigger_try(struct ovsdb_trigger *, long long int now); |
f85f8ebb BP |
30 | static void ovsdb_trigger_complete(struct ovsdb_trigger *); |
31 | ||
32 | void | |
e317253b BP |
33 | ovsdb_trigger_init(struct ovsdb_session *session, |
34 | struct ovsdb_trigger *trigger, | |
35 | struct json *request, long long int now) | |
f85f8ebb | 36 | { |
e317253b BP |
37 | trigger->session = session; |
38 | list_push_back(&trigger->session->db->triggers, &trigger->node); | |
f85f8ebb BP |
39 | trigger->request = request; |
40 | trigger->result = NULL; | |
41 | trigger->created = now; | |
42 | trigger->timeout_msec = LLONG_MAX; | |
e317253b | 43 | ovsdb_trigger_try(trigger, now); |
f85f8ebb BP |
44 | } |
45 | ||
46 | void | |
47 | ovsdb_trigger_destroy(struct ovsdb_trigger *trigger) | |
48 | { | |
49 | list_remove(&trigger->node); | |
50 | json_destroy(trigger->request); | |
51 | json_destroy(trigger->result); | |
52 | } | |
53 | ||
54 | bool | |
55 | ovsdb_trigger_is_complete(const struct ovsdb_trigger *trigger) | |
56 | { | |
57 | return trigger->result != NULL; | |
58 | } | |
59 | ||
60 | struct json * | |
61 | ovsdb_trigger_steal_result(struct ovsdb_trigger *trigger) | |
62 | { | |
63 | struct json *result = trigger->result; | |
64 | trigger->result = NULL; | |
65 | return result; | |
66 | } | |
67 | ||
68 | void | |
69 | ovsdb_trigger_run(struct ovsdb *db, long long int now) | |
70 | { | |
71 | struct ovsdb_trigger *t, *next; | |
72 | bool run_triggers; | |
73 | ||
74 | run_triggers = db->run_triggers; | |
75 | db->run_triggers = false; | |
4e8e4213 | 76 | LIST_FOR_EACH_SAFE (t, next, node, &db->triggers) { |
f85f8ebb | 77 | if (run_triggers || now - t->created >= t->timeout_msec) { |
e317253b | 78 | ovsdb_trigger_try(t, now); |
f85f8ebb BP |
79 | } |
80 | } | |
81 | } | |
82 | ||
83 | void | |
84 | ovsdb_trigger_wait(struct ovsdb *db, long long int now) | |
85 | { | |
86 | if (db->run_triggers) { | |
87 | poll_immediate_wake(); | |
88 | } else { | |
89 | long long int deadline = LLONG_MAX; | |
90 | struct ovsdb_trigger *t; | |
91 | ||
4e8e4213 | 92 | LIST_FOR_EACH (t, node, &db->triggers) { |
f85f8ebb BP |
93 | if (t->created < LLONG_MAX - t->timeout_msec) { |
94 | long long int t_deadline = t->created + t->timeout_msec; | |
95 | if (deadline > t_deadline) { | |
96 | deadline = t_deadline; | |
97 | if (now >= deadline) { | |
98 | break; | |
99 | } | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | if (deadline < LLONG_MAX) { | |
7cf8b266 | 105 | poll_timer_wait_until(deadline); |
f85f8ebb BP |
106 | } |
107 | } | |
108 | } | |
109 | ||
110 | static bool | |
e317253b | 111 | ovsdb_trigger_try(struct ovsdb_trigger *t, long long int now) |
f85f8ebb | 112 | { |
da897f41 BP |
113 | t->result = ovsdb_execute(t->session->db, t->session, |
114 | t->request, now - t->created, &t->timeout_msec); | |
f85f8ebb BP |
115 | if (t->result) { |
116 | ovsdb_trigger_complete(t); | |
117 | return true; | |
118 | } else { | |
119 | return false; | |
120 | } | |
121 | } | |
122 | ||
123 | static void | |
124 | ovsdb_trigger_complete(struct ovsdb_trigger *t) | |
125 | { | |
126 | assert(t->result != NULL); | |
127 | list_remove(&t->node); | |
e317253b | 128 | list_push_back(&t->session->completions, &t->node); |
f85f8ebb | 129 | } |