ENGINE-133 added timeout in sync state machine, relying on callback sync message retrieve timeout - adapters need update ENGINE-133
authorEdouard Tisserant <edouard@pep-project.org>
Sat, 26 Nov 2016 03:54:06 +0100
branchENGINE-133
changeset 14455d233bcdde76
parent 1444 30d8ab127616
child 1446 a948c1312c47
ENGINE-133 added timeout in sync state machine, relying on callback sync message retrieve timeout - adapters need update
src/sync.c
src/sync.h
src/sync_driver.c
src/sync_fsm.h
src/sync_impl.c
src/sync_impl.h
sync/devicegroup.fsm
sync/fsm.yml2
sync/gen_statemachine.ysl2
     1.1 --- a/src/sync.c	Fri Nov 25 15:59:58 2016 +0100
     1.2 +++ b/src/sync.c	Sat Nov 26 03:54:06 2016 +0100
     1.3 @@ -10,7 +10,8 @@
     1.4  
     1.5  PEP_STATUS receive_sync_msg(
     1.6          PEP_SESSION session,
     1.7 -        sync_msg_t *sync_msg
     1.8 +        sync_msg_t *sync_msg,
     1.9 +        time_t *timeout
    1.10      );
    1.11  
    1.12  DYNAMIC_API PEP_STATUS register_sync_callbacks(
    1.13 @@ -38,7 +39,8 @@
    1.14  
    1.15      // start state machine
    1.16      session->sync_state = InitState;
    1.17 -    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
    1.18 +    time_t unused = 0;
    1.19 +    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL, &unused);
    1.20      if (status != PEP_STATUS_OK)
    1.21          unregister_sync_callbacks(session);
    1.22  
    1.23 @@ -149,6 +151,7 @@
    1.24  {
    1.25      sync_msg_t *msg = NULL;
    1.26      PEP_STATUS status = PEP_STATUS_OK;
    1.27 +    time_t timeout = 0;
    1.28  
    1.29      assert(session && session->retrieve_next_sync_msg);
    1.30      assert(management);
    1.31 @@ -158,9 +161,9 @@
    1.32  
    1.33      log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    1.34  
    1.35 -    while ((msg = (sync_msg_t *) session->retrieve_next_sync_msg(management))) 
    1.36 +    while ((msg = (sync_msg_t *) session->retrieve_next_sync_msg(management, &timeout))) 
    1.37      {
    1.38 -        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
    1.39 +        if ((status = receive_sync_msg(session, msg, &timeout) != PEP_STATUS_OK)) {
    1.40              char buffer[MAX_LINELENGTH];
    1.41              memset(buffer, 0, MAX_LINELENGTH);
    1.42              snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
     2.1 --- a/src/sync.h	Fri Nov 25 15:59:58 2016 +0100
     2.2 +++ b/src/sync.h	Sat Nov 26 03:54:06 2016 +0100
     2.3 @@ -84,11 +84,14 @@
     2.4  //
     2.5  //  parameters:
     2.6  //      management (in)     application defined
     2.7 +//      timeout (in,out)    do not wait longer than timeout for message
     2.8  //
     2.9  //  return value:
    2.10 -//      next message or NULL for termination
    2.11 +//      next message or :
    2.12 +//      NULL + timeout == 0 for termination
    2.13 +//      NULL + timeout != 0 for timeout occurence
    2.14  
    2.15 -typedef void *(*retrieve_next_sync_msg_t)(void *management);
    2.16 +typedef void *(*retrieve_next_sync_msg_t)(void *management, time_t *timeout);
    2.17  
    2.18  
    2.19  // register_sync_callbacks() - register adapter's callbacks
     3.1 --- a/src/sync_driver.c	Fri Nov 25 15:59:58 2016 +0100
     3.2 +++ b/src/sync_driver.c	Sat Nov 26 03:54:06 2016 +0100
     3.3 @@ -8,7 +8,8 @@
     3.4          PEP_SESSION session,
     3.5          DeviceState_event event,
     3.6          Identity partner,
     3.7 -        void *extra
     3.8 +        void *extra,
     3.9 +        time_t *timeout
    3.10      )
    3.11  {
    3.12      assert(session);
    3.13 @@ -18,7 +19,7 @@
    3.14      while(true)
    3.15      {
    3.16          DeviceState_state new_state = fsm_DeviceState(session,
    3.17 -            session->sync_state, event, partner, extra);
    3.18 +            session->sync_state, event, partner, extra, timeout);
    3.19  
    3.20          if (new_state == DeviceState_state_invalid_out_of_memory)
    3.21              return PEP_OUT_OF_MEMORY;
     4.1 --- a/src/sync_fsm.h	Fri Nov 25 15:59:58 2016 +0100
     4.2 +++ b/src/sync_fsm.h	Sat Nov 26 03:54:06 2016 +0100
     4.3 @@ -65,7 +65,7 @@
     4.4      HandshakeRejected, 
     4.5      HandshakeAccepted, 
     4.6      Cancel, 
     4.7 -    Reject, 
     4.8 +    Timeout, 
     4.9      UpdateRequest, 
    4.10      GroupUpdate
    4.11  } DeviceState_event;
    4.12 @@ -107,7 +107,8 @@
    4.13          DeviceState_state state,
    4.14          DeviceState_event event,
    4.15          Identity partner,
    4.16 -        void *extra
    4.17 +        void *extra,
    4.18 +        time_t *timeout
    4.19      );
    4.20  
    4.21  // driver
    4.22 @@ -116,7 +117,8 @@
    4.23          PEP_SESSION session,
    4.24          DeviceState_event event,
    4.25          Identity partner,
    4.26 -        void *extra
    4.27 +        void *extra,
    4.28 +        time_t *timeout
    4.29      );
    4.30  
    4.31  #ifdef __cplusplus
     5.1 --- a/src/sync_impl.c	Fri Nov 25 15:59:58 2016 +0100
     5.2 +++ b/src/sync_impl.c	Sat Nov 26 03:54:06 2016 +0100
     5.3 @@ -31,7 +31,8 @@
     5.4  
     5.5  PEP_STATUS receive_sync_msg(
     5.6          PEP_SESSION session,
     5.7 -        sync_msg_t *sync_msg
     5.8 +        sync_msg_t *sync_msg,
     5.9 +        time_t *timeout
    5.10      )
    5.11  {
    5.12      PEP_STATUS status;
    5.13 @@ -183,7 +184,7 @@
    5.14              goto error;
    5.15      }
    5.16  
    5.17 -    status = fsm_DeviceState_inject(session, event, partner, extra);
    5.18 +    status = fsm_DeviceState_inject(session, event, partner, extra, timeout);
    5.19  
    5.20      free_identity(partner);
    5.21  
     6.1 --- a/src/sync_impl.h	Fri Nov 25 15:59:58 2016 +0100
     6.2 +++ b/src/sync_impl.h	Sat Nov 26 03:54:06 2016 +0100
     6.3 @@ -10,7 +10,8 @@
     6.4  
     6.5  PEP_STATUS receive_sync_msg(
     6.6          PEP_SESSION session,
     6.7 -        sync_msg_t *sync_msg
     6.8 +        sync_msg_t *sync_msg,
     6.9 +        time_t *timeout
    6.10      );
    6.11  
    6.12  PEP_STATUS inject_DeviceState_event(
     7.1 --- a/sync/devicegroup.fsm	Fri Nov 25 15:59:58 2016 +0100
     7.2 +++ b/sync/devicegroup.fsm	Sat Nov 26 03:54:06 2016 +0100
     7.3 @@ -40,7 +40,7 @@
     7.4              }
     7.5          }
     7.6  
     7.7 -        state HandshakingSole(Identity partner) {
     7.8 +        state HandshakingSole timeout=10 (Identity partner) {
     7.9              on Init
    7.10                  do showHandshake(partner);
    7.11              on HandshakeRejected(Identity partner) {
    7.12 @@ -56,16 +56,24 @@
    7.13                  }
    7.14                  go WaitForGroupKeysSole(Identity partner);
    7.15              }
    7.16 +            on Cancel go Sole;
    7.17 +            on Timeout {
    7.18 +                // TODO signal group failure to app
    7.19 +                // do dismissHandshake(partner);
    7.20 +                go Sole;
    7.21 +            }
    7.22          }
    7.23      
    7.24 -        state WaitForGroupKeysSole(Identity partner) {
    7.25 +        state WaitForGroupKeysSole timeout=10 (Identity partner) {
    7.26              on GroupKeys(Identity partner, Stringlist keys) {
    7.27                  do storeGroupKeys(partner, keys);
    7.28 +                // TODO signal group success to app
    7.29 +                // do groupSuccess(partner);
    7.30                  go Grouped;
    7.31              }
    7.32 -            on Cancel go Sole;
    7.33 -            on Reject(Identity partner) {
    7.34 -                do rejectHandshake(partner);
    7.35 +            on Timeout {
    7.36 +                // TODO signal group failure to app
    7.37 +                // do groupTimeout(partner);
    7.38                  go Sole;
    7.39              }
    7.40          }
    7.41 @@ -89,7 +97,7 @@
    7.42                  do storeGroupKeys(partner, keys);
    7.43          }
    7.44  
    7.45 -        state HandshakingGrouped(Identity partner) {
    7.46 +        state HandshakingGrouped timeout=10 (Identity partner) {
    7.47              on Init
    7.48                  do showHandshake(partner);
    7.49              on HandshakeRejected(Identity partner) {
    7.50 @@ -101,6 +109,15 @@
    7.51  
    7.52                  // an already existing group always wins
    7.53                  do sendGroupKeys(partner);
    7.54 +
    7.55 +                // TODO signal group success to app
    7.56 +                // do groupSuccess(partner);
    7.57 +
    7.58 +                go Grouped;
    7.59 +            }
    7.60 +            on Timeout {
    7.61 +                // TODO kill Handshake dialog in app
    7.62 +                // do dismissHandshake();
    7.63                  go Grouped;
    7.64              }
    7.65          }
     8.1 --- a/sync/fsm.yml2	Fri Nov 25 15:59:58 2016 +0100
     8.2 +++ b/sync/fsm.yml2	Sat Nov 26 03:54:06 2016 +0100
     8.3 @@ -6,7 +6,7 @@
     8.4  
     8.5  decl protocol @name;
     8.6  decl fsm @name;
     8.7 -decl state @name;
     8.8 +decl state @name (timeout=0);
     8.9  decl event @name, on is event;
    8.10  decl transition @target, go is transition;
    8.11  decl action @name, do is action;
     9.1 --- a/sync/gen_statemachine.ysl2	Fri Nov 25 15:59:58 2016 +0100
     9.2 +++ b/sync/gen_statemachine.ysl2	Sat Nov 26 03:54:06 2016 +0100
     9.3 @@ -115,7 +115,8 @@
     9.4                  «@name»_state state,
     9.5                  «@name»_event event,
     9.6                  Identity partner,
     9.7 -                void *extra
     9.8 +                void *extra,
     9.9 +                time_t *timeout
    9.10              );
    9.11  
    9.12          // driver
    9.13 @@ -124,7 +125,8 @@
    9.14                  PEP_SESSION session,
    9.15                  «@name»_event event,
    9.16                  Identity partner,
    9.17 -                void *extra
    9.18 +                void *extra,
    9.19 +                time_t *timeout
    9.20              );
    9.21  
    9.22          #ifdef __cplusplus
    9.23 @@ -145,7 +147,8 @@
    9.24                  PEP_SESSION session,
    9.25                  «@name»_event event,
    9.26                  Identity partner,
    9.27 -                void *extra
    9.28 +                void *extra,
    9.29 +                time_t *timeout
    9.30              )
    9.31          {
    9.32              assert(session);
    9.33 @@ -155,7 +158,7 @@
    9.34              while(true)
    9.35              {
    9.36                  «@name»_state new_state = fsm_«@name»(session,
    9.37 -                    session->«@filename»_state, event, partner, extra);
    9.38 +                    session->«@filename»_state, event, partner, extra, timeout);
    9.39  
    9.40                  if (new_state == «@name»_state_invalid_out_of_memory)
    9.41                      return PEP_OUT_OF_MEMORY;
    9.42 @@ -186,7 +189,8 @@
    9.43                  «@name»_state state,
    9.44                  «@name»_event event,
    9.45                  Identity partner,
    9.46 -                void *extra
    9.47 +                void *extra,
    9.48 +                time_t *timeout
    9.49              )
    9.50          {
    9.51              int cond_result;
    9.52 @@ -207,6 +211,7 @@
    9.53      template "state"
    9.54      ||
    9.55      case «@name»:
    9.56 +        *timeout = «@timeout»;
    9.57          switch (event) {
    9.58          `` if "not(event[@name='Init'])" |>> case Init: break;
    9.59          `` apply "event", 2