-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscribble-java.html
More file actions
3816 lines (3094 loc) · 136 KB
/
Copy pathscribble-java.html
File metadata and controls
3816 lines (3094 loc) · 136 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>
Scribble-Java tutorial
</title>
<style>
<!--body { background-color: white; }-->
span.code { font-family: monospace; }
.boxed {
border: 1px solid black ;
}
</style>
<link href="user/Scribble-Java-tutorial_files/style.css" rel="stylesheet" type="text/css">
<link href="user/Scribble-Java-tutorial_files/prettify.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="user/Scribble-Java-tutorial_files/prettify-scribble.js"></script>
</head>
<body onload="prettyPrint()">
<h1>
Scribble-Java tutorial
</h1>
<!--<pre class="prettyprint lang-java">
def say_hi():
print("Hello World!")
</pre>-->
<p>
This is a tutorial for using the <a href="https://github.com/scribble/scribble-java">Scribble-Java</a> toolchain (for Scribble version 0.4).
</p>
<!--
<p>
For ...tools based on (older versions of) Scribble...
<ul>
<li>
savara? - not Scribble itself though
</li>
</ul>
</p>
-->
<hr><hr>
<p>
<a id="TOP">Contents:</a>
<br>
</p><ol class="nested">
<li class="nested">
<a href="#ABOUT">About this tutorial</a>
</li>
<br>
<li class="nested">
<a href="#QUICK">Quick Start: the Adder application protocol</a>
<ol class="nested" type="a">
<li class="nested">
<a href="#ADDEROVER">Protocol overview</a>
</li>
<li class="nested">
<a href="#ADDERGLOBAL">Scribble global protocol</a>
<ul>
<li>
<a href="#ADDERCL">Running the Scribble command line tool</a>
</li>
</ul>
</li>
<li class="nested">
<a href="#ADDERCLIENT">An Adder client</a>
<ul>
<li>
<a href="#CLIENTFSM">Endpoint FSM</a>
</li>
<li>
<a href="#CLIENTAPI">Endpoint API overview</a>
</li>
<li>
<a href="#CLIENTIMPLE">Endpoint implementation</a>
</li>
</ul>
</li>
<li class="nested">
<a href="#ADDERSERVER">An Adder server</a>
<ul>
<li>
<a href="#SERVERIMPLE">Endpoint implementation</a>
</li>
</ul>
</li>
</ol>
</li>
<br>
<li class="nested">
<a href="#SCRIBCORE">Scribble modules and global protocols</a>
<ol class="nested">
<li class="nested">
<a href="#SCRIBMODULE">Scribble modules</a>
</li>
<li class="nested">
<a href="#SCRIBSIG">Message signatures</a>
</li>
<li class="nested">
<a href="#SCRIBGLOBAL">Global protocol declarations</a>
</li>
<li class="nested">
<a href="#SCRIBINTERACT">The message interaction statement</a>
</li>
<li class="nested">
<a href="#SCRIBCHOICE">The <span class="code">choice</span> statement</a>
</li>
<!--<li class=nested>
<a href="#RECURSION">Recursion</a>
</li>-->
<li class="nested">
<a href="#SCRIBDO">The <span class="code">do</span> statement</a>
<ul>
<li>
<a href="#SCRIBRECURS">Recursive protocols</a>
</li>
</ul>
</li>
</ol>
</li>
<br>
<li class="nested">
<a href="#SCRIBMORE">Further global protocol features</a>
<ol class="nested">
<li class="nested">
<a href="#SCRIBSIGNAME">Message signature name declarations</a>
</li>
<li class="nested">
<a href="#SCRIBCONNECT">The connection interaction statement</a>
</li>
<!--<li class=nested>
<a href="#..TODO..">..parameterised global protocols...</a>
</li>-->
</ol>
</li>
<br>
<li class="nested">
<a href="#PROTOVALID">Scribble protocol validation</a>
<ol class="nested">
<li class="nested">
<a href="#VALIDWF">Valid global protocols and well-formed modules</a>
</li>
<li class="nested">
<a href="#VALIDFSM">Characteristics of Endpoint FSMs</a>
</li>
<li class="nested">
<a href="#VALIDPROP">Safety and liveness of Scribble global protocols</a>
</li>
</ol>
</li>
<br>
<li class="nested">
<a href="#APIGEN">Endpoint API generation</a>
<ol class="nested">
<li class="nested">
<a href="#APIOVER">Endpoint API overview</a>
</li>
<li class="nested">
<a href="#APINAMES">Names as singleton types</a>
</li>
<li class="nested">
<a href="#APISTATECHAN">State Channel API</a>
</li>
<li class="nested">
<a href="#APIOUTPUT">Output state API</a>
</li>
<li class="nested">
<a href="#APIRECEIVE">Receive state API</a>
</li>
<li class="nested">
<a href="#APIACCEPT">Accept state API</a>
</li>
<li class="nested">
<a href="#APIDISCONNECT">Disconnect state API</a>
</li>
<li class="nested">
<a href="#APILINEAR">Linear usage of state channel instances</a>
</li>
</ol>
</li>
<br>
<li class="nested">
<a href="#IMPLE">Endpoint implementation using Scribble-generated APIs</a>
<ol class="nested" type="a">
<li class="nested">
<a href="#IMPLECONNECT">Connection establishment for non-<span class="code">explicit</span> global protocols</a>
</li>
<li class="nested">
<a href="#IMPLEHYBRID">Usage contract of Endpoint APIs and endpoint implementation safety</a>
</li>
</ol>
</li>
<br>
<li class="nested">
<a href="#SCRIBBUILD">Building/installing Scribble-Java</a>
</li>
<br>
<li class="nested">
<a href="#SCRIBCL">Running the Scribble-Java command line tool</a>
</li>
</ol>
<p></p>
<hr><hr>
<h2>
<a id="ABOUT">About this tutorial</a>
</h2>
<p>
Scribble-Java is a toolchain for programming distributed applications in Java based on the theory of <em>multiparty session types</em>.
</p>
<p>
The main input and output of the toolchain are:
</p><ul>
<li>
Input: a multiparty message passing protocol written in the Scribble <a href="#SCRIBCORE">protocol specification language</a>;
</li>
<li>
Output: <a href="#APIGEN">Java APIs</a> for <a href="#IMPLE">implementing</a> the endpoints of the source protocol.
</li>
</ul>
<p></p>
<!--<p>
HAS BEEN DISCUSSED MANY TIMES: GENERAL CONCENSUS IS THAT A PROTOCOL IS SOMETHING WE WISH TO VERIFY, NOT VALIDATE. VERIFY=DID WE IMPLEMENT THE PROTOCOL CORRECTLY? VALIDATE=ARE WE VERIFYING THE RIGHT PROTOCOL? (TAKING THE SEEMINGLY TYPICAL NOTION OF THE TERMS VERIFY/VALIDATE IN ENGINEERING)
- but now well-formedness positioned as validation (does the protocol make sense?), and typing is verification (does the program follow the protocol?)
</p>-->
<h4>
Useful links and resources
</h4>
<p>
</p><ul>
<li>
Scribble <a href="http://www.scribble.org/">home page</a>
<ul>
<li>
<strong>N.B.</strong> some of the tools currently available from
the home page are for an older version of Scribble than presented in
this tutorial. Those include:
<ul>
<li>
Eclipse integration
</li>
<li>
Message trace simulation
</li>
<li>
Browser based tooling
</li>
</ul>
</li>
</ul>
</li>
<li>
Scribble-Java open source repository: <a href="https://github.com/scribble/scribble-java">GitHub</a>
<ul>
<li>
Some Scribble protocol and Java endpoint program examples: <a href="https://github.com/scribble/scribble-java/tree/master/scribble-demos/scrib">GitHub</a>
</li>
</ul>
</li>
<!--<li>
Scribble language reference: <a href="langref.html">html</a>
</li>
<li>
OOI use cases in Scribble taster document: <a href="./taster.html">pdf</a>
</li>-->
<!--
<li>
Papers ...
</li>
-->
</ul>
<p></p>
<h4>
Tutorial version history
</h4>
<ul>
<table>
<tbody><tr>
<td>
<li>0.4b </li>
</td>
<td>
(Draft) revision for Scribble 0.4.
</td>
</tr>
<!--<tr>
<td>
<li>0.3</li>
</td>
<td>
Sync with Language Ref 0.3. Adding Hello World from Gary.
</td>
</tr>
<tr>
<td>
<li>0.2</li>
</td>
<td>
Added OOI use cases. Miscellaneous edits.
</td>
</tr>
<tr>
<td>
<li>0.2</li>
</td>
<td>
Overview of the Scribble constructs for global protocol specification.
</td>
</tr>-->
</tbody></table>
</ul>
<h4>
Upcoming in future tutorial versions
</h4>
<p>
</p><ul>
<li>
Scribble language:
<ul>
<li>
Payload type and message signature name parameters for global protocol declarations
</li>
<li>
Messages in connection interaction statements
</li>
</ul>
</li>
<li>
Endpoint API generation:
<ul>
<li>
Branch callback API
</li>
<li>
Accept-branch API
</li>
<li>
Receive state futures
</li>
<li>
Abstract I/O state interfaces
<ul>
<li>
Choice subtype interfaces
</li>
</ul>
</li>
</ul>
</li>
<li>
Host languages:
<ul>
<li>
Scribble-Scala
<ul>
<li>
Multiparty session delegation
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p></p>
<h4>
Upcoming in future Scribble versions
</h4>
<p>
</p><ul>
<li>
Scribble language:
<ul>
<li>
Protocol annotations
<ul>
<li>
Message value assertions
</li>
<li>
Dynamic port actions (binary shared channel passing)
</li>
</ul>
</li>
</ul>
</li>
<li>
Endpoint API generation:
<ul>
<li>
State name annotations
</li>
<li>
Event-driven API
</li>
</ul>
</li>
<li>
Scribble Runtime API:
<ul>
<li>
Additional transports: shared memory, HTTP
</li>
</ul>
</li>
<li>
Host languages:
<ul>
<li>
Scribble-Go
<ul>
<li>
Role parameters for global protocol declarations
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p></p>
<p>
<
<a href="#TOP">top</a>;
<a href="#QUICK">next</a>
>
</p>
<hr><hr>
<h2>
<a id="QUICK">
Quick Start: the Adder application protocol
</a>
</h2>
<p>
This quick start will run through the specification of a simple
application protocol in Scribble, to Java implementations of client and
server endpoints using the Scribble-generated APIs.
</p>
<p>
<
<a href="#ABOUT">prev</a>;
<a href="#TOP">top</a>;
<a href="#ADDEROVER">next</a>
>
</p>
<hr>
<h3><a id="ADDEROVER">Protocol overview</a></h3>
<p>
We illustrate a client-server protocol for a network service that adds two numbers.
</p>
<p>
An outline of the protocol:
</p><ul>
<li>
The client <span class="code">C</span> may choose to send to the server <span class="code">S</span> one of the following two messages.
<ul>
<li>
An <span class="code">Add</span> message with a payload of two <span class="code">Int</span>s.
<ul>
<li>
After receiving the <span class="code">Add</span>, <span class="code">S</span> sends to <span class="code">C</span> a <span class="code">Res</span> message with a payload of one <span class="code">Int</span> (the sum of the received <span class="code">Int</span>s).
</li>
<li>
Then <span class="code">C</span> and <span class="code">S</span> continue by "looping" back to the start of the protocol.
</li>
</ul>
</li>
<li>
A <span class="code">Bye</span> message with an empty payload.
<ul>
<li>
The protocol ends for <span class="code">C</span> after sending.
</li>
<li>
The protocol ends for <span class="code">S</span> after receiving.
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p></p>
<p>
<
<a href="#QUICK">prev</a>;
<a href="#TOP">top</a>;
<a href="#ADDERGLOBAL">next</a>
>
</p>
<hr>
<h3><a id="ADDERGLOBAL">Scribble global protocol</a></h3>
<p>
Below we give a Scribble specification of the <a href="#ADDEROVER">Adder</a> application protocol.
</p>
<p>
This code is available from the Scribble <a href="https://github.com/scribble/scribble-java">GitHub</a> repository:
</p><ul>
<li>
<a href="https://github.com/scribble/scribble-java/blob/master/scribble-demos/scrib/tutorial/src/tutorial/adder/Adder.scr"><span class="code">https://github.com/scribble/scribble-java/blob/master/scribble-demos/scrib/tutorial/src/tutorial/adder/Adder.scr</span></a>
</li>
</ul>
<p></p>
<table>
<caption align="bottom">
<tt>Adder.scr</tt>
<br><br>
Lst. 2.1: Scribble specification of the Adder application protocol.
</caption>
<tbody><tr><td> </td><td>
<pre class="prettyprint linenums lang-scribble"><ol class="linenums"><li class="L0"><span class="kwd">module</span><span class="pln"> tutorial</span><span class="pun">.</span><span class="pln">adder</span><span class="pun">.</span><span class="typ">Adder</span><span class="pun">;</span></li><li class="L1"><span class="pln"> </span></li><li class="L2"><span class="kwd">type</span><span class="pln"> </span><span class="str"><java></span><span class="pln"> </span><span class="str">"java.lang.Integer"</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">"rt.jar"</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> </span><span class="typ">Int</span><span class="pun">;</span></li><li class="L3"><span class="pln"> </span></li><li class="L4"><span class="kwd">global</span><span class="pln"> </span><span class="kwd">protocol</span><span class="pln"> </span><span class="typ">Adder</span><span class="pun">(</span><span class="kwd">role</span><span class="pln"> C</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">role</span><span class="pln"> S</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></li><li class="L5"><span class="pln"> </span><span class="kwd">choice</span><span class="pln"> </span><span class="kwd">at</span><span class="pln"> C </span><span class="pun">{</span></li><li class="L6"><span class="pln"> </span><span class="typ">Add</span><span class="pun">(</span><span class="typ">Int</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Int</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> C </span><span class="kwd">to</span><span class="pln"> S</span><span class="pun">;</span></li><li class="L7"><span class="pln"> </span><span class="typ">Res</span><span class="pun">(</span><span class="typ">Int</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> S </span><span class="kwd">to</span><span class="pln"> C</span><span class="pun">;</span></li><li class="L8"><span class="pln"> </span><span class="kwd">do</span><span class="pln"> </span><span class="typ">Adder</span><span class="pun">(</span><span class="pln">C</span><span class="pun">,</span><span class="pln"> S</span><span class="pun">);</span></li><li class="L9"><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> </span><span class="pun">{</span></li><li class="L0"><span class="pln"> </span><span class="typ">Bye</span><span class="pun">()</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> C </span><span class="kwd">to</span><span class="pln"> S</span><span class="pun">;</span></li><li class="L1"><span class="pln"> </span><span class="pun">}</span></li><li class="L2"><span class="pun">}</span></li></ol></pre>
</td></tr>
</tbody></table>
<p>
We use the above example to give a brief overiew of Scribble syntax. The links give further details of each element.
</p><ul>
<li>
The <a href="#SCRIBMODULE"><em>payload type declaration</em></a>
<pre class="code"> type <java> "java.lang.Integer" from "rt.jar" as Int;
</pre>
declares <span class="code">java.lang.Integer</span> as a Scribble payload type, <span class="code">Int</span>, for use in this Scribble module.
</li>
<li>
In the <a href="#SCRIBGLOBAL"><em>global protocol declaration</em></a>:
<pre class="code"> global protocol Adder(role C, role S) { ... }
</pre>
<ul>
<li>
<span class="code">Adder</span> is the name of the protocol;
</li>
<li>
<span class="code">C</span> and <span class="code">S</span> are the two participants in this protocol.
</li>
</ul>
</li>
<li>
The <a href="#SCRIBCHOICE"><span class="code">choice</span></a> statement
<pre class="code"> choice at C { ... } or { ... }
</pre>
states that the protocol proceeds according to <em>one</em> of the <span class="code">or</span>-separated blocks.
<ul>
<li>
In this example, <span class="code">C</span> is the <em>master</em> role that decides which block to follow.
</li>
<li>
<span class="code">C</span> communicates its decision to the <em>slave</em> <span class="code">S</span> by the message passing in each case.
</li>
</ul>
</li>
<li>
The <a href="#SCRIBSIG"><em>message signature</em></a>
<pre class="code"> Add(Int, Int)
</pre>
specifies:
<ul>
<li>
<span class="code">Add</span> is the <em>operator</em> (<em>i.e.</em>, a label or header that identifies the message);
</li>
<li>
<span class="code">(Int, Int)</span> is a <em>payload</em> of two <span class="code">Int</span>s.
</li>
</ul>
</li>
<li>
In the <a href="#SCRIBINTERACT"><em>message passing</em></a> statement:
<pre class="code"> Add(Int, Int) from C to S;
</pre>
<ul>
<li>
<span class="code">C</span> is the <em>sender</em>, which asynchronously dispatches the specified message (<em>i.e.</em>, without blocking);
</li>
<li>
<span class="code">C</span> is the <em>receiver</em>, which blocks until the message is received.
</li>
</ul>
</li>
<li>
The <a href="#SCRIBDO"><span class="code">do</span></a> statement
<pre class="code"> do Adder(C, S);
</pre>
allows a protocol to be defined in terms of other <em>(sub)protocols</em>. It is also used for <em>recursive</em> protocol definitions, as for <span class="code">Adder</span> in this example.
</li>
</ul>
<p></p>
<p>
<
<a href="#ADDEROVER">prev</a>;
<a href="#TOP">top</a>;
<a href="#ADDERCL">next</a>
>
</p>
<hr>
<h4><a id="ADDERCL">Running the Scribble-Java command line tool</a></h4>
<p>
The command given below assumes:
</p><ul>
<li>
The <span class="code">scribblec.sh</span> script is installed at the current directory.
<ul>
<li>
(See <a href="#SCRIBBUILD">here</a> for instructions on building and installing Scribble-Java.)
</li>
</ul>
</li>
<li>
<span class="code">Adder.scr</span> is located under the current directory at:
<pre class="code"> scribble-demos/scrib/tutorial/src/tutorial/adder/Adder.scr
</pre>
(As found in the Scribble-Java <a href="https://github.com/scribble/scribble-java/blob/master/scribble-demos/scrib/tutorial/src/tutorial/adder/Adder.scr">GitHub</a> repository.)
</li>
</ul>
<p></p>
<p>
In a Linux, Cygwin/Windows, or Mac OS terminal, the command is:
</p><pre class="code"> ./scribblec.sh scribble-demos/scrib/tutorial/src/tutorial/adder/Adder.scr -api Adder C -d scribble-demos/scrib/tutorial/src/
</pre>
<p></p>
<p>
The above command performs the following tasks.
</p><ul>
<li>
Checks that the Scribble module in <span class="code">Adder.scr</span> is a valid Scribble module -- this means that every global protocol (<em>i.e.</em>, <span class="code">Adder</span>) in the module must be a valid Scribble protocol (which it is).
</li>
<li>
Generates the Java Endpoint API for implementing the <span class="code">C</span> endpoint of the <span class="code">Adder</span> protocol. The output is written to the directory specified by the <span class="code">-d</span> argument.
</li>
</ul>
<p></p>
<p>
See <a href="#SCRIBCL">here</a> for more information on using the command line tool.
</p>
<p>
<
<a href="#ADDERGLOBAL">prev</a>;
<a href="#TOP">top</a>;
<a href="#ADDERCLIENT">next</a>
>
</p>
<hr>
<h3><a id="ADDERCLIENT">An Adder client</a></h3>
<p>
We use the Endpoint API generated in the <a href="#ADDERCL">previous step</a> to summarise the key elements behind Scribble Endpoint APIs.
</p><ul>
<li>
The Endpoint API is generated from a <a href="#CLIENTFSM">finite state machine</a> (FSM) representation of the I/O actions to be performed in the protocol by the target role.
</li>
<li>
An Endpoint API comprises a <a href="#CLIENTAPI">family of Java classes</a>
that represent the states of the protocol relevant to the target role,
with methods for the I/O actions permitted in each state.
</li>
</ul>
<p></p>
<p>
We then give an example <a href="#CLIENTIMPLE">implementation</a> using the above Endpoint API.
</p>
<p>
<
<a href="#ADDERCL">prev</a>;
<a href="#TOP">top</a>;
<a href="#CLIENTFSM">next</a>
>
</p>
<hr>
<h4><a id="CLIENTFSM">Endpoint FSM</a></h4>
<p>
Below is the <em>endpoint finite state machine</em> (FSM) for the <span class="code">C</span> role in <a href="#ADDERGLOBAL"><span class="code">Adder</span></a>.
</p>
<table>
<caption align="bottom">
Fig. 2.3.1: The FSM for <span class="code">C</span> in <span class="code">Adder</span>.
</caption>
<tbody><tr>
<td>
<img src="user/Scribble-Java-tutorial_files/fsm-Adder-C.png" ,="" width="250">
</td>
</tr>
</tbody></table>
<p>
</p><ul>
<li>
State <span class="code">1</span> is the <em>initial state</em>.
</li>
<li>
State <span class="code">3</span> is the <em>terminal state</em>.
</li>
<li>
The notation <span class="code">S!Add(Int, Int)</span> denotes the action by <span class="code">C</span> to <em>send</em> <span class="code">S</span>
a message with the specified <a href="#SCRIBSIG">signature</a>.
<!--an <span class="code">Add</span> message with a payload of two <span class="code">Int</span>s.-->
</li>
<li>
The directed edge labelled by the above action means that performing this action in state <span class="code">1</span> transitions <span class="code">C</span> from to state <span class="code">2</span>.
</li>
<li>
The notation <span class="code">S?Res(Int)</span> means <span class="code">C</span> <em>receives</em> from <span class="code">S</span>
a message of the specified <a href="#SCRIBSIG">signature</a>.
<!--a <span class="code">Res</span> message with a payload of one <span class="code">Int</span>.-->
</li>
</ul>
<p></p>
<p>
<
<a href="#ADDERCLIENT">prev</a>;
<a href="#TOP">top</a>;
<a href="#CLIENTAPI">next</a>
>
</p>
<hr>
<h4><a id="CLIENTAPI">Endpoint API overview</a></h4>
<p>
So that they may be used as types in Java, Scribble generates the various role and operator names in the source protocol as <em>singleton types</em>. For the <span class="code">Adder</span> protocol, Scribble generates the following singleton types as API constants:
</p><div class="boxed">
<table>
<tbody><tr>
<td>
Roles
</td>
<td>
<span class="code">C</span>,
<span class="code">S</span>
</td>
</tr>
<tr>
<td>
Operators
</td>
<td>
<span class="code">Add</span>,
<span class="code">Res</span>,
<span class="code">Bye</span>
</td>
</tr>
</tbody></table>
</div>
<p></p>
<p>
The API generation renders each state in the <a href="#CLIENTFSM">Endpoint FSM</a> as a Java class for a <em>state-specific</em> communication channel (which we call a <em>state channel</em> for short).
</p><ul>
<li>
By default, Scribble names the state channel classes by a state
enumeration. For example, the class for the initial state is named <span class="code">Adder_C_1</span>. The terminal state (if any) is generated as a specially named <span class="code">EndSocket</span> class.
</li>
<li>
Each class is generated with methods to perform the I/O actions permitted at the corresponding state.
</li>
<li>
The return type of each method is the class generated for the successor state of the corresponding I/O action.
</li>
</ul>
<p></p>
<p>
See <a href="https://www.doc.ic.ac.uk/%7Erhu/scribble/tutorial/javadoc/adder/tutorial/adder/Adder/Adder/channels/C/Adder_C_1.html">here</a> for an example Javadoc generated (by the standard <span class="code">javadoc</span> tool) from the Scribble-generated API for <span class="code">C</span> in <a href="#ADDERGLOBAL"><span class="code">Adder</span></a>.
</p>
<p>
The following summarises the key state channel classes and their I/O methods.
</p>
<p></p><div class="boxed">
<strong><a href="https://www.doc.ic.ac.uk/%7Erhu/scribble/tutorial/javadoc/adder/index.html"><span class="code">Adder_C_1</span></a></strong> (state <span class="code">1</span> in the <a href="#CLIENTFSM">FSM</a>).
<br><br>
<table>
<tbody><tr>
<td>
Type
</td>
<td>
Method
</td>
</tr>
<tr>
<td>
<span class="code">Adder_C_2</span>
</td>
<td>
<span class="code">send(S role, Add op, Integer arg0, Integer arg1)</span>
</td>
</tr>
<tr>
<td>
<span class="code">EndSocket</span>
</td>
<td>
<span class="code">send(S role, Bye op)</span>
</td>
</tr>
</tbody></table>
<ul>
<li>
The <span class="code">S</span>, <span class="code">Add</span> and <span class="code">Bye</span> parameter types are the generated singleton types.
</li>
<li>
The <span class="code">send</span> methods are <em>non</em>-blocking, <em>i.e.</em> they return immediately after asynchronously dispatching the specified message.
</li>
<li>
Calling a generated I/O method returns a <em>new</em> instance of the successor channel class.
</li>
</ul>
</div><p></p>
<p></p><div class="boxed">
<strong><a href="https://www.doc.ic.ac.uk/%7Erhu/scribble/tutorial/javadoc/adder/tutorial/adder/Adder/Adder/channels/C/Adder_C_2.html"><span class="code">Adder_C_2</span></a></strong> (state <span class="code">2</span> in the <a href="#CLIENTFSM">FSM</a>).
<br><br>
<table>
<tbody><tr>
<td>
Type
</td>
<td>
Method
</td>
</tr>
<tr>
<td>
<span class="code">Adder_C_1</span>
</td>
<td>
<span class="code">receive(S role, Res op, Buf<? super Integer> arg0)</span>
</td>
</tr>
</tbody></table>
<ul>
<li>
<span class="code">Buf<T></span> is a utility class in the Scribble API.
<ul>
<li>
It has a mutable field <span class="code">val</span> of type <span class="code">T</span>.
</li><li>
It has a public constructor <span class="code">Buf(T t)</span>, which initialises <span class="code">val</span> to <span class="code">t</span>.
</li>
</ul>
</li>
<li>
The <span class="code">receive</span> method blocks until the message is received. It then writes the received <span class="code">Integer</span> value to the <span class="code">val</span> field of supplied <span class="code">Buf</span> before returning a new instance of the successor channel class.
</li>
</ul>
</div><p></p>
<p></p><div class="boxed">
<strong><a href="https://www.doc.ic.ac.uk/%7Erhu/scribble/tutorial/javadoc/adder/tutorial/adder/Adder/Adder/channels/C/EndSocket.html"><span class="code">EndSocket</span></a></strong> (state <span class="code">3</span> in the <a href="#CLIENTFSM">FSM</a>) has no I/O methods.
</div><p></p>
<p>
We explain the <a href="#ADDERSERVER">Endpoint API for <span class="code">S</span></a> later. It features the API generation for <a href="#APIRECEIVE">non-unary receive</a> (<em>i.e.</em>, <em>branch</em>) states, which is not demonstrated by the API for <span class="code">C</span>.
</p>