rpcapi.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. This file is part of the Free Component Library
  3. Unit with the server-side interface implementations of the interfaces
  4. Registers the classes.
  5. Copyright (c) 2022 by Michael Van Canneyt [email protected]
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit rpcapi;
  13. {$mode ObjFPC}{$H+}
  14. interface
  15. uses
  16. Classes, SysUtils, fprpcrtti, myapi;
  17. Type
  18. { TIntfImpl }
  19. TIntfImpl = class(TInterfacedObject, IMyInterface)
  20. public
  21. procedure SayHello;
  22. Function Echo(args : Array of string) : String;
  23. function DoSum(a,b : Integer) : integer;
  24. function Split(aLine,aSep : string) : TStringArray;
  25. function DoVarTest(var aArg: String): Boolean;
  26. end;
  27. { TIntf2Impl }
  28. TIntf2Impl = class(TInterfacedObject, IMyOtherInterface)
  29. public
  30. function Echo(args: TStringArray): String;
  31. function SayHello: string;
  32. end;
  33. Implementation
  34. { TIntf2Impl }
  35. function TIntf2Impl.Echo(args: TStringArray): String;
  36. var
  37. S : String;
  38. begin
  39. Result:='';
  40. For S in Args do
  41. begin
  42. if Result<>'' then
  43. Result:=Result+' ';
  44. Result:=Result+S;
  45. end
  46. end;
  47. function TIntf2Impl.SayHello: string;
  48. begin
  49. Result:='Hello, World!';
  50. end;
  51. procedure TIntfImpl.SayHello;
  52. begin
  53. Writeln('Hello, World!');
  54. end;
  55. function TIntfImpl.Echo(args: array of string): String;
  56. var
  57. S : String;
  58. begin
  59. Result:='';
  60. For S in Args do
  61. begin
  62. if Result<>'' then
  63. Result:=Result+' ';
  64. Result:=Result+S;
  65. end
  66. end;
  67. function TIntfImpl.DoSum(a,b : Integer) : integer;
  68. begin
  69. Result := a + b;
  70. end;
  71. function TIntfImpl.Split(aLine,aSep : string) : TStringArray;
  72. begin
  73. Result := aLine.Split(aSep);
  74. end;
  75. function TIntfImpl.DoVarTest(var aArg: String): Boolean;
  76. begin
  77. if aArg = 'Test' then begin
  78. aArg := 'Foo';
  79. Result := True;
  80. end else
  81. Result := False;
  82. end;
  83. Function GetMyInterface(Const aName : string) : IInterface;
  84. begin
  85. Result:=TIntfImpl.Create as IInterface;
  86. end;
  87. Function GetMyOtherInterface(Const aName : string) : IInterface;
  88. begin
  89. Result:=TIntf2Impl.Create as IInterface;
  90. end;
  91. initialization
  92. RTTIJSONRPCRegistry.Add(TypeInfo(IMyInterface),@GetMyInterface);
  93. RTTIJSONRPCRegistry.Add(TypeInfo(IMyOtherInterface),@GetMyOtherInterface,'Service2');
  94. end.