genode/repos/os/src/server/log_terminal/main.cc

200 lines
4.2 KiB
C++
Raw Normal View History

/*
* \brief Terminal that directs output to the LOG interface
* \author Norman Feske
* \date 2013-11-08
*/
/*
* Copyright (C) 2013-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* Genode includes */
#include <base/component.h>
#include <base/heap.h>
#include <root/component.h>
#include <base/attached_ram_dataspace.h>
#include <terminal_session/terminal_session.h>
#include <log_session/log_session.h>
namespace Terminal {
class Session_component;
class Root_component;
class Main;
using namespace Genode;
};
/**
* Utility for the buffered output of small successive write operations
*/
class Buffered_output
{
private:
enum { SIZE = Genode::Log_session::String::MAX_SIZE };
typedef Genode::size_t size_t;
char _buf[SIZE + 1 /* room for null-termination */ ];
/* index of next character within '_buf' to write */
unsigned _index = 0;
void _flush()
{
/* append null termination */
_buf[_index] = 0;
/* flush buffered characters to LOG */
Genode::log(Genode::Cstring(_buf));
/* reset */
_index = 0;
}
size_t _remaining_capacity() const { return SIZE - _index; }
public:
size_t write(char const *src, size_t num_bytes)
{
size_t const consume_bytes = Genode::min(num_bytes,
_remaining_capacity());
for (unsigned i = 0; i < consume_bytes; i++) {
char const c = src[i];
if (c == '\n')
_flush();
else
_buf[_index++] = c;
}
if (_remaining_capacity() == 0)
_flush();
return consume_bytes;
}
};
/**************
** Terminal **
**************/
class Terminal::Session_component : public Rpc_object<Session, Session_component>
{
private:
/**
* Buffer shared with the terminal client
*/
Attached_ram_dataspace _io_buffer;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Buffered_output _output { };
public:
Session_component(Ram_allocator &ram,
Region_map &rm,
size_t io_buffer_size)
:
_io_buffer(ram, rm, io_buffer_size)
{ }
/********************************
** Terminal session interface **
********************************/
Size size() override { return Size(0, 0); }
bool avail() override { return false; }
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
size_t _read(size_t) { return 0; }
size_t _write(Genode::size_t num_bytes)
{
/* sanitize argument */
num_bytes = Genode::min(num_bytes, _io_buffer.size());
char const *src = _io_buffer.local_addr<char>();
size_t written_bytes;
for (written_bytes = 0; written_bytes < num_bytes; )
written_bytes += _output.write(src + written_bytes,
num_bytes - written_bytes);
return written_bytes;
}
Dataspace_capability _dataspace() { return _io_buffer.cap(); }
void read_avail_sigh(Signal_context_capability) override { }
void size_changed_sigh(Signal_context_capability) override { }
void connected_sigh(Signal_context_capability sigh)
{
/*
* Immediately reflect connection-established signal to the
* client because the session is ready to use immediately after
* creation.
*/
Signal_transmitter(sigh).submit();
}
size_t read(void *, size_t) override { return 0; }
size_t write(void const *, size_t) override { return 0; }
};
class Terminal::Root_component : public Genode::Root_component<Session_component>
{
private:
Ram_allocator &_ram;
Region_map &_rm;
protected:
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Session_component *_create_session(const char *)
{
size_t const io_buffer_size = 4096;
return new (md_alloc()) Session_component(_ram, _rm, io_buffer_size);
}
public:
Root_component(Entrypoint &ep,
Allocator &md_alloc,
Ram_allocator &ram,
Region_map &rm)
:
Genode::Root_component<Session_component>(&ep.rpc_ep(), &md_alloc),
_ram(ram), _rm(rm)
{ }
};
struct Terminal::Main
{
Env &_env;
Sliced_heap sliced_heap { _env.ram(), _env.rm() };
Root_component terminal_root { _env.ep(), sliced_heap,
_env.ram(), _env.rm() };
Main(Env &env) : _env(env)
{
env.parent().announce(env.ep().manage(terminal_root));
}
};
void Component::construct(Genode::Env &env) { static Terminal::Main main(env); }