]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/trigger.c
ovsdb: Refactor jsonrpc-server to make the concept of a session public.
[mirror_ovs.git] / ovsdb / trigger.c
CommitLineData
e317253b 1/* Copyright (c) 2009, 2010, 2011 Nicira Networks
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 29static bool ovsdb_trigger_try(struct ovsdb_trigger *, long long int now);
f85f8ebb
BP
30static void ovsdb_trigger_complete(struct ovsdb_trigger *);
31
32void
e317253b
BP
33ovsdb_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
46void
47ovsdb_trigger_destroy(struct ovsdb_trigger *trigger)
48{
49 list_remove(&trigger->node);
50 json_destroy(trigger->request);
51 json_destroy(trigger->result);
52}
53
54bool
55ovsdb_trigger_is_complete(const struct ovsdb_trigger *trigger)
56{
57 return trigger->result != NULL;
58}
59
60struct json *
61ovsdb_trigger_steal_result(struct ovsdb_trigger *trigger)
62{
63 struct json *result = trigger->result;
64 trigger->result = NULL;
65 return result;
66}
67
68void
69ovsdb_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
83void
84ovsdb_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
110static bool
e317253b 111ovsdb_trigger_try(struct ovsdb_trigger *t, long long int now)
f85f8ebb 112{
e317253b
BP
113 t->result = ovsdb_execute(t->session->db, t->request,
114 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
123static void
124ovsdb_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}