302a82650102ce5e74981930fdb921c86c494aa2
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / process_pool.hh
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_
18 #define LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_
19
20 #include <glib.h>
21 #include <sys/types.h>
22
23 #include <queue>
24 #include <memory>
25 #include <vector>
26
27 #include <app_packet.hh>
28 #include <socket.hh>
29
30 #include "launchpad-process-pool/executor.hh"
31
32 namespace launchpad {
33
34 class ProcessPool : public Executor::Delegator,
35                     public Executor {
36  public:
37   class IEvent {
38    public:
39     virtual ~IEvent() = default;
40     virtual void OnRequestReceived(std::shared_ptr<AppPacket> app_packet) = 0;
41   };
42
43   explicit ProcessPool(int num_processes, IEvent* event_listener);
44   virtual ~ProcessPool();
45
46   bool IsPrepared() const;
47   pid_t Execute(std::shared_ptr<AppPacket> app_packet);
48
49  private:
50   class Process {
51    public:
52     Process(pid_t pid, int fd);
53
54     pid_t GetPid() const;
55     int Send(std::shared_ptr<AppPacket> app_packet);
56     void Kill();
57
58    private:
59     pid_t pid_;
60     std::unique_ptr<Socket> socket_;
61   };
62
63   void OnExecution() override;
64
65   void PrepareProcess();
66   int WaitForRequest(std::unique_ptr<Socket> socket);
67   void SetTimer();
68   static gboolean OnTimeout(gpointer user_data);
69
70  private:
71   int num_processes_;
72   IEvent* event_listener_;
73   int pipe_fd_[2] = { -1, -1 };
74   std::queue<std::shared_ptr<Process>> queue_;
75   guint timer_ = 0;
76 };
77
78 }  // namespace launchpad
79
80 #endif  // LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_