signalling of receiving sync message sync
authorVolker Birk <vb@pep.foundation>
Thu, 23 Aug 2018 17:14:41 +0200
branchsync
changeset 28885359f8f5cc5c
parent 2887 c5ab1f6ad3ee
child 2889 683eba0e3d81
signalling of receiving sync message
src/message_api.c
sync/gen_statemachine.ysl2
test/include/SyncTests.h
test/src/engine_tests/SyncTests.cc
     1.1 --- a/src/message_api.c	Thu Aug 23 15:33:19 2018 +0200
     1.2 +++ b/src/message_api.c	Thu Aug 23 17:14:41 2018 +0200
     1.3 @@ -7,6 +7,7 @@
     1.4  #include "platform.h"
     1.5  #include "mime.h"
     1.6  #include "blacklist.h"
     1.7 +#include "Sync_impl.h"
     1.8  
     1.9  #include <assert.h>
    1.10  #include <string.h>
    1.11 @@ -3472,7 +3473,16 @@
    1.12          PEP_decrypt_flags_t *flags
    1.13      )
    1.14  {
    1.15 -    return _decrypt_message( session, src, dst, keylist, rating, flags, NULL );
    1.16 +    PEP_STATUS status = _decrypt_message(session, src, dst, keylist, rating, flags, NULL);
    1.17 +
    1.18 +    if (session->inject_sync_event && *dst && (*dst)->attachments) {
    1.19 +        for (bloblist_t *bl = (*dst)->attachments; bl ; bl = bl->next) {
    1.20 +            if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0)
    1.21 +                signal_Sync_message(session, *rating, bl->value, bl->size);
    1.22 +        }
    1.23 +    }
    1.24 +
    1.25 +    return status;
    1.26  }
    1.27  
    1.28  DYNAMIC_API PEP_STATUS own_message_private_key_details(
     2.1 --- a/sync/gen_statemachine.ysl2	Thu Aug 23 15:33:19 2018 +0200
     2.2 +++ b/sync/gen_statemachine.ysl2	Thu Aug 23 17:14:41 2018 +0200
     2.3 @@ -145,15 +145,6 @@
     2.4                  | PEP_STATUS «@name»(PEP_SESSION session);
     2.5              ||
     2.6  
     2.7 -            // notify state machine from event
     2.8 -            // use state to generate «@name» message if necessary
     2.9 -
    2.10 -            PEP_STATUS «@name»_notify(
    2.11 -                    PEP_SESSION session, 
    2.12 -                    «@name»_PR fsm,
    2.13 -                    int message_type
    2.14 -                );
    2.15 -
    2.16              // send message about an event to communication partners using state
    2.17  
    2.18              PEP_STATUS send_«@name»_message(
    2.19 @@ -178,12 +169,25 @@
    2.20                      int event
    2.21                  );
    2.22  
    2.23 +            // API being used by the engine internally
    2.24 +
    2.25 +            // call this if you need to signal an external event
    2.26 +
    2.27              PEP_STATUS signal_«@name»_event(
    2.28                      PEP_SESSION session, 
    2.29                      «@name»_PR fsm,
    2.30                      int event
    2.31                  );
    2.32 +            
    2.33 +            // call this if you are a transport and are receiving a sync
    2.34 +            // message
    2.35  
    2.36 +            PEP_STATUS signal_«@name»_message(
    2.37 +                    PEP_SESSION session, 
    2.38 +                    PEP_rating rating,
    2.39 +                    const char *data,
    2.40 +                    size_t size
    2.41 +                );
    2.42  
    2.43              #ifdef __cplusplus
    2.44              }
    2.45 @@ -241,79 +245,77 @@
    2.46  
    2.47                  PEP_STATUS status = PEP_STATUS_OK;
    2.48  
    2.49 -                if (!session->inject_«yml:lcase(@name)»_event) {
    2.50 -                   status = PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
    2.51 -                   goto error;
    2.52 -                }
    2.53 +                if (!session->inject_«yml:lcase(@name)»_event)
    2.54 +                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
    2.55  
    2.56                  if (event < Extra) {
    2.57                      msg = new_«@name»_message(fsm, event);
    2.58                      if (!msg) {
    2.59                          status = PEP_OUT_OF_MEMORY;
    2.60 -                        goto error;
    2.61 +                        goto the_end;
    2.62                      }
    2.63  
    2.64                      status = update_«@name»_message(session, msg);
    2.65                      if (status)
    2.66 -                        goto error;
    2.67 +                        goto the_end;
    2.68                  }
    2.69  
    2.70 -                ev = («@name»_event_t *) calloc(1, sizeof(«@name»_event_t));
    2.71 -                assert(ev);
    2.72 +                ev = new_«@name»_event(fsm, event, msg);
    2.73                  if (!ev) {
    2.74                      status = PEP_OUT_OF_MEMORY;
    2.75 -                    goto error;
    2.76 +                    goto the_end;
    2.77                  }
    2.78 -                
    2.79 -                ev->fsm = fsm;
    2.80 -                ev->event = event;
    2.81 -                ev->msg = msg;
    2.82  
    2.83                  int result = session->inject_«yml:lcase(@name)»_event(ev,
    2.84                          session->«yml:lcase(@name)»_management);
    2.85                  if (result) {
    2.86                      status = PEP_STATEMACHINE_ERROR;
    2.87 -                    goto error;
    2.88 +                    goto the_end;
    2.89                  }
    2.90 -
    2.91 -                goto the_end;
    2.92 -
    2.93 -            error:
    2.94 -                free(ev);
    2.95 -                free_«@name»_message(msg);
    2.96 +                return PEP_STATUS_OK;
    2.97  
    2.98              the_end:
    2.99 +                free_«@name»_event(ev);
   2.100 +                free_«@name»_message(msg);
   2.101                  return status;
   2.102              }
   2.103  
   2.104 -            PEP_STATUS «@name»_notify(
   2.105 +            PEP_STATUS signal_«@name»_message(
   2.106                      PEP_SESSION session, 
   2.107 -                    «@name»_PR fsm,
   2.108 -                    int message_type
   2.109 +                    PEP_rating rating,
   2.110 +                    const char *data,
   2.111 +                    size_t size
   2.112                  )
   2.113              {
   2.114 -                assert(session && fsm > 0 && message_type > 1 && message_type < Extra);
   2.115 -                if (!(session && fsm > 0 && message_type > 1 && message_type < Extra))
   2.116 +                assert(session && data && size);
   2.117 +                if (!(session && data && size))
   2.118                      return PEP_ILLEGAL_VALUE;
   2.119  
   2.120 -                PEP_STATUS status = PEP_STATUS_OK;
   2.121 +                if (!session->inject_«yml:lcase(@name)»_event)
   2.122 +                   return PEP_«yml:ucase(@name)»_NO_INJECT_CALLBACK;
   2.123  
   2.124 -                «@name»_t *msg = new_«@name»_message(fsm, message_type);
   2.125 -                if (!msg) {
   2.126 +                «@name»_t *msg = NULL;
   2.127 +                PEP_STATUS status = decode_«@name»_message(data, size, &msg);
   2.128 +                if (status)
   2.129 +                    return status;
   2.130 +
   2.131 +                «@name»_event_t *ev = new_«@name»_event(None, None, msg);
   2.132 +                if (!ev) {
   2.133                      status = PEP_OUT_OF_MEMORY;
   2.134 -                    goto error;
   2.135 +                    goto the_end;
   2.136                  }
   2.137  
   2.138 -                status = update_«@name»_message(session, msg);
   2.139 -                if (status)
   2.140 -                    goto error;
   2.141 -
   2.142 -                goto the_end;
   2.143 -
   2.144 -            error:
   2.145 -                free_«@name»_message(msg);
   2.146 +                int result = session->inject_«yml:lcase(@name)»_event(ev,
   2.147 +                        session->«yml:lcase(@name)»_management);
   2.148 +                if (result) {
   2.149 +                    status = PEP_STATEMACHINE_ERROR;
   2.150 +                    goto the_end;
   2.151 +                }
   2.152 +                return PEP_STATUS_OK;
   2.153  
   2.154              the_end:
   2.155 +                free_«@name»_event(ev);
   2.156 +                free_«@name»_message(msg);
   2.157                  return status;
   2.158              }
   2.159  
   2.160 @@ -450,39 +452,34 @@
   2.161                      return PEP_ILLEGAL_VALUE;
   2.162  
   2.163                  PEP_STATUS status = PEP_STATUS_OK;
   2.164 +                «@name»_PR fsm = (int) None;
   2.165 +                int event = None;
   2.166  
   2.167                  if (ev->event < Extra) {
   2.168 -                    «@name»_PR fsm = (int) None;
   2.169 -                    int event = None;
   2.170 -
   2.171                      status = update_«@name»_state(session, ev->msg, &fsm, &event);
   2.172                      if (status)
   2.173 -                        goto error;
   2.174 +                        goto the_end;
   2.175  
   2.176                      if (ev->fsm) {
   2.177                          if (ev->fsm != fsm |`> |` ev->event != event) {
   2.178                              status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   2.179 -                            goto error;
   2.180 +                            goto the_end;
   2.181                          }
   2.182                      }
   2.183 -                    else {
   2.184 -                        if (ev->event) {
   2.185 -                            status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   2.186 -                            goto error;
   2.187 -                        }
   2.188 -                        ev->fsm = fsm;
   2.189 -                        ev->event = event;
   2.190 +                    else if (ev->event) {
   2.191 +                        status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   2.192 +                        goto the_end;
   2.193                      }
   2.194                  }
   2.195 +                else {
   2.196 +                    fsm = ev->fsm;
   2.197 +                    event = ev->event;
   2.198 +                }
   2.199  
   2.200 -                free_«@name»_message(ev->msg);
   2.201 -                free(ev);
   2.202 -                status = «@name»_driver(session, ev->fsm, ev->event);
   2.203 -                return status;
   2.204 +                status = «@name»_driver(session, fsm, event);
   2.205  
   2.206 -            error:
   2.207 -                free_«@name»_message(ev->msg);
   2.208 -                free(ev);
   2.209 +            the_end:
   2.210 +                free_«@name»_event(ev);
   2.211                  return status;
   2.212              }
   2.213  
     3.1 --- a/test/include/SyncTests.h	Thu Aug 23 15:33:19 2018 +0200
     3.2 +++ b/test/include/SyncTests.h	Thu Aug 23 17:14:41 2018 +0200
     3.3 @@ -17,6 +17,8 @@
     3.4  public:
     3.5      utility::locked_queue< Sync_event_t * > q;
     3.6  
     3.7 +    void processing();
     3.8 +
     3.9      static PEP_STATUS notifyHandshake(
    3.10              void *obj,
    3.11              pEp_identity *me,
     4.1 --- a/test/src/engine_tests/SyncTests.cc	Thu Aug 23 15:33:19 2018 +0200
     4.2 +++ b/test/src/engine_tests/SyncTests.cc	Thu Aug 23 17:14:41 2018 +0200
     4.3 @@ -15,6 +15,14 @@
     4.4  
     4.5  using namespace std;
     4.6  
     4.7 +void Sync_Adapter::processing()
     4.8 +{
     4.9 +    cout << "waiting for processing\n";
    4.10 +    while (!q.empty()) {
    4.11 +        sleep(1);
    4.12 +    }
    4.13 +}
    4.14 +
    4.15  PEP_STATUS Sync_Adapter::notifyHandshake(
    4.16          void *obj,
    4.17          pEp_identity *me,
    4.18 @@ -132,10 +140,7 @@
    4.19  
    4.20  void SyncTests::tear_down()
    4.21  {
    4.22 -    cout << "waiting for processing\n";
    4.23 -    while (!adapter.q.empty()) {
    4.24 -        sleep(1);
    4.25 -    }
    4.26 +    adapter.processing();
    4.27  
    4.28      cout << "sending shutdown to sync thread\n";
    4.29      adapter.q.push_front(nullptr);
    4.30 @@ -151,6 +156,7 @@
    4.31  {
    4.32      cout << "check_sync(): trigger KeyGen event\n";
    4.33      signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
    4.34 +    adapter.processing();
    4.35  
    4.36      cout << "check_sync(): cry for unknown key\n";
    4.37      signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);