From a0fa5e580e1b51e89061dde649a4e5d81f74d0d9 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Thu, 20 Mar 2014 23:37:06 +0000
Subject: automatically select best mode


diff --git a/switchctl.c b/switchctl.c
index d18883b..65f439c 100644
--- a/switchctl.c
+++ b/switchctl.c
@@ -487,7 +487,7 @@ int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt, c
   return 0;
 }
 
-void process_cmd_heartbeat(const char* param, int fd, client_t* client_lst)
+int process_cmd_heartbeat(const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt)
 {
   if(param) {
     client_t* client = client_find(client_lst, fd);
@@ -501,6 +501,28 @@ void process_cmd_heartbeat(const char* param, int fd, client_t* client_lst)
           break;
         }
       }
+      switchctl_mode_t old_mode = state_.mode_;
+      if(state_.mode_ == MODE_MASTER) {
+        if(!state_.hb_state_master_) {
+          if(state_.hb_state_standby_) {
+            state_.mode_ = MODE_STANDBY;
+            return change_mode(old_mode, fd, cmd_q, opt, client_lst);
+          } else {
+            log_printf(ERROR, "both heartbeats are offline!!!!!!");
+                //TODO: sent this to emergency listeners
+          }
+        }
+      } else {
+        if(!state_.hb_state_standby_) {
+          if(state_.hb_state_master_) {
+            state_.mode_ = MODE_MASTER;
+            return change_mode(old_mode, fd, cmd_q, opt, client_lst);
+          } else {
+            log_printf(ERROR, "both heartbeats are offline!!!!!!");
+                //TODO: sent this to emergency listeners
+          }
+        }
+      }
     }
     else {
       log_printf(ERROR, "unable to update heartbeat status: client not found");
@@ -511,6 +533,7 @@ void process_cmd_heartbeat(const char* param, int fd, client_t* client_lst)
     log_printf(ERROR, "unable to update heartbeat status: empty parameter");
     send_response(fd, "EEE: heartbeat: missing parameter");
   }
+  return 0;
 }
 
 void process_cmd_listen(const char* param, int fd, client_t* client_lst)
@@ -643,7 +666,12 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op
       return ret;
     break;
   }
-  case HEARTBEAT: process_cmd_heartbeat(param, fd, client_lst); break;
+  case HEARTBEAT: {
+    int ret = process_cmd_heartbeat(param, fd, cmd_q, client_lst, opt);
+    if(ret)
+      return ret;
+    break;
+  }
   case LOG: {
     if(param && param[0])
       log_printf(NOTICE, "ext msg: %s", param);
-- 
cgit v0.10.2