simple_components_4.62.0_a5c16c1d/synchronization-interprocess-process_call_service.adb

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     Synchronization.Interprocess.               Luebeck            --
--     Generic_Process_Call_Service                Spring, 2018       --
--  Implementation                                                    --
--                                Last revision :  14:52 29 Feb 2020  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Calendar;             use Ada.Calendar;
with Ada.IO_Exceptions;        use Ada.IO_Exceptions;
with Ada.Tags;                 use Ada.Tags;
with Ada.Task_Identification;  use Ada.Task_Identification;
with Strings_Edit.Floats;      use Strings_Edit.Floats;
with Strings_Edit.Integers;    use Strings_Edit.Integers;

with Ada.Strings.Fixed;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with System.Address_To_Access_Conversions;

--  with Ada.Text_IO, Synchronization.Mutexes;
--  with GNAT.Exception_Actions, GNAT.Traceback.Symbolic;

package body Synchronization.Interprocess.Process_Call_Service is

   Caller_Init  : constant String := "The caller's service is not " &
                                     "started or alredy stopped";
   Callee_Init  : constant String := "The callee's service is not " &
                                     "started or alredy stopped";
   Diff_Service : constant String := "The caller and callee belong " &
                                     "to different call services";
   Method_Init  : constant String := "Method is not initialized or " &
                                     "already finalized";
   Service_Init : constant String := "Call service is not " &
                                     "initialized or already finalized";
   Same_Process : constant String := "Caller and callee belong to " &
                                     "the same process";
--
-- No -- The unique request sequence number
--
   No : aliased Sequence_No := 0;
--
-- Main -- Environment_Task.  Unfortunately the older versions of Ada do
--         not have  it  defined  in  Ada.Task_Identification.  We  use
--         Current_Task while the package elaboration to get it.
--
   Main : Task_ID;

--     Trace_Lock   : aliased Synchronization.Mutexes.Mutex;
--     Trace_File   : Ada.Text_IO.File_Type;
--     Trace_Open   : Boolean := False;

--     function "&" (Left : String; Right : Duration) return String is
--     begin
--        return Left & Image (Float (Right), AbsSmall => -3) & "ms";
--     end "&";
--
--     function "&" (Left : String; Right : Storage_Offset) return String is
--     begin
--        return Left & Image (Integer (Right));
--     end "&";
--
--     procedure Trace_Exception_Action
--               (  Occurrence : Exception_Occurrence
--               );
--
--     procedure Trace (ID : Call_Service_ID; Text : String) is
--        use Ada.Strings.Fixed;
--        use Ada.Text_IO;
--        use GNAT.Exception_Actions;
--        Lock : Synchronization.Mutexes.Holder (Trace_Lock'Access);
--     begin
--        if not Trace_Open then
--           Register_Global_Action (Trace_Exception_Action'Access);
--           Create
--           (  Trace_File,
--              Out_File,
--              "d:\temp\test_" & Image (Integer (ID)) & ".log"
--           );
--           Trace_Open := True;
--        end if;
--        declare
--           Prefix : constant String := Image (Current_Task);
--        begin
--           Put_Line
--           (  Trace_File,
--              (  Prefix
--              &  "| "
--              &  (  (  (  Character'Pos (Prefix (Prefix'First))
--                       +  Character'Pos (Prefix (Prefix'Last))
--                       )
--                    mod
--                       5
--                    )
--                 *  "   "
--                 )
--              &  Text
--           )  );
--        end;
--        Flush (Trace_File);
--     end Trace;
--
--     procedure Trace_Exception_Action
--               (  Occurrence : Exception_Occurrence
--               )  is
--        use GNAT.Traceback.Symbolic;
--     begin
--        Trace
--        (  0,
--           "++! " & Exception_Information (Occurrence)
--        );
--     end Trace_Exception_Action;

   function "=" (Left, Right : Request_Header) return Boolean is
   begin
      return Left.No = Right.No;
   end "=";

   procedure Enumerate
             (  Stream : access Root_Stream_Type'Class;
                Object : Call_Service
             )  is
   begin
      Enumerate (Stream, Abstract_Shared_Object (Object));
      Event'Write (Stream, Object.Requests.Not_Full);
      Event'Write (Stream, Object.Requests.Not_Empty);
      Mutex'Write (Stream, Object.Requests.Lock);
      Universal_FIFO'Write (Stream, Object.Queue);
      Universal_Stream'Write (Stream, Object.Requests.Stream);
      Event'Write (Stream, Object.Responses.Not_Full);
      Event'Write (Stream, Object.Responses.Not_Empty);
      Mutex'Write (Stream, Object.Responses.Lock);
      Universal_Stream'Write (Stream, Object.Responses.Stream);
      Enumerate (Stream, Object.Epilogue);
      Call_Service'Class (Object.Self.all).Total_Size :=
         (  Get_Offset (Object.Responses.Stream)
         -  Get_Offset (Object)
         +  Get_Size   (Object)
         );
   end Enumerate;

   procedure Finalize (Lock : in out Lock_Holder) is
   begin
      if Lock.Owner then
--           Trace
--           (  Get_ID (Lock.Stream.Service.all),
--              (  "releasing results mutex of "
--              &  Get_Offset (Lock.Stream.Service.Responses.Stream)
--           )  );
         Lock.Owner := False;
         Release (Lock.Stream.Service.Responses.Lock);
      end if;
   end Finalize;

   procedure Read_Response_Header
             (  Service : in out Call_Service'Class;
                Header  : in out Request_Header;
                Started : Time;
                Timeout : Duration
             )  is
       Empty   : Boolean;
       Current : Request_Header;
   begin
      loop
--           Trace
--           (  Get_ID (Service),
--              (  "wait for results "
--              &  (Timeout - (Clock - Started))
--              &  " "
--              &  Image (Header)
--              &  " from "
--              &  Get_Offset (Service.Responses.Stream)
--           )  );
         select
            Service.Response.Wait_For_Results (Header, Empty);
         or delay Timeout - (Clock - Started);
            Raise_Exception
            (  Timeout_Error'Identity,
               "Timed out awaiting for the response"
            );
         end select;
         exit when not Empty; -- Have header read by another task
         --
         -- Reading the header
         --
--           Trace
--           (  Get_ID (Service),
--              (  "wait for response header "
--              &  (Timeout - (Clock - Started))
--              &  " "
--              &  Image (Header)
--              &  " from "
--              &  Get_Offset (Service.Responses.Stream)
--           )  );
         Wait_For_Not_Empty
         (  Service.Responses.Stream,
            Timeout - (Clock - Started),
            Empty
         );
         if Empty then
            Raise_Exception
            (  Timeout_Error'Identity,
               "No response from the call server"
            );
         end if;
         begin
            Request_Header'Read
            (  Service.Responses.Stream'Access,
               Current
            );
            Empty := False;
         exception
            when Timeout_Error =>
               Raise_Exception
               (  Timeout_Error'Identity,
                  "Response header reading timeout"
               );
         end;
--           Trace
--           (  Get_ID (Service),
--              (  "response "
--              &  Image (Current.From)
--              &  "->"
--              &  Image (Get_ID (Service))
--              &  " header "
--              &  Image (Current)
--              &  " from "
--              &  Get_Offset (Service.Responses.Stream)
--           )  );
         if Current.Method not in Service.Methods'Range then
            Raise_Exception
            (  Data_Error'Identity,
               "Invalid method in the response header"
            );
         end if;
         if Header = Current then -- The header is of the response to us
            Header.Mode := Current.Mode;
            return;
         end if;
          -- The header is for another task
--           Trace (Get_ID (Service), "pass response to another task");
         Service.Response.Accept_Results (Current);
      end loop;
   exception
      when others =>
         Service.Response.Cancel;
         Close (Service.Requests.Stream);
         raise;
   end Read_Response_Header;

   procedure Generic_Complete
             (  Method : in out Abstract_Method'Class;
                Caller : in out Call_Service'Class;
                No     : Sequence_No
             )  is
      Results : Results_Stream (Caller'Access);
   begin
      Results.Header.Mode   := Success_Response;
      Results.Header.Method := Get_ID (Method);
      Results.Header.From   := Get_ID (Get_Service (Method).all);
      Results.Header.No     := No;
--                            Trace
--                            (  Results.Header.From,
--                               (  "execute "
--                               &  Image (Get_ID (Caller))
--                               &  "->"
--                               &  Image (Results.Header.From)
--                               &  " from "
--                               &  Get_Offset (Caller.Responses.Stream)
--                            )  );
      Complete
      (  Method     => Method,
         Results    => Results,
         Caller     => Caller,
         No         => No
      );
   exception
      when Error : others =>
         declare
            Stream : Universal_Stream renames
                     Caller.Responses.Stream;
            Lock   : Holder (Caller.Responses.Lock'Access);
         begin
            Results.Header.Mode := Failure_Response;
            Request_Header'Write (Stream'Access, Results.Header);
            Exception_Id'Write
            (  Stream'Access,
               Exception_Identity (Error)
            );
            String'Output (Stream'Access, Exception_Message (Error));
         exception
            when others =>
               null;
         end;
   end Generic_Complete;

   function Generic_Function_Call
            (  Method  : Abstract_Method'Class;
               Callee  : Call_Service'Class;
               Timeout : Duration := Duration'Last
            )  return Result_Type is
   begin
      if Method.Service = null or else Method.Index = 0 then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif (  Method.Service.Data = null
            or else
               Method.Service.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Caller_Init);
      elsif (  Callee.Data = null
            or else
               Callee.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Callee_Init);
      elsif Method.Service.Parent /= Callee.Parent then
         Raise_Exception (Use_Error'Identity, Diff_Service);
      elsif Method.Service.Data.Index = Callee.Data.Index then
         Raise_Exception (Use_Error'Identity, Same_Process);
      end if;
      declare
         Caller  : Call_Service'Class renames Method.Service.all;
         Target  : Call_Service'Class renames
                   Method.Service.Servers (Callee.Data.Index).all;
         Started : constant Time  := Clock;
         Header  : Request_Header :=
                   (  Mode   => Synchronous_Request,
                      Method => Method.Index,
                      From   => Caller.Data.Index,
                      No     => Initiate (Caller'Access)
                   );
      begin
--           Trace
--           (  Get_ID (Caller),
--              (  "do function call "
--              &  Image (Get_ID (Caller))
--              &  "->"
--              &  Image (Get_ID (Target))
--           )  );
         Seize (Target.Requests.Lock, Timeout);
         begin
--              Trace
--              (  Get_ID (Caller),
--                 (  "queue header "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " "
--                 &  Image (Header)
--                 &  " to "
--                 &  Get_Offset (Target.Queue)
--              )  );
            if Is_Closed (Target.Requests.Stream) then
               Raise_Exception (Status_Error'Identity, Callee_Init);
            end if;
            Put (Target.Queue, Header, Timeout - (Clock - Started));
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Send_Parameters (Target.Requests.Stream);
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters done "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Release (Target.Requests.Lock);
         exception
            when End_Error =>
               Release (Target.Requests.Lock);
               Raise_Exception (Status_Error'Identity, Caller_Init);
            when others =>
               Release (Target.Requests.Lock);
               raise;
         end;
         begin
            Read_Response_Header (Caller, Header, Started, Timeout);
         exception
            when End_Error =>
               Raise_Exception (Status_Error'Identity, Callee_Init);
         end;
         begin
            case Header.Mode is
               when Failure_Response => -- Error
--                    Trace
--                    (  Get_ID (Caller),
--                       (  "reading exception occurrence "
--                       &  Image (Get_ID (Caller))
--                       &  "->"
--                       &  Image (Get_ID (Target))
--                       &  " from "
--                       &  Get_Offset (Caller.Responses.Stream)
--                    )  );
                  declare -- Receive exception ID and message
                     Responses : Universal_Stream renames
                                 Caller.Responses.Stream;
                     ID        : constant Exception_ID :=
                                 Exception_ID'Input (Responses'Access);
                     Message   : constant String :=
                                 String'Input (Responses'Access);
                  begin
                     Raise_Exception (ID, Message);
                  end;
               when Success_Response => -- Success, receive results
--                    Trace
--                    (  Get_ID (Caller),
--                       (  "reading result "
--                       &  Image (Get_ID (Caller))
--                       &  "->"
--                       &  Image (Get_ID (Target))
--                       &  " from "
--                       &  Get_Offset (Caller.Responses.Stream)
--                    )  );
                  declare
                      Result : constant Result_Type :=
                               Result_Type'Input
                               (  Caller.Responses.Stream'Access
                               );
                  begin
--                       Trace
--                       (  Get_ID (Caller),
--                          (  "result read "
--                          &  Image (Get_ID (Caller))
--                          &  "->"
--                          &  Image (Get_ID (Target))
--                          &  " from "
--                          &  Get_Offset (Caller.Responses.Stream)
--                       )  );
                     Caller.Response.Complete;
                     return Result;
                  end;
               when others => -- Cancel
                  Raise_Exception
                  (  Status_Error'Identity,
                     "Call canceled by the server"
                  );
            end case;
         exception
            when End_Error =>
               Caller.Response.Complete;
               Raise_Exception (Status_Error'Identity, Callee_Init);
            when others =>
               Caller.Response.Complete;
               raise;
         end;
      end;
   end Generic_Function_Call;

   procedure Generic_Post
             (  Method  : Abstract_Method'Class;
                Callee  : Call_Service'Class;
                Timeout : Duration := Duration'Last
             )  is
   begin
      if Method.Service = null or else Method.Index = 0 then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif (  Method.Service.Data = null
            or else
               Method.Service.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Caller_Init);
      elsif (  Callee.Data = null
            or else
               Callee.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Callee_Init);
      elsif Method.Service.Parent /= Callee.Parent then
         Raise_Exception (Use_Error'Identity, Diff_Service);
      elsif Method.Service.Data.Index = Callee.Data.Index then
         Raise_Exception (Use_Error'Identity, Same_Process);
      end if;
      declare
         Caller  : Call_Service'Class renames Method.Service.all;
         Target  : Call_Service'Class renames
                   Method.Service.Servers (Callee.Data.Index).all;
         Started : constant Time  := Clock;
         Header  : constant Request_Header :=
                            (  Mode   => Synchronous_Request,
                               Method => Method.Index,
                               From   => Caller.Data.Index,
                               No     => Initiate (Caller'Access)
                            );
      begin
--           Trace
--           (  Get_ID (Caller),
--              (  "do asynchronous call "
--              &  Image (Get_ID (Caller))
--              &  " ->"
--              &  Image (Get_ID (Target))
--           )  );
         Seize (Target.Requests.Lock, Timeout);
         begin
--              Trace
--              (  Get_ID (Caller),
--                 (  "queue header "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " "
--                 &  Image (Header)
--                 &  " to "
--                 &  Get_Offset (Target.Queue)
--              )  );
            if Is_Closed (Target.Requests.Stream) then
               Raise_Exception (Status_Error'Identity, Callee_Init);
            end if;
            Put (Target.Queue, Header, Timeout - (Clock - Started));
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Send_Parameters (Target.Requests.Stream);
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters done "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Release (Target.Requests.Lock);
         exception
            when End_Error =>
               Release (Target.Requests.Lock);
               Raise_Exception (Status_Error'Identity, Caller_Init);
            when others =>
               Release (Target.Requests.Lock);
               raise;
         end;
      end;
   end Generic_Post;

   procedure Generic_Procedure_Call
             (  Method  : Abstract_Method'Class;
                Callee  : Call_Service'Class;
                Timeout : Duration := Duration'Last
             )  is
   begin
      if Method.Service = null or else Method.Index = 0 then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif (  Method.Service.Data = null
            or else
               Method.Service.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Caller_Init);
      elsif (  Callee.Data = null
            or else
               Callee.Data.Index not in 1..Last_ID
            )  then
         Raise_Exception (Status_Error'Identity, Callee_Init);
      elsif Method.Service.Parent /= Callee.Parent then
         Raise_Exception (Use_Error'Identity, Diff_Service);
      elsif Method.Service.Data.Index = Callee.Data.Index then
         Raise_Exception (Use_Error'Identity, Same_Process);
      end if;
      declare
         Caller  : Call_Service'Class renames Method.Service.all;
         Target  : Call_Service'Class renames
                   Method.Service.Servers (Callee.Data.Index).all;
         Started : constant Time  := Clock;
         Header  : Request_Header :=
                   (  Mode   => Synchronous_Request,
                      Method => Method.Index,
                      From   => Caller.Data.Index,
                      No     => Initiate (Caller'Access)
                   );
      begin
--           Trace
--           (  Get_ID (Caller),
--              (  "do procedure call "
--              &  Image (Get_ID (Caller))
--              &  "->"
--              &  Image (Get_ID (Target))
--           )  );
         Seize (Target.Requests.Lock, Timeout);
         begin
--              Trace
--              (  Get_ID (Caller),
--                 (  "queue header "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " "
--                 &  Image (Header)
--                 &  " to "
--                 &  Get_Offset (Target.Queue)
--              )  );
            if Is_Closed (Target.Requests.Stream) then
               Raise_Exception (Status_Error'Identity, Callee_Init);
            end if;
            Put (Target.Queue, Header, Timeout - (Clock - Started));
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Send_Parameters (Target.Requests.Stream);
--              Trace
--              (  Get_ID (Caller),
--                 (  "write parameters done "
--                 &  Image (Get_ID (Caller))
--                 &  "->"
--                 &  Image (Get_ID (Target))
--                 &  " to "
--                 &  Get_Offset (Target.Requests.Stream)
--              )  );
            Release (Target.Requests.Lock);
         exception
            when End_Error =>
               Release (Target.Requests.Lock);
               Raise_Exception (Status_Error'Identity, Caller_Init);
            when others =>
               Release (Target.Requests.Lock);
               raise;
         end;
         begin
            Read_Response_Header (Caller, Header, Started, Timeout);
         exception
            when End_Error =>
               Raise_Exception (Status_Error'Identity, Callee_Init);
         end;
         begin
            if Header.Mode = Failure_Response then -- Error
--                 Trace
--                 (  Get_ID (Caller),
--                    (  "reading exception occurrence "
--                    &  Image (Get_ID (Caller))
--                    &  "->"
--                    &  Image (Get_ID (Target))
--                 )  );
               declare -- Receive exception ID and message
                  Responses : Universal_Stream renames
                              Caller.Responses.Stream;
                  ID        : constant Exception_ID :=
                                 Exception_ID'Input (Responses'Access);
                  Message   : constant String :=
                                 String'Input (Responses'Access);
               begin
                  Raise_Exception (ID, Message);
               end;
            else -- Success
--                 Trace
--                 (  Get_ID (Caller),
--                    (  "reading results "
--                    &  Image (Get_ID (Caller))
--                    &  "->"
--                    &  Image (Get_ID (Target))
--                    &  " from "
--                    &  Get_Offset (Caller.Responses.Stream)
--                 )  );
               Receive_Results (Caller.Responses.Stream);
               Caller.Response.Complete;
               return;
            end if;
         exception
            when End_Error =>
               Caller.Response.Complete;
               Raise_Exception (Status_Error'Identity, Callee_Init);
            when others =>
               Caller.Response.Complete;
               raise;
         end;
      end;
   end Generic_Procedure_Call;

   function Get_ID (Service : Call_Service) return Call_Service_ID is
   begin
      if Service.Data = null then
         Raise_Exception (Status_Error'Identity, Service_Init);
      else
         return Service.Data.Index;
      end if;
   end Get_ID;

   function Get_ID (Method : Abstract_Method) return Method_ID is
   begin
      if Method.Index = Null_Method_ID then
         Raise_Exception (Status_Error'Identity, Method_Init);
      else
         return Method.Index;
      end if;
   end Get_ID;

   function Get_Process_ID (Service : Call_Service) return Process_ID is
   begin
      if Service.Data = null then
         Raise_Exception (Status_Error'Identity, Service_Init);
      else
         return Service.Data.Process;
      end if;
   end Get_Process_ID;

   function Get_Service
            (  Method : Abstract_Method;
               ID     : Call_Service_ID
            )  return Call_Service_Ptr is
   begin
      if Method.Service = null then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif Method.Service.Data = null then
         Raise_Exception (Status_Error'Identity, Service_Init);
      elsif ID not in Method.Service.Servers'Range then
         Raise_Exception
         (  Constraint_Error'Identity,
            "Invalid call service ID"
         );
      else
         return Method.Service.Servers (ID);
      end if;
   end Get_Service;

   function Get_Service
            (  Method : Abstract_Method
            )  return Call_Service_Ptr is
   begin
      if Method.Service = null then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif Method.Service.Data = null then
         Raise_Exception (Status_Error'Identity, Service_Init);
      else
         return Method.Service;
      end if;
   end Get_Service;

   function Get_Service_ID (Method : Abstract_Method)
      return Call_Service_ID is
   begin
      if Method.Service = null then
         Raise_Exception (Status_Error'Identity, Method_Init);
      elsif Method.Service.Data = null then
         Raise_Exception (Status_Error'Identity, Service_Init);
      else
         return Method.Service.Data.Index;
      end if;
   end Get_Service_ID;

   function Get_Size (Object : Call_Service_Stop)
      return Storage_Count is
   begin
      return 0;
   end Get_Size;

   function Get_Size (Object : Call_Service) return Storage_Count is
   begin
      return Round (Service_Data'Max_Size_In_Storage_Elements);
   end Get_Size;

   function Get_Size (Object : Abstract_Method) return Storage_Count is
   begin
      return 0;
   end Get_Size;

   function Image (ID : Call_Service_ID) return String is
      Result : constant String := Call_Service_ID'Image (ID);
   begin
      return Result (Result'First + 1..Result'Last);
   end Image;

   function Image (ID : Method_ID) return String is
      Result : constant String := Method_ID'Image (ID);
   begin
      return Result (Result'First + 1..Result'Last);
   end Image;

   function Image
            (  Request   : Request_Header;
               Show_Mode : Boolean := True
            )  return String is
      Text : constant String := Sequence_No'Image (Request.No);
   begin
      if Show_Mode then
         return
         (  Request_Type'Image (Request.Mode)
         &  "->"
         &  Image (Request.Method)
         &  ":"
         &  Text (Text'First + 1..Text'Last)
         );
      else
         return
         (  Image (Request.Method)
         &  ":"
         &  Text (Text'First + 1..Text'Last)
         );
      end if;
   end Image;

   function Initiate (Caller : access Call_Service)
      return Sequence_No is
      function Increment
               (  Accumulator : access Sequence_No;
                  Value       : Sequence_No
               )  return Sequence_No;
      pragma Import
             (  Intrinsic,
                Increment,
                "__sync_add_and_fetch_8"
             );
   begin
      return Increment (No'Access, 1);
   end Initiate;

   function Is_Server (Service : Call_Service) return Boolean is
   begin
      return Service.Mode = Server_Mode;
   end Is_Server;

   function Is_Synchronous (Method : Abstract_Method) return Boolean is
   begin
      return True;
   end Is_Synchronous;

   procedure Map
             (  Object   : in out Call_Service_Stop;
                Shared   : in out Abstract_Shared_Environment'Class;
                Location : System.Address;
                Size     : Storage_Count;
                Owner    : Boolean
             )  is
   begin
      null;
   end Map;

   procedure Map
             (  Object   : in out Call_Service;
                Shared   : in out Abstract_Shared_Environment'Class;
                Location : System.Address;
                Size     : Storage_Count;
                Owner    : Boolean
             )  is
      package Mapper is new Generic_Memory_Mapper (Service_Data);
   begin
      Object.Data := Mapper.Map (Location, Owner).all'Unchecked_Access;
      Object.Parent := Shared'Unchecked_Access;
      case Object.Mode is
         when Server_Mode =>
            Set_Mode (Object.Queue,            Out_End);
            Set_Mode (Object.Requests.Stream,  In_End);
            Set_Mode (Object.Responses.Stream, In_End);
            declare -- Enumerate methods and servers
               This        : Abstract_Shared_Object_Ptr := Shared.First;
               Method_No   : Method_ID       := 0;
               Server_No   : Call_Service_ID := 0;
               Server_ID   : Call_Service_ID := 0;
               Done        : Boolean := False;    -- No more methods
               Last_Method : Abstract_Method_Ptr; -- Last method
               Last_Server : Call_Service_Ptr;    -- Last server
            begin
               while This /= null loop
                  if This = Object'Unchecked_Access then
                     Server_No   := Server_No + 1;
                     Server_ID   := Server_No;
                     Done        := True;
                     Object.Prev := Last_Server;
                     Last_Server := Object'Unchecked_Access;
                  elsif This.all in Call_Service'Class then
                     declare
                        Service : Call_Service'Class renames
                                  Call_Service'Class (This.all);
                     begin
                        Server_No    := Server_No + 1;
                        Service.Prev := Last_Server;
                        Last_Server  := Service'Unchecked_Access;
                     end;
                  elsif (  not Done
                        and then
                           This.all in Abstract_Method'Class
                        )  then
                     declare
                        Method : Abstract_Method'Class renames
                                 Abstract_Method'Class (This.all);
                     begin
                        if Method.Service = null then -- A free method
                           Method_No      := Method_No + 1;
                           Method.Index   := Method_No;
                           Method.Service := Object'Unchecked_Access;
                           Method.Prev    := Last_Method;
                           Last_Method    := Method'Unchecked_Access;
                        end if;
                     end;
                  end if;
                  This := This.Next;
               end loop;
               Object.Methods := new Method_Array (1..Method_No);
               while Method_No > 0 and then Last_Method /= null loop
                  Object.Methods (Method_No) := Last_Method;
                  Last_Method := Last_Method.Prev;
                  Method_No   := Method_No - 1;
               end loop;
               Object.Servers := new Call_Service_Array (1..Server_No);
               while Server_No > 0 and then Last_Server /= null loop
                  Object.Servers (Server_No) := Last_Server;
                  Last_Server := Last_Server.Prev;
                  Server_No   := Server_No - 1;
               end loop;
               Object.Data.Service := Object'Unchecked_Access;
               if (  Compare_And_Swap
                     (  Object.Data.Index'Access,
                        0,
                        Server_ID
                     )
                  or else
                     (  Compare_And_Swap
                        (  Object.Data.Index'Access,
                           Server_ID,
                           Server_ID
                     )  )
                  or else
                     (  Compare_And_Swap
                        (  Object.Data.Index'Access,
                           Server_ID + Last_ID - 1,
                           Server_ID
                  )  )  )
               then
                  Object.Server_ID    := Server_ID;
                  Object.Data.Process := Get_Process_ID;
                  Object.Data.Service := Object'Unchecked_Access;
               else
                  Raise_Exception
                  (  Mode_Error'Identity,
                     (  "The service (no."
                     &  Image (Server_ID)
                     &  ") is already assigned to another "
                     &  "process (no."
                     &  Image (Object.Data.Index)
                     &  ")"
                  )  );
               end if;
            end;
         when Client_Mode =>
            Set_Mode (Object.Queue,            Multiplexed_In_End);
            Set_Mode (Object.Requests.Stream,  Multiplexed_Out_End);
            Set_Mode (Object.Responses.Stream, Multiplexed_Out_End);
      end case;
   end Map;

   procedure Map
             (  Object   : in out Abstract_Method;
                Shared   : in out Abstract_Shared_Environment'Class;
                Location : System.Address;
                Size     : Storage_Count;
                Owner    : Boolean
             )  is
   begin
      null;
   end Map;

   procedure On_Start (Service : in out Call_Service) is
   begin
      null;
   end On_Start;

   procedure Read
             (  Stream  : access Root_Stream_Type'Class;
                Request : out Request_Header
             )  is
      subtype Four_Elements is Stream_Element_Array (1..4);
      function To_32 is
         new Ada.Unchecked_Conversion (Four_Elements, Unsigned_32);
      subtype Eight_Elements is Stream_Element_Array (1..8);
      function To_64 is
         new Ada.Unchecked_Conversion (Eight_Elements, Unsigned_64);
      Data : Stream_Element_Array (1..17);
      Last : Stream_Element_Offset;
   begin
      Read (Stream.all, Data, Last);
      if Last /= 17 then
         raise End_Error;
      end if;
      Request.Mode   := Request_Type'Val (Data (1));
      Request.Method := Method_ID (To_32 (Data (2..5)));
      Request.From   := Call_Service_ID (To_32 (Data (6..9)));
      Request.No     := Sequence_No (To_64 (Data (10..17)));
   end Read;

   procedure Read
             (  Results : in out Null_Stream;
                Item    : out Stream_Element_Array;
                Last    : out Stream_Element_Offset
             )  is
   begin
      Last := Item'First - 1;
   end Read;

   procedure Read
             (  Results : in out Results_Stream;
                Item    : out Stream_Element_Array;
                Last    : out Stream_Element_Offset
             )  is
   begin
      Last := Item'First - 1;
   end Read;

   procedure Set_Server (Service : in out Call_Service) is
   begin
      if Service.Data /= null then
         Raise_Exception
         (  Status_Error'Identity,
            "Call service is already initialized"
         );
      end if;
      Service.Mode := Server_Mode;
      Set_Mode (Service.Queue,            Out_End);
      Set_Mode (Service.Requests.Stream,  In_End);
      Set_Mode (Service.Responses.Stream, In_End);
   end Set_Server;

   procedure Start
             (  Object : in out Call_Service;
                Shared : in out Abstract_Shared_Environment'Class;
                Owner  : Boolean
             )  is
   begin
      case Object.Mode is
         when Server_Mode =>
            Object.Server :=
               new Call_Server (Object'Unchecked_Access);
         when Client_Mode =>
            null;
      end case;
   end Start;

   procedure Unmap
             (  Object : in out Call_Service_Stop;
                Shared : in out Abstract_Shared_Environment'Class;
                Owner  : Boolean
             )  is
      procedure Free is
         new Ada.Unchecked_Deallocation
             (  Call_Server,
                Call_Server_Ptr
             );
      procedure Free is
         new Ada.Unchecked_Deallocation
             (  Method_Array,
                Method_Array_Ptr
             );
      procedure Free is
         new Ada.Unchecked_Deallocation
             (  Call_Service_Array,
                Call_Service_Array_Ptr
             );
       This : Call_Service'Class renames Object.Parent.all;
   begin
      if This.Server /= null then
         Close (This.Requests.Stream);
         Close (This.Responses.Stream);
         Signal (This.Requests.Not_Empty);  -- Wake up the readers
         Signal (This.Responses.Not_Empty);
         if Current_Task /= Main then
            This.Server.Quit;
         end if;
         while not This.Server'Terminated loop
            delay 0.001;
         end loop;
         if Compare_And_Swap
            (  This.Data.Index'Access,
               This.Server_ID,
               0
            )
         then
            null;
         end if;
         This.Data.Process := Null_Process;
         Free (This.Server);
         Free (This.Methods);
         Free (This.Servers);
      end if;
   end Unmap;

   procedure Write
             (  Stream  : access Root_Stream_Type'Class;
                Request : Request_Header
             )  is
      subtype Four_Elements is Stream_Element_Array (1..4);
      function To_32 is
         new Ada.Unchecked_Conversion (Unsigned_32, Four_Elements);
      subtype Eight_Elements is Stream_Element_Array (1..8);
      function To_64 is
         new Ada.Unchecked_Conversion (Unsigned_64, Eight_Elements);
   begin
      Write
      (  Stream.all,
         (  Stream_Element (Request_Type'Pos (Request.Mode))
         &  To_32 (Unsigned_32 (Request.Method))
         &  To_32 (Unsigned_32 (Request.From))
         &  To_64 (Unsigned_64 (Request.No))
      )  );
   end Write;

   procedure Write
             (  Results : in out Null_Stream;
                Item    : Stream_Element_Array
             )  is
   begin
      null;
   end Write;

   procedure Write
             (  Results : in out Results_Stream;
                Item    : Stream_Element_Array
             )  is
      Service : Call_Service'Class renames Results.Service.all;
      Stream  : Universal_Stream   renames Service.Responses.Stream;
   begin
      if not Results.Lock.Owner then
--           Trace
--           (  Get_ID (Results.Service.all),
--              "taking results mutex to " & Get_Offset (Stream)
--           );
         Seize (Service.Responses.Lock, Service.Timeout);
         Results.Lock.Owner := True;
--           Trace
--           (  Get_ID (Results.Service.all),
--              (  "writing results header "
--              &  Image (Results.Header)
--              &  " to "
--              &  Get_Offset (Stream)
--           )  );
         Request_Header'Write (Stream'Access, Results.Header);
      end if;
--        Trace
--        (  Get_ID (Results.Service.all),
--           "writing results to " & Get_Offset (Stream)
--        );
      Write (Stream, Item);
--        Trace
--        (  Get_ID (Results.Service.all),
--           "results written to " & Get_Offset (Stream)
--        );
   end Write;

   protected body Response_Event is

      entry Accept_Results (Header : Request_Header) when True is
      begin
         if Exiting then
            return;
         elsif Cancel'Count > 0 then
            Raise_Exception
            (  Data_Error'Identity,
               "Canceled due to communication error"
            );
         end if;
         State   := not State;
         Pending := True;
         Result  := Header;
         requeue Accept_Results_Lounge with abort;
      end Accept_Results;

      entry Cancel when (  (  Accept_Results'Count
                           +  Incoming_Results (True)'Count
                           +  Incoming_Results (False)'Count
                           )
                        =  0
                        )  is
      begin
         Pending := False;
      end Cancel;

      entry Incoming_Results (for Current in Boolean)
            (  Header    : in out Request_Header;
               No_Header : out Boolean
            )  when (  Exiting
                    or else
                       Cancel'Count > 0
                    or else
                       not Pending
                    or else
                       Current = State
                    )  is
      begin
         if Exiting then
            Raise_Exception
            (  End_Error'Identity,
               "Process call service is exiting"
            );
         elsif Cancel'Count > 0 then
            Raise_Exception
            (  Data_Error'Identity,
               "Canceled due to communication error"
            );
         elsif Pending then -- Have a header already read
            if Header = Result then -- The expected header
               No_Header   := False;
               Header.Mode := Result.Mode;
            else -- Wait for another header
               requeue Incoming_Results (not State);
            end if;
         else -- Free to read a header
            No_Header := True;
            Pending   := True;
         end if;
      end Incoming_Results;

      entry Accept_Results_Lounge (Header : Request_Header)
         when not Pending or else Exiting is
      begin
         null;
      end Accept_Results_Lounge;

      entry Wait_For_Results
            (  Header    : in out Request_Header;
               No_Header : out Boolean
            )  when True is
      begin
         if Exiting then
            Raise_Exception
            (  End_Error'Identity,
               "Process call service is exiting"
            );
         elsif Cancel'Count > 0 then
            Raise_Exception
            (  Data_Error'Identity,
               "Canceled due to communication error"
            );
         elsif Pending then -- Have a header already read
            if Header = Result then -- The expected header
               No_Header   := False;
               Header.Mode := Result.Mode;
            else -- Wait for another header
               requeue Incoming_Results (not State);
            end if;
         else -- Free to read a header
            No_Header := True;
            Pending   := True;
         end if;
      end Wait_For_Results;

      procedure Complete is
      begin
         Pending := False;
      end Complete;

      procedure Quit is
      begin
         Exiting := True;
      end Quit;
   end Response_Event;

   task body Call_Server is
      Current : Request_Header;
      Stock   : Null_Stream;
      Empty   : Boolean;
   begin
      On_Start (Service.all);
      while Is_Callable (Main) loop
         select
            accept Quit;
            exit;
         else
            null;
         end select;
--           Trace
--           (  Get_ID (Service.all),
--              (  "wait for an incoming request to "
--              &  Get_Offset (Service.Queue)
--           )  );
         Wait_For_Not_Empty (Service.Queue, Service.Timeout, Empty);
         if not Empty then
            Current := Get (Service.Queue);
--              Trace
--              (  Get_ID (Service.all),
--                 (  "request "
--                 &  Image (Current.From)
--                 &  "->"
--                 &  Image (Get_ID (Service.all))
--                 &  " header "
--                 &  Image (Current)
--                 &  " to "
--                 &  Get_Offset (Service.Queue)
--              )  );
            declare
               Method : Abstract_Method'Class renames
                        Service.Methods (Current.Method).all;
               Caller : Call_Service'Class renames
                        Service.Servers (Current.From).all;
            begin
               if Current.Mode = Asynchronous_Request then
                  begin
                     Execute
                     (  Method     => Method,
                        Parameters => Service.Requests.Stream,
                        Results    => Stock,
                        Caller     => Caller,
                        No         => Current.No
                     );
                  exception
                     when others =>
                        null;
                  end;
               else
                  begin
                     if Is_Synchronous (Method) then
                        declare
                           Results : Results_Stream (Caller'Access);
                        begin
                           Results.Header.Mode   := Success_Response;
                           Results.Header.Method := Current.Method;
                           Results.Header.From   := Get_ID (Service.all);
                           Results.Header.No     := Current.No;
--                            Trace
--                            (  Get_ID (Service.all),
--                               (  "execute "
--                               &  Image (Get_ID (Caller))
--                               &  "->"
--                               &  Image (Get_ID (Service.all))
--                               &  " from "
--                               &  Get_Offset (Caller.Responses.Stream)
--                               &  " to "
--                               &  Get_Offset (Service.Requests.Stream)
--                            )  );
                           Execute
                           (  Method     => Method,
                              Parameters => Service.Requests.Stream,
                              Results    => Results,
                              Caller     => Caller,
                              No         => Current.No
                           );
                        end;
                     else
                        Execute
                        (  Method     => Method,
                           Parameters => Service.Requests.Stream,
                           Results    => Stock,
                           Caller     => Caller,
                           No         => Current.No
                        );
                     end if;
                  exception
                     when Error : others =>
                        declare
                           Stream : Universal_Stream renames
                                    Caller.Responses.Stream;
                           Lock   : Holder
                                    (  Caller.Responses.Lock'Access
                                    );
                        begin
                           Current.Mode := Failure_Response;
                           Current.From := Get_ID (Service.all);
                           Request_Header'Write
                           (  Stream'Access,
                              Current
                           );
                           Exception_Id'Write
                           (  Stream'Access,
                              Exception_Identity (Error)
                           );
                           String'Output
                           (  Stream'Access,
                              Exception_Message (Error)
                           );
                        exception
                           when others =>
                              null;
                        end;
                  end;
               end if;
            end;
         end if;
      end loop;
--        Trace (Get_ID (Service.all), "initiate exiting call service");
      Close (Service.Requests.Stream);
      Close (Service.Responses.Stream);
      while not Is_Empty (Service.Queue) loop
         Current := Get (Service.Queue);
--           Trace
--           (  Get_ID (Service.all),
--              (  "canceling request "
--              &  Image (Current.From)
--              &  "->"
--              &  Image (Get_ID (Service.all))
--              &  " header "
--              &  Image (Current)
--              &  " to "
--              &  Get_Offset (Service.Queue)
--           )  );
         if Current.Mode /= Asynchronous_Request then
            declare
               Caller  : Call_Service'Class renames
                         Service.Servers (Current.From).all;
               Stream  : Universal_Stream renames
                         Caller.Responses.Stream;
               Lock    : Holder (Caller.Responses.Lock'Access);
            begin
               Current.Mode := Cancel_Response;
               Current.From := Get_ID (Service.all);
               Request_Header'Write (Stream'Access, Current);
            exception
               when others =>
                  null;
            end;
         end if;
      end loop;
--        Trace (Get_ID (Service.all), "*** exiting call service");
   end Call_Server;

begin
   Main := Current_Task;
end Synchronization.Interprocess.Process_Call_Service;