hello_tutorial: Made hello session stateful.

This commit is contained in:
Michael Müller
2022-07-11 14:26:28 +02:00
parent 481a26d286
commit f98359cbe6
4 changed files with 26 additions and 15 deletions

View File

@@ -22,14 +22,15 @@ namespace Hello { struct Connection; }
struct Hello::Connection : Genode::Connection<Session>, Session_client
{
Connection(Genode::Env &env)
:
/* create session */
Genode::Connection<Hello::Session>(env, session(env.parent(),
"ram_quota=6K, cap_quota=4")),
Connection(Genode::Env &env, unsigned short id)
: /* create session */
Genode::Connection<Hello::Session>(env, session(env.parent(),
"ram_quota=6K, cap_quota=4")),
/* initialize RPC interface */
Session_client(cap()) { }
/* initialize RPC interface */
Session_client(cap())
{
}
};
#endif /* _INCLUDE__HELLO_SESSION__CONNECTION_H_ */

View File

@@ -24,6 +24,8 @@ struct Hello::Session : Genode::Session
{
static const char *service_name() { return "Hello"; }
unsigned short _id;
enum { CAP_QUOTA = 2 };
virtual void say_hello() = 0;

View File

@@ -60,7 +60,7 @@ struct HelloClient {
Genode::log("hello test completed.");
}
HelloClient(Genode::Env &env, Hello::Connection &conn) : _env(env), _hello(conn), _a(5), _b(2)
HelloClient(Genode::Env &env, Hello::Connection &conn) : _env(env), _a(5), _b(2), _hello(conn)
{
_config.sigh(_config_handler);
_handle_config();
@@ -70,7 +70,8 @@ struct HelloClient {
void
Component::construct(Genode::Env &env)
{
Hello::Connection hello(env);
Timer::Connection timer(env);
Hello::Connection hello(env, (unsigned short)timer.elapsed_ms());
HelloClient client(env, hello);
client.run();

View File

@@ -18,7 +18,7 @@
#include <root/component.h>
#include <hello_session/hello_session.h>
#include <base/rpc_server.h>
#include <timer_session/connection.h>
namespace Hello {
struct Session_component;
struct Root_component;
@@ -28,8 +28,12 @@ namespace Hello {
struct Hello::Session_component : Genode::Rpc_object<Session>
{
unsigned int _id;
Session_component(unsigned short id) : Genode::Rpc_object<Session>(), _id(id) {}
void say_hello() override {
Genode::log("I am here... Hello."); }
Genode::log("I am here... Hello. My id is ", _id, "."); }
int add(int a, int b) override {
return a + b; }
@@ -41,19 +45,21 @@ class Hello::Root_component
public Genode::Root_component<Session_component>
{
protected:
Timer::Connection &_timer;
Session_component *_create_session(const char *) override
{
Genode::log("creating hello session");
return new (md_alloc()) Session_component();
return new (md_alloc()) Session_component((unsigned short)_timer.elapsed_ms());
}
public:
Root_component(Genode::Entrypoint &ep,
Genode::Allocator &alloc)
Genode::Allocator &alloc,
Timer::Connection &timer)
:
Genode::Root_component<Session_component>(ep, alloc)
Genode::Root_component<Session_component>(ep, alloc), _timer(timer)
{
Genode::log("creating root component");
}
@@ -63,6 +69,7 @@ class Hello::Root_component
struct Hello::Main
{
Genode::Env &env;
Timer::Connection _timer { env };
/*
* A sliced heap is used for allocating session objects - thereby we
@@ -70,7 +77,7 @@ struct Hello::Main
*/
Genode::Sliced_heap sliced_heap { env.ram(), env.rm() };
Hello::Root_component root { env.ep(), sliced_heap };
Hello::Root_component root { env.ep(), sliced_heap, _timer };
Main(Genode::Env &env) : env(env)
{