sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Sat, 26 Nov 2016 03:54:06 +0100
branchENGINE-133
changeset 1445 5d233bcdde76
parent 1409 e9bcd6142d54
child 1459 ab329d7db8d7
permissions -rw-r--r--
ENGINE-133 added timeout in sync state machine, relying on callback sync message retrieve timeout - adapters need update
vb@563
     1
// DeviceGroup protocol for p≡p
vb@563
     2
vb@563
     3
// Copyleft (c) 2016, p≡p foundation
vb@563
     4
vb@563
     5
// Written by Volker Birk
vb@563
     6
vb@563
     7
include ./fsm.yml2
vb@563
     8
vb@563
     9
protocol DeviceGroup {
vb@563
    10
    // all messages have a timestamp, time out and are removed after timeout
vb@563
    11
vb@1047
    12
    broadcast sendBeacon;
edouard@1281
    13
    broadcast sendGroupUpdate;
edouard@1297
    14
    broadcast sendUpdateRequest;
vb@1097
    15
    unencrypted sendBeacon;
vb@1047
    16
vb@807
    17
    fsm DeviceState filename=sync {
vb@951
    18
        condition storedGroupKeys();
vb@951
    19
        condition keyElectionWon(Identity partner);
vb@951
    20
vb@624
    21
        state InitState {
vb@624
    22
            on Init {
vb@951
    23
                if storedGroupKeys()
vb@624
    24
                    go Grouped;
vb@624
    25
                go Sole;
vb@624
    26
            }
vb@563
    27
        }
vb@563
    28
vb@1409
    29
        state Sole end=1 {
vb@944
    30
            on KeyGen // injected by generate_keypair()
vb@1097
    31
                do sendBeacon;
vb@563
    32
            on CannotDecrypt
vb@566
    33
                do sendBeacon;  // cry, baby
Edouard@594
    34
            on Beacon(Identity partner) // this event will not happen for already
vb@571
    35
                                        // rejected partners
vb@582
    36
                do sendHandshakeRequest(partner);
Edouard@594
    37
            on HandshakeRequest(Identity partner) {
vb@582
    38
                do sendHandshakeRequest(partner);
vb@563
    39
                go HandshakingSole(partner);
vb@563
    40
            }
vb@563
    41
        }
vb@563
    42
edouard@1445
    43
        state HandshakingSole timeout=10 (Identity partner) {
vb@582
    44
            on Init
vb@563
    45
                do showHandshake(partner);
Edouard@594
    46
            on HandshakeRejected(Identity partner) {
edouard@1161
    47
                do rejectHandshake(partner);             // stores rejection of partner
vb@563
    48
                go Sole;
vb@563
    49
            }
Edouard@594
    50
            on HandshakeAccepted(Identity partner) {
edouard@1161
    51
                do acceptHandshake(partner); 
vb@567
    52
                if keyElectionWon(partner) {    // an already existing group
vb@567
    53
                                                // always wins
edouard@1161
    54
                    do sendGroupKeys(partner);
vb@563
    55
                    go Grouped;
vb@563
    56
                }
edouard@1216
    57
                go WaitForGroupKeysSole(Identity partner);
vb@563
    58
            }
edouard@1445
    59
            on Cancel go Sole;
edouard@1445
    60
            on Timeout {
edouard@1445
    61
                // TODO signal group failure to app
edouard@1445
    62
                // do dismissHandshake(partner);
edouard@1445
    63
                go Sole;
edouard@1445
    64
            }
vb@563
    65
        }
vb@563
    66
    
edouard@1445
    67
        state WaitForGroupKeysSole timeout=10 (Identity partner) {
vb@711
    68
            on GroupKeys(Identity partner, Stringlist keys) {
Edouard@605
    69
                do storeGroupKeys(partner, keys);
edouard@1445
    70
                // TODO signal group success to app
edouard@1445
    71
                // do groupSuccess(partner);
vb@563
    72
                go Grouped;
vb@563
    73
            }
edouard@1445
    74
            on Timeout {
edouard@1445
    75
                // TODO signal group failure to app
edouard@1445
    76
                // do groupTimeout(partner);
vb@569
    77
                go Sole;
vb@569
    78
            }
vb@563
    79
        }
vb@563
    80
vb@1409
    81
        state Grouped end=1 {
edouard@1297
    82
            on Init 
edouard@1297
    83
                do enterGroup;
vb@563
    84
            on KeyGen
edouard@1281
    85
                do sendGroupUpdate;
edouard@1297
    86
            on CannotDecrypt
edouard@1297
    87
                do sendUpdateRequest; // TODO: narrow request to missing key
edouard@1297
    88
            on UpdateRequest
edouard@1297
    89
                do sendGroupUpdate;
edouard@1216
    90
            on Beacon(Identity partner)
edouard@1216
    91
                do sendHandshakeRequest(partner);
Edouard@594
    92
            on HandshakeRequest(Identity partner) {
vb@582
    93
                do sendHandshakeRequest(partner);
edouard@1216
    94
                go HandshakingGrouped(partner);
edouard@1216
    95
            }
edouard@1281
    96
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1236
    97
                do storeGroupKeys(partner, keys);
edouard@1216
    98
        }
edouard@1216
    99
edouard@1445
   100
        state HandshakingGrouped timeout=10 (Identity partner) {
edouard@1216
   101
            on Init
vb@563
   102
                do showHandshake(partner);
edouard@1216
   103
            on HandshakeRejected(Identity partner) {
edouard@1216
   104
                do rejectHandshake(partner);             // stores rejection of partner
edouard@1216
   105
                go Grouped;
vb@563
   106
            }
edouard@1216
   107
            on HandshakeAccepted(Identity partner) {
edouard@1161
   108
                do acceptHandshake(partner); 
edouard@1216
   109
edouard@1216
   110
                // an already existing group always wins
vb@711
   111
                do sendGroupKeys(partner);
edouard@1445
   112
edouard@1445
   113
                // TODO signal group success to app
edouard@1445
   114
                // do groupSuccess(partner);
edouard@1445
   115
edouard@1445
   116
                go Grouped;
edouard@1445
   117
            }
edouard@1445
   118
            on Timeout {
edouard@1445
   119
                // TODO kill Handshake dialog in app
edouard@1445
   120
                // do dismissHandshake();
edouard@1216
   121
                go Grouped;
edouard@1161
   122
            }
vb@563
   123
        }
edouard@1216
   124
    
Edouard@613
   125
vb@951
   126
        tag Init 1;
vb@951
   127
        tag Beacon 2;
vb@951
   128
        tag HandshakeRequest 3;
vb@951
   129
        tag GroupKeys 4;
Edouard@613
   130
    }
vb@563
   131
}
vb@563
   132