Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.09 KB | None | 0 0
  1. /// simple SOA server using a callback for long process ending notification
  2. program Project31LongWorkServer;
  3.  
  4. uses
  5.   {$I SynDprUses.inc} // use FastMM4 on older versions of Delphi
  6.   SysUtils,
  7.   Classes,
  8.   SynCommons,
  9.   SynLog,
  10.   mORMot,
  11.   SynBidirSock,
  12.   mORMotHttpServer,
  13.   Project31LongWorkCallbackInterface in 'Project31LongWorkCallbackInterface.pas';
  14.  
  15. {$APPTYPE CONSOLE}
  16.  
  17. type
  18.   TLongWorkServiceThread = class(TThread)
  19.   protected
  20.     fCallback: ILongWorkCallback;
  21.     fWorkName: string;
  22.     procedure Execute; override;
  23.   public
  24.     constructor Create();
  25.     procedure SetCallback(const workName: string; const callback: ILongWorkCallback);
  26.   end;
  27.  
  28.   TLongWorkService = class(TInterfacedObject,ILongWorkService)
  29.   protected
  30.     fTotalWorkCount: Integer;
  31.     fThread : TLongWorkServiceThread;
  32.     procedure OnClientDisconnected(sender : TObject);
  33.   public
  34.     constructor Create();
  35.     procedure StartWork(const workName: string; const onFinish: ILongWorkCallback);
  36.     function TotalWorkCount: Integer;
  37.   end;
  38.  
  39. constructor TLongWorkService.Create();
  40. begin
  41.   inherited Create;
  42.   fThread := TLongWorkServiceThread.Create();
  43. end;
  44.  
  45. procedure TLongWorkService.OnClientDisconnected(sender : TObject);
  46. begin
  47.   fThread.SetCallback('', nil);
  48. end;
  49.  
  50. procedure TLongWorkService.StartWork(const workName: string;
  51.   const onFinish: ILongWorkCallback);
  52. begin
  53.   InterlockedIncrement(fTotalWorkCount);
  54.   fThread.SetCallback(workName, onFinish);
  55. end;
  56.  
  57. function TLongWorkService.TotalWorkCount: Integer;
  58. begin
  59.   result := fTotalWorkCount;
  60. end;
  61.  
  62. constructor TLongWorkServiceThread.Create();
  63. begin
  64.   inherited Create(false);
  65.   fCallback := nil;
  66.   fWorkName := '';
  67.   FreeOnTerminate := true;
  68. end;
  69.  
  70. procedure TLongWorkServiceThread.Execute;
  71. var tix: Int64;
  72. begin
  73.   TSQLLog.Add.Log(sllInfo,'%.Execute(%) started',[self,fWorkName]);
  74.   tix := GetTickCount64;
  75.   repeat
  76.     Sleep(1000); // some hard work
  77.     if Assigned(fCallback) then
  78.     begin
  79.       try
  80.       if Random(100)>20 then
  81.         fCallback.WorkFinished(fWorkName,GetTickCount64-tix) else
  82.         fCallback.WorkFailed(fWorkName,'expected random failure');
  83.       except
  84.       //connection error
  85.       end;
  86.     end;
  87.     TSQLLog.Add.Log(sllInfo,'%.Execute(%) notified',[self,fWorkName]);
  88.   until Terminated;
  89. end;
  90.  
  91. procedure TLongWorkServiceThread.SetCallback(const workName: string; const callback: ILongWorkCallback);
  92. begin
  93.   fCallback := callback;
  94.   fWorkName := workName;
  95. end;
  96.  
  97.  
  98. procedure Run;
  99.  
  100. var HttpServer: TSQLHttpServer;
  101.     Server: TSQLRestServerFullMemory;
  102.   WSServer: TWebSocketServerRest;
  103.   LongWorkService : TLongWorkService;
  104. begin
  105.   Server := TSQLRestServerFullMemory.CreateWithOwnModel([]);
  106.   try
  107.     Server.CreateMissingTables;
  108.     LongWorkService := TLongWorkService.Create;
  109.     Server.ServiceDefine(LongWorkService,[ILongWorkService]{,sicShared}).
  110.       ByPassAuthentication := true;
  111.     HttpServer := TSQLHttpServer.Create('8888',[Server],'+',useBidirSocket);
  112.     try
  113.      WSServer := HttpServer.WebSocketsEnable(Server,PROJECT31_TRANSMISSION_KEY);
  114.      WSServer.Settings.SetFullLog; // full verbose logs for this demo
  115.      WSServer.Settings.OnClientDisconnected := LongWorkService.OnClientDisconnected;
  116. //     WSServer.Settings.OnClientConnected := OnClientConnected;
  117.      WSServer.Settings.HeartbeatDelay := 5000;
  118.  
  119.  
  120.  
  121.       TextColor(ccLightGreen);
  122.       writeln('WebSockets Long Work Server running on localhost:8888'#13#10);
  123.       TextColor(ccWhite);
  124.       writeln('Please compile and run Project31LongWorkClient.exe'#13#10);
  125.       TextColor(ccLightGray);
  126.       writeln('Press [Enter] to quit'#13#10);
  127.       TextColor(ccCyan);
  128.       readln;
  129.     finally
  130.       HttpServer.Free;
  131.     end;
  132.   finally
  133.     Server.Free;
  134.   end;
  135. end;
  136.  
  137.  
  138. begin
  139.   with TSQLLog.Family do begin // enable logging to file and to console
  140.     Level := LOG_VERBOSE;
  141.     EchoToConsole := LOG_VERBOSE;
  142.     PerThreadLog := ptIdentifiedInOnFile;
  143.   end;
  144.   WebSocketLog := TSQLLog; // verbose log of all WebSockets activity
  145.   try
  146.     Run;
  147.   except
  148.     on E: Exception do
  149.       ConsoleShowFatalException(E);
  150.   end;
  151. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement