genode/repos/gems/src/server/http_blk/main.cc

111 lines
2.5 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Block interface for HTTP block driver
* \author Sebastian Sumpf <sebastian.sumpf@genode-labs.com>
* \author Stefan Kalkowski <stefan.kalkowski@genode-labs.com>
2011-12-22 16:19:25 +01:00
* \date 2010-08-24
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2010-2013 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/attached_rom_dataspace.h>
#include <base/log.h>
#include <block/component.h>
#include <libc/component.h>
2011-12-22 16:19:25 +01:00
/* local includes */
#include "http.h"
using namespace Genode;
class Driver : public Block::Driver
{
private:
2011-12-22 16:19:25 +01:00
size_t _block_size;
Http _http;
public:
2011-12-22 16:19:25 +01:00
Driver(Heap &heap, size_t block_size, ::String &uri)
: _block_size(block_size), _http(heap, uri) {}
2011-12-22 16:19:25 +01:00
/*******************************
** Block::Driver interface **
*******************************/
2011-12-22 16:19:25 +01:00
Genode::size_t block_size() { return _block_size; }
Block::sector_t block_count() { return _http.file_size() / _block_size; }
2011-12-22 16:19:25 +01:00
Block::Session::Operations ops()
{
Block::Session::Operations o;
o.set_operation(Block::Packet_descriptor::READ);
return o;
}
2011-12-22 16:19:25 +01:00
void read(Block::sector_t block_nr,
Genode::size_t block_count,
char *buffer,
Block::Packet_descriptor &packet)
{
_http.cmd_get(block_nr * _block_size, block_count * _block_size,
(addr_t)buffer);
ack_packet(packet);
}
2011-12-22 16:19:25 +01:00
};
class Factory : public Block::Driver_factory
2011-12-22 16:19:25 +01:00
{
private:
2011-12-22 16:19:25 +01:00
Env &_env;
Heap &_heap;
Attached_rom_dataspace _config { _env, "config" };
::String _uri;
size_t _blk_sz;
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
Factory(Env &env, Heap &heap)
: _env(env), _heap(heap), _blk_sz(512)
{
try {
_config.xml().attribute("uri").value(&_uri);
_config.xml().attribute("block_size").value(&_blk_sz);
}
catch (...) { }
2011-12-22 16:19:25 +01:00
log("Using file=", _uri, " as device with block size ",
Hex(_blk_sz, Hex::OMIT_PREFIX), ".");
2011-12-22 16:19:25 +01:00
}
Block::Driver *create() {
return new (&_heap) Driver(_heap, _blk_sz, _uri); }
2011-12-22 16:19:25 +01:00
void destroy(Block::Driver *driver) {
Genode::destroy(&_heap, driver); }
};
2011-12-22 16:19:25 +01:00
struct Main
2011-12-22 16:19:25 +01:00
{
Env &env;
Heap heap { env.ram(), env.rm() };
Factory factory { env, heap };
Block::Root root { env.ep(), heap, factory };
2011-12-22 16:19:25 +01:00
Main(Env &env) : env(env) {
env.parent().announce(env.ep().manage(root)); }
};
2011-12-22 16:19:25 +01:00
void Libc::Component::construct(Genode::Env &env) { static Main m(env); }