b.a.t.m.a.n.lists.open-mesh.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Haws <jhaws@sdl.usu.edu>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Jonathan Haws <jhaws@sdl.usu.edu>
Subject: [B.A.T.M.A.N.] [PATCH 2/2] alfred: Adding time_subtract utility function
Date: Thu, 25 Aug 2016 14:39:56 -0600	[thread overview]
Message-ID: <1472157596-19774-2-git-send-email-jhaws@sdl.usu.edu> (raw)
In-Reply-To: <1472157596-19774-1-git-send-email-jhaws@sdl.usu.edu>

Signed-off-by: Jonathan Haws <jhaws@sdl.usu.edu>

Added time_subtract() utility function to use for simply subtracting
one timespec from another.
---
 alfred.h |  2 ++
 server.c |  5 ++++-
 util.c   | 13 +++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/alfred.h b/alfred.h
index 5b7e965..194c62a 100644
--- a/alfred.h
+++ b/alfred.h
@@ -196,6 +196,8 @@ int netsock_own_address(const struct globals *globals,
 /* util.c */
 int time_diff(struct timespec *tv1, struct timespec *tv2,
 	      struct timespec *tvdiff);
+void time_subtract(struct timespec *tv1, struct timespec *tv2,
+	      struct timespec *tvout);
 void time_random_seed(void);
 uint16_t get_random_id(void);
 bool is_valid_ether_addr(uint8_t *addr);
diff --git a/server.c b/server.c
index c5df945..884a1a7 100644
--- a/server.c
+++ b/server.c
@@ -372,7 +372,10 @@ int alfred_server(struct globals *globals)
 
 	while (1) {
 		clock_gettime(CLOCK_MONOTONIC, &now);
-		time_diff(&now, &globals->sync_period, &now);
+
+		/* subtract the synchronization period from the current time */
+		time_subtract(&now, &globals->sync_period, &now);
+
 		if (!time_diff(&last_check, &now, &tv)) {
 			tv.tv_sec = 0;
 			tv.tv_nsec = 0;
diff --git a/util.c b/util.c
index c7e11cc..1b67f78 100644
--- a/util.c
+++ b/util.c
@@ -41,6 +41,19 @@ int time_diff(struct timespec *tv1, struct timespec *tv2,
 	return (tvdiff->tv_sec >= 0);
 }
 
+void time_subtract(struct timespec *tv1, struct timespec *tv2,
+	      struct timespec *tvout) {
+	tvout->tv_sec = tv1->tv_sec - tv2->tv_sec;
+	if (tv1->tv_nsec < tv2->tv_nsec) {
+		tvout->tv_nsec = 1000000000 + tv1->tv_nsec - tv2->tv_nsec;
+		tvout->tv_sec -= 1;
+	} else {
+		tvout->tv_nsec = tv1->tv_nsec - tv2->tv_nsec;
+	}
+
+	return;
+}
+
 void time_random_seed(void)
 {
 	struct timespec now;
-- 
2.7.4


  reply	other threads:[~2016-08-25 20:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-25 20:39 [B.A.T.M.A.N.] [PATCH 1/2] alfred: Externalized synchronization interval Jonathan Haws
2016-08-25 20:39 ` Jonathan Haws [this message]
2016-08-26  6:45   ` [B.A.T.M.A.N.] [PATCH 2/2] alfred: Adding time_subtract utility function Sven Eckelmann
2016-08-26 15:02     ` Jonathan Haws
2016-08-28  7:17       ` Sven Eckelmann
2016-09-06  7:39       ` Simon Wunderlich
  -- strict thread matches above, loose matches on Subject: below --
2016-08-25 20:30 [B.A.T.M.A.N.] [PATCH 1/2] alfred: Externalized synchronization interval Jonathan Haws
2016-08-25 20:30 ` [B.A.T.M.A.N.] [PATCH 2/2] alfred: Adding time_subtract utility function Jonathan Haws
2016-08-25 20:37   ` Jonathan Haws

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1472157596-19774-2-git-send-email-jhaws@sdl.usu.edu \
    --to=jhaws@sdl.usu.edu \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).