13 #ifndef __PROCESS_PROCESS_HPP__
14 #define __PROCESS_PROCESS_HPP__
68 void install(std::vector<Owned<FirewallRule>>&& rules);
79 const UPID&
self()
const {
return pid; }
87 std::move(event).consume(
this);
143 const std::string&
name,
144 const char* data =
nullptr,
210 typedef lambda::function<void(const UPID&, const std::string&)>
217 const std::string&
name,
220 handlers.message[
name] = handler;
226 template <
typename T>
228 const std::string&
name,
229 void (T::*method)(
const UPID&,
const std::string&))
235 lambda::bind(method, dynamic_cast<T*>(
this), lambda::_1, lambda::_2);
244 delegates[
name] = pid;
256 typedef lambda::function<Future<http::Response>(
const http::Request&)>
277 const std::string& name,
285 template <
typename T>
287 const std::string& name,
296 lambda::bind(method, dynamic_cast<T*>(
this), lambda::_1);
297 route(name, help, handler, options);
320 typedef lambda::function<Future<http::Response>(
327 const std::string& name,
328 const std::string& realm,
336 template <
typename T>
338 const std::string& name,
339 const std::string& realm,
350 lambda::bind(method, dynamic_cast<T*>(
this), lambda::_1, lambda::_2);
351 route(name, realm, help, handler, options);
369 const std::string& name,
370 const std::string& path,
378 assets[
name] = asset;
386 template <
typename T>
410 std::atomic<bool> termination = ATOMIC_VAR_INIT(
false);
413 void enqueue(
Event* event);
416 std::map<std::string, UPID> delegates;
438 RouteOptions options;
456 std::map<std::string, std::string>
types;
460 Future<http::Response> _consume(
461 const HttpEndpoint& endpoint,
462 const std::string& name,
463 const Owned<http::Request>&
request);
470 std::map<std::string, Asset> assets;
474 std::unique_ptr<EventQueue> events;
478 std::shared_ptr<ProcessBase*> reference;
480 std::shared_ptr<Gate> gate;
492 template <
typename T>
543 void finalize(
bool finalize_wsa =
false);
583 return spawn(&process, manage);
586 template <
typename T>
594 if (!
spawn(static_cast<ProcessBase*>(t), manage)) {
601 template <
typename T>
604 return spawn(&t, manage);
621 void terminate(
const UPID& pid,
bool inject =
true);
647 void post(
const UPID& to,
648 const std::string& name,
649 const char* data =
nullptr,
653 void post(
const UPID& from,
655 const std::string& name,
656 const char* data =
nullptr,
715 #endif // __PROCESS_PROCESS_HPP__
void finalize(bool finalize_wsa=false)
Clean up the library.
lambda::function< void(const UPID &, const std::string &)> MessageHandler
Any function which takes a "from" UPID and a message body as arguments.
Definition: process.hpp:211
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
ProcessBase(const std::string &id="")
If a persistent socket to the target pid does not exist, a new link is created.
RemoteConnection
Describes the behavior of the link call when the target pid points to a remote process.
Definition: process.hpp:161
void provide(const std::string &name, const std::string &path, const std::map< std::string, std::string > &types=mime::types)
Sets up the default HTTP request handler to provide the static asset(s) at the specified absolute pat...
Definition: process.hpp:368
bool initialize(const Option< std::string > &delegate=None(), const Option< std::string > &readwriteAuthenticationRealm=None(), const Option< std::string > &readonlyAuthenticationRealm=None())
Initialize the library.
Definition: event.hpp:142
T Self
Definition: process.hpp:506
Owned< Sequence > httpSequence
Definition: process.hpp:449
Definition: event.hpp:178
Definition: process.hpp:72
void route(const std::string &name, const Option< std::string > &help, const HttpRequestHandler &handler, const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
Definition: socket_manager.hpp:36
UPID spawn(ProcessBase *process, bool manage=false)
Spawn a new process.
Definition: duration.hpp:32
lambda::function< Future< http::Response > const http::Request &, const Option< http::authentication::Principal > &)> AuthenticatedHttpRequestHandler
Any function which takes a process::http::Request and an Option<Principal> and returns a process::htt...
Definition: process.hpp:323
void terminate(const UPID &pid, bool inject=true)
Sends a TerminateEvent to the given process.
virtual ~Process()
Definition: process.hpp:495
RouteOptions()
Definition: process.hpp:262
Definition: event.hpp:238
hashmap< std::string, HttpEndpoint > http
Definition: process.hpp:444
Definition: event.hpp:214
std::map< std::string, std::string > types
PID< Logging > logging()
Return the PID associated with the global logging process.
void send(const UPID &to, const std::string &name, const char *data=nullptr, size_t length=0)
Sends the message to the specified UPID.
thread_local ProcessBase * __process__
void install(std::vector< Owned< FirewallRule >> &&rules)
Install a list of firewall rules which are used to forbid incoming HTTP requests. ...
void post(const UPID &to, const std::string &name, const char *data=nullptr, size_t length=0)
Sends a message with data without a return address.
hashmap< std::string, MessageHandler > message
Definition: process.hpp:443
An "untyped" PID, used to encapsulate the process ID for lower-layer abstractions (eg...
Definition: pid.hpp:39
friend class ProcessManager
Definition: process.hpp:391
Definition: duration.hpp:259
void route(const std::string &name, const Option< std::string > &help, Future< http::Response >(T::*method)(const http::Request &), const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
Definition: process.hpp:286
virtual void initialize()
Invoked when a process gets spawned.
Definition: process.hpp:100
void install(const std::string &name, const MessageHandler &handler)
Sets up a handler for messages with the specified name.
Definition: process.hpp:216
lambda::function< Future< http::Response >const http::Request &)> HttpRequestHandler
Any function which takes a process::http::Request and returns a process::http::Response.
Definition: process.hpp:257
void consume(MessageEvent &&event) override
bool requestStreaming
Definition: process.hpp:267
UPID link(const UPID &pid, const RemoteConnection remote=RemoteConnection::REUSE)
Links with the specified UPID.
If a persistent socket to the target pid does not exist, a new link is created.
virtual void finalize()
Invoked when a process is terminated.
Definition: process.hpp:108
bool wait(const UPID &pid, const Duration &duration=Seconds(-1))
Wait for the process to exit for no more than the specified seconds.
Definition: address.hpp:52
bool exited(const UPID &from, const UPID &to)
Simulates disconnection of the link between 'from' and 'to' by sending an ExitedEvent to 'to'...
virtual void exited(const UPID &)
Invoked when a linked process has exited.
Definition: process.hpp:124
Definition: process.hpp:260
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:279
friend void * schedule(void *)
void route(const std::string &name, const std::string &realm, const Option< std::string > &help, Future< http::Response >(T::*method)(const http::Request &, const Option< http::authentication::Principal > &), const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
Definition: process.hpp:337
std::string absolutePath(const std::string &path)
Get the request absolutePath path with delegate prefix.
network::inet::Address address()
Returns the socket address associated with this instance of the library.
Definition: event.hpp:103
virtual void lost(const UPID &)
Invoked when a linked process can no longer be monitored.
Definition: process.hpp:133
size_t eventCount()
Returns the number of events of the given type currently on the event queue.
void install(const std::string &name, void(T::*method)(const UPID &, const std::string &))
Sets up a handler for messages with the specified name.
Definition: process.hpp:227
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
Definition: process.hpp:493
virtual void serve(Event &&event)
Invoked when an event is serviced.
Definition: process.hpp:85
const UPID & self() const
Definition: process.hpp:79
void delegate(const std::string &name, const UPID &pid)
Delegates incoming messages, with the specified name, to the UPID.
Definition: process.hpp:242
constexpr const char * name
Definition: shell.hpp:41
long workers()
Returns the number of worker threads the library has created.
T This
Definition: process.hpp:507