somewhat bigger sync
authorVolker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 289963b619aef131
parent 2891 adf0103397e4
child 2900 7bef073c3952
somewhat bigger
asn.1/Makefile
src/Makefile
src/pEpEngine.h
src/sync_api.c
src/sync_api.h
sync/Makefile
sync/gen_codec.ysl2
sync/gen_statemachine.ysl2
test/include/SyncTests.h
test/src/engine_tests/SyncTests.cc
     1.1 --- a/asn.1/Makefile	Thu Aug 23 22:20:04 2018 +0200
     1.2 +++ b/asn.1/Makefile	Wed Aug 29 17:10:23 2018 +0200
     1.3 @@ -19,7 +19,7 @@
     1.4  	$(CC) $(CFLAGS) $(OPTIMIZE) -I. -I$(ASN1C_INC) -c $< -o $@
     1.5  
     1.6  Sync.c: sync.asn1 keysync.asn1 pEp.asn1
     1.7 -	$(ASN1C) -gen-PER -fincludes-quoted -fcompound-names -pdu=PEP.Message pEp.asn1 keysync.asn1 $<
     1.8 +	$(ASN1C) -gen-PER -fincludes-quoted -fcompound-names -pdu=auto pEp.asn1 keysync.asn1 $<
     1.9  
    1.10  sync.asn1 keysync.asn1 pEp.asn1:
    1.11  	cp -f ../sync/generated/*.asn1 ../asn.1
     2.1 --- a/src/Makefile	Thu Aug 23 22:20:04 2018 +0200
     2.2 +++ b/src/Makefile	Wed Aug 29 17:10:23 2018 +0200
     2.3 @@ -90,7 +90,7 @@
     2.4  	cp -v pEpEngine.h keymanagement.h message_api.h dynamic_api.h stringlist.h \
     2.5  	      timestamp.h identity_list.h bloblist.h stringpair.h message.h mime.h \
     2.6  	      cryptotech.h sync_api.h blacklist.h pEp_string.h openpgp_compat.h \
     2.7 -		  ../asn.1/*.h $(PREFIX)/include/pEp/
     2.8 +		  ../asn.1/*.h sync_codec.h $(PREFIX)/include/pEp/
     2.9  
    2.10  .PHONY: uninstall
    2.11  uninstall:
     3.1 --- a/src/pEpEngine.h	Thu Aug 23 22:20:04 2018 +0200
     3.2 +++ b/src/pEpEngine.h	Wed Aug 29 17:10:23 2018 +0200
     3.3 @@ -122,7 +122,6 @@
     3.4  // messageToSend() - a message needs to be delivered by application
     3.5  //
     3.6  //  parameters:
     3.7 -//      obj (in)        object handle (implementation defined)
     3.8  //      msg (in)        message struct with message to send
     3.9  //
    3.10  //  return value:
    3.11 @@ -132,7 +131,7 @@
    3.12  //      the ownership of msg goes to the callee
    3.13  
    3.14  struct _message;
    3.15 -typedef PEP_STATUS (*messageToSend_t)(void *obj, struct _message *msg);
    3.16 +typedef PEP_STATUS (*messageToSend_t)(struct _message *msg);
    3.17  
    3.18  
    3.19  struct Sync_event;
     4.1 --- a/src/sync_api.c	Thu Aug 23 22:20:04 2018 +0200
     4.2 +++ b/src/sync_api.c	Wed Aug 29 17:10:23 2018 +0200
     4.3 @@ -87,7 +87,6 @@
     4.4      )
     4.5  {
     4.6      Sync_event_t *event= NULL;
     4.7 -    PEP_STATUS status = PEP_STATUS_OK;
     4.8  
     4.9      assert(session && session->retrieve_next_sync_event);
    4.10      if (!(session && session->retrieve_next_sync_event))
    4.11 @@ -95,20 +94,13 @@
    4.12  
    4.13      log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    4.14  
    4.15 -    session->sync_obj = obj;
    4.16      while (true) 
    4.17      {
    4.18          event = session->retrieve_next_sync_event(session->sync_management);
    4.19          if (!event)
    4.20              break;
    4.21  
    4.22 -        status = recv_Sync_event(session, event);
    4.23 -        if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
    4.24 -            char buffer[MAX_LINELENGTH];
    4.25 -            memset(buffer, 0, MAX_LINELENGTH);
    4.26 -            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
    4.27 -            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
    4.28 -        }
    4.29 +        do_sync_protocol_step(session, obj, event);
    4.30      }
    4.31      session->sync_obj = NULL;
    4.32  
    4.33 @@ -117,3 +109,37 @@
    4.34      return PEP_STATUS_OK;
    4.35  }
    4.36  
    4.37 +DYNAMIC_API PEP_STATUS do_sync_protocol_step(
    4.38 +        PEP_SESSION session,
    4.39 +        void *obj,
    4.40 +        SYNC_EVENT event
    4.41 +    )
    4.42 +{
    4.43 +    assert(session);
    4.44 +    if (!session)
    4.45 +        return PEP_ILLEGAL_VALUE;
    4.46 +
    4.47 +    if (!event)
    4.48 +        return PEP_STATUS_OK;
    4.49 +
    4.50 +    session->sync_obj = obj;
    4.51 +
    4.52 +    PEP_STATUS status = recv_Sync_event(session, event);
    4.53 +    if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
    4.54 +        char buffer[MAX_LINELENGTH];
    4.55 +        memset(buffer, 0, MAX_LINELENGTH);
    4.56 +        snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
    4.57 +        log_event(session, buffer, "pEp sync protocol", NULL, NULL);
    4.58 +    }
    4.59 +
    4.60 +    return status == PEP_MESSAGE_IGNORE ? PEP_STATUS_OK : status;
    4.61 +}
    4.62 +
    4.63 +DYNAMIC_API bool is_sync_thread(PEP_SESSION session)
    4.64 +{
    4.65 +    assert(session);
    4.66 +    if (!session)
    4.67 +        return false;
    4.68 +    return session->retrieve_next_sync_event != NULL;
    4.69 +}
    4.70 +
     5.1 --- a/src/sync_api.h	Thu Aug 23 22:20:04 2018 +0200
     5.2 +++ b/src/sync_api.h	Wed Aug 29 17:10:23 2018 +0200
     5.3 @@ -121,10 +121,6 @@
     5.4  //  return value:
     5.5  //      PEP_STATUS_OK if thread has to terminate successfully or any other
     5.6  //      value on failure
     5.7 -//
     5.8 -//  caveat:
     5.9 -//      to ensure proper working of this library, a thread has to be started
    5.10 -//      with this function immediately after initialization
    5.11  
    5.12  DYNAMIC_API PEP_STATUS do_sync_protocol(
    5.13          PEP_SESSION session,
    5.14 @@ -132,6 +128,34 @@
    5.15      );
    5.16  
    5.17  
    5.18 +// do_sync_protocol_step() - function for single threaded implementations
    5.19 +//
    5.20 +//  parameters:
    5.21 +//      session                 pEp session to use
    5.22 +//      retrieve_next_sync_msg  pointer to retrieve_next_identity() callback
    5.23 +//                              which returns at least a valid address field in
    5.24 +//                              the identity struct
    5.25 +//      obj                     application defined sync object
    5.26 +//      event                   Sync event to process
    5.27 +
    5.28 +DYNAMIC_API PEP_STATUS do_sync_protocol_step(
    5.29 +        PEP_SESSION session,
    5.30 +        void *obj,
    5.31 +        SYNC_EVENT event
    5.32 +    );
    5.33 +
    5.34 +
    5.35 +// is_sync_thread() - determine if this is sync thread's session
    5.36 +//
    5.37 +//  paramters:
    5.38 +//      session                 pEp session to test
    5.39 +//
    5.40 +//  return value:
    5.41 +//      true if this is sync thread's session, false otherwise
    5.42 +
    5.43 +DYNAMIC_API bool is_sync_thread(PEP_SESSION session);
    5.44 +
    5.45 +
    5.46  #ifdef __cplusplus
    5.47  }
    5.48  #endif
     6.1 --- a/sync/Makefile	Thu Aug 23 22:20:04 2018 +0200
     6.2 +++ b/sync/Makefile	Wed Aug 29 17:10:23 2018 +0200
     6.3 @@ -27,7 +27,7 @@
     6.4  clean:
     6.5  	rm -f *.xml *.xsl \
     6.6      $(pathsub generated/%, ../src/% $(wildcard generated/*.*)) \
     6.7 -    ../generated/* .statemachines .actions .codecs .messages *.dot *.svg
     6.8 +    generated/* .statemachines .actions .codecs .messages *.dot *.svg
     6.9  
    6.10  copy:
    6.11  	cp -f generated/*.c generated/*.h ../src
     7.1 --- a/sync/gen_codec.ysl2	Thu Aug 23 22:20:04 2018 +0200
     7.2 +++ b/sync/gen_codec.ysl2	Wed Aug 29 17:10:23 2018 +0200
     7.3 @@ -13,7 +13,7 @@
     7.4      include standardlib.ysl2
     7.5  
     7.6      template "/protocol" {
     7.7 -        document "generated/{@name}_codec.h", "text"
     7.8 +        document "generated/{yml:lcase(@name)}_codec.h", "text"
     7.9          ||
    7.10          // This file is under GNU General Public License 3.0
    7.11          // see LICENSE.txt
    7.12 @@ -22,7 +22,6 @@
    7.13  
    7.14  
    7.15          #include "pEpEngine.h"
    7.16 -        #include "../asn.1/«@name».h"
    7.17  
    7.18  
    7.19          #ifdef __cplusplus
    7.20 @@ -30,6 +29,8 @@
    7.21          #endif
    7.22  
    7.23  
    7.24 +        struct «@name»;
    7.25 +
    7.26          // decode_«@name»_message() - decode PER encoded «@name» message
    7.27          //
    7.28          //  parameters:
    7.29 @@ -43,7 +44,7 @@
    7.30          DYNAMIC_API PEP_STATUS decode_«@name»_message(
    7.31                  const char *data,
    7.32                  size_t size,
    7.33 -                «@name»_t **msg
    7.34 +                struct «@name» **msg
    7.35              );
    7.36  
    7.37  
    7.38 @@ -58,7 +59,7 @@
    7.39          //      data goes to the ownership of the caller
    7.40  
    7.41          DYNAMIC_API PEP_STATUS encode_«@name»_message(
    7.42 -                «@name»_t *msg,
    7.43 +                struct «@name» *msg,
    7.44                  char **data,
    7.45                  size_t *size
    7.46              );
    7.47 @@ -97,12 +98,13 @@
    7.48          #endif
    7.49  
    7.50          ||
    7.51 -        document "generated/{@name}_codec.c", "text"
    7.52 +        document "generated/{yml:lcase(@name)}_codec.c", "text"
    7.53          ||
    7.54          // This file is under GNU General Public License 3.0
    7.55          // see LICENSE.txt
    7.56  
    7.57 -        #include "«@name»_codec.h"
    7.58 +        #include "«yml:lcase(@name)»_codec.h"
    7.59 +        #include "../asn.1/«@name».h"
    7.60          #include "pEp_internal.h"
    7.61          #include "growing_buf.h"
    7.62  
     8.1 --- a/sync/gen_statemachine.ysl2	Thu Aug 23 22:20:04 2018 +0200
     8.2 +++ b/sync/gen_statemachine.ysl2	Wed Aug 29 17:10:23 2018 +0200
     8.3 @@ -51,7 +51,7 @@
     8.4          //      event must be valid for fsm or None
     8.5          //      in case msg is given event will be calculated out of message
     8.6  
     8.7 -        DYNAMIC_API «@name»_event_t *new_«@name»_event(«@name»_PR fsm, int event, «@name»_t *msg);
     8.8 +        «@name»_event_t *new_«@name»_event(«@name»_PR fsm, int event, «@name»_t *msg);
     8.9  
    8.10  
    8.11          // free_«@name»_event() - free memory occupied by event
    8.12 @@ -59,7 +59,7 @@
    8.13          //  parameters:
    8.14          //      ev (in)         event to free
    8.15  
    8.16 -        DYNAMIC_API void free_«@name»_event(«@name»_event_t *ev);
    8.17 +        void free_«@name»_event(«@name»_event_t *ev);
    8.18  
    8.19  
    8.20          #ifdef __cplusplus
    8.21 @@ -78,7 +78,7 @@
    8.22          #include "«@name»_func.h"
    8.23          `` for "fsm" | #include "«@name»_fsm.h"
    8.24  
    8.25 -        DYNAMIC_API «@name»_event_t *new_«@name»_event(«@name»_PR fsm, int event, «@name»_t *msg)
    8.26 +        «@name»_event_t *new_«@name»_event(«@name»_PR fsm, int event, «@name»_t *msg)
    8.27          {
    8.28              assert(fsm > 0 && (event >= 0 |`> |` msg));
    8.29              if (!(fsm > 0 && (event >= 0 |`> |` msg)))
    8.30 @@ -106,7 +106,7 @@
    8.31              return ev;
    8.32          }
    8.33  
    8.34 -        DYNAMIC_API void free_«@name»_event(«@name»_event_t *ev)
    8.35 +        void free_«@name»_event(«@name»_event_t *ev)
    8.36          {
    8.37              if (ev) {
    8.38                  free_«@name»_message(ev->msg);
    8.39 @@ -204,7 +204,7 @@
    8.40              #include "«@name»_impl.h"
    8.41              #include "pEp_internal.h"
    8.42              #include "«@name»_event.h"
    8.43 -            #include "«@name»_codec.h"
    8.44 +            #include "«yml:lcase(@name)»_codec.h"
    8.45              #include "baseprotocol.h"
    8.46              `` for "fsm" | #include "«@name»_fsm.h"
    8.47  
    8.48 @@ -433,13 +433,13 @@
    8.49                          default:
    8.50                              status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
    8.51                              if (status) {
    8.52 -                                status = PEP_SYNC_CANNOT_ENCRYPT;
    8.53 +                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
    8.54                                  goto the_end;
    8.55                              }
    8.56                              free_message(_m);
    8.57                      }
    8.58  
    8.59 -                    status = session->messageToSend(session->«yml:lcase(@name)»_obj, m);
    8.60 +                    status = session->messageToSend(m);
    8.61                      m = NULL;
    8.62                  }
    8.63  
     9.1 --- a/test/include/SyncTests.h	Thu Aug 23 22:20:04 2018 +0200
     9.2 +++ b/test/include/SyncTests.h	Wed Aug 29 17:10:23 2018 +0200
     9.3 @@ -27,7 +27,7 @@
     9.4          );
     9.5      static int inject_sync_event(SYNC_EVENT ev, void *management);
     9.6      static Sync_event_t *retrieve_next_sync_event(void *management);
     9.7 -    static PEP_STATUS messageToSend(void *obj, struct _message *msg);
     9.8 +    static PEP_STATUS messageToSend(struct _message *msg);
     9.9  
    9.10      static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter);
    9.11  };
    10.1 --- a/test/src/engine_tests/SyncTests.cc	Thu Aug 23 22:20:04 2018 +0200
    10.2 +++ b/test/src/engine_tests/SyncTests.cc	Wed Aug 29 17:10:23 2018 +0200
    10.3 @@ -8,7 +8,7 @@
    10.4  
    10.5  #include "pEp_internal.h"
    10.6  #include "KeySync_fsm.h"
    10.7 -#include "Sync_codec.h"
    10.8 +#include "sync_codec.h"
    10.9  
   10.10  #include "EngineTestSessionSuite.h"
   10.11  #include "SyncTests.h"
   10.12 @@ -19,7 +19,7 @@
   10.13  {
   10.14      cout << "waiting for processing\n";
   10.15      while (!q.empty()) {
   10.16 -        sleep(1);
   10.17 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
   10.18      }
   10.19  }
   10.20  
   10.21 @@ -54,7 +54,7 @@
   10.22      auto adapter = static_cast< Sync_Adapter *>(management);
   10.23  
   10.24      while (adapter->q.empty()) {
   10.25 -        sleep(1);
   10.26 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
   10.27      }
   10.28  
   10.29      Sync_event_t *ev = adapter->q.pop_front();
   10.30 @@ -75,7 +75,7 @@
   10.31      return ev;
   10.32  }
   10.33  
   10.34 -PEP_STATUS Sync_Adapter::messageToSend(void *obj, struct _message *msg)
   10.35 +PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
   10.36  {
   10.37      assert(msg && msg->attachments);
   10.38