servletada_1.3.0_7ba99a77/samples/rest/monitor.ads

 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
-----------------------------------------------------------------------
--  monitor - A simple monitor API
--  Copyright (C) 2016, 2018 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Calendar;
with Servlet.Rest.Operation;
package Monitor is

   --  Get values of the monitor.
   procedure Get_Values (Req    : in out Servlet.Rest.Request'Class;
                         Reply  : in out Servlet.Rest.Response'Class;
                         Stream : in out Servlet.Rest.Output_Stream'Class);

   --  PUT /mon/:id
   procedure Put_Value (Req    : in out Servlet.Rest.Request'Class;
                        Reply  : in out Servlet.Rest.Response'Class;
                        Stream : in out Servlet.Rest.Output_Stream'Class);

   --  Declare each REST API with a relative URI from Mon_API definition.
   --  GET /api/monitor/:id
   package API_Get_Values is new Servlet.Rest.Operation (Handler    => Get_Values'Access,
                                                         Method     => Servlet.Rest.GET,
                                                         URI        => "/mon/:id");

   --  PUT /api/monitor/:id
   package API_Put_Value is new Servlet.Rest.Operation (Handler    => Put_Value'Access,
                                                        Method     => Servlet.Rest.PUT,
                                                        URI        => "/mon/:id");

private

   MAX_VALUES  : constant Natural := 1000;
   MAX_MONITOR : constant Natural := 10;

   type Value_Array is array (Natural range <>) of Natural;

   protected type Monitor_Data is

      procedure Put (Value : in Natural);

      procedure Put (Value : in Natural; Slot : in Natural);

      function Get_Values return Value_Array;

   private
      Values      : Value_Array (1 .. MAX_VALUES) := (others => 0);
      Value_Count : Natural := 0;
      Pos         : Natural := 1;
      Slot_Size   : Duration := 10.0;
      Slot_Start  : Ada.Calendar.Time := Ada.Calendar.Clock;
   end Monitor_Data;

end Monitor;