Ejemplo n.º 1
0
void berkeleydb_store<Object>::get(const timestamp& time,
                                   const long lp_id,
                                   obj_ptr& ret) {
  boost::lock_guard<boost::mutex> guard(get_mutex_);
  /* generate key */
  std::vector<char> key;
  key_lpid_to_char(time, lp_id, key);
  Dbt db_key(&key[0], key.size());

  /* get data */
  char* event_binary;
  Dbt data;
  data.set_data(event_binary);
  data.set_flags(DB_DBT_MALLOC);

  db->get(NULL, &db_key, &data, 0);

  /* deserialize the data */
  std::string binary = std::string((char*) data.get_data(), data.get_size());
  std::stringstream from_ss;
  from_ss << binary;
  boost::archive::binary_iarchive iar(from_ss);
  Object obj;
  iar >> obj;

  ret = boost::make_shared<Object>(obj);
};
void
persistence_leveldb::store (map_coordinates xy, chunk_height z)
{
    uint32_t key[3];
    key[0] = data_type::cnk_height;
    key[1] = xy.x;
    key[2] = xy.y;

    auto ser (serialize(z));

    leveldb::Slice db_key   (reinterpret_cast<const char*>(key), sizeof(key));
    leveldb::Slice db_value (reinterpret_cast<const char*>(&*ser.begin()), ser.size());

    check(db_->Put(leveldb::WriteOptions(), db_key, db_value));
}
void
persistence_leveldb::store (const es::storage& es, es::storage::iterator i)
{
    std::vector<char> buffer;
    uint32_t key[2];
    key[0] = type_entity;
    key[1] = i->first;

    es.serialize(i, buffer);

    leveldb::Slice db_key   (reinterpret_cast<const char*>(key), sizeof(key));
    leveldb::Slice db_value (reinterpret_cast<const char*>(&buffer[0]), buffer.size());

    check(db_->Put(leveldb::WriteOptions(), db_key, db_value));
}
bool
persistence_leveldb::is_available (map_coordinates xy)
{
    uint32_t key[3];
    key[0] = data_type::cnk_height;
    key[1] = xy.x;
    key[2] = xy.y;

    leveldb::Slice db_key (reinterpret_cast<const char*>(key), sizeof(key));
    std::string result;

    auto rc (db_->Get(leveldb::ReadOptions(), db_key, &result));

    return !rc.IsNotFound();
}
chunk_height
persistence_leveldb::retrieve (map_coordinates xy)
{
    uint32_t key[3];
    key[0] = data_type::cnk_height;
    key[1] = xy.x;
    key[2] = xy.y;

    leveldb::Slice db_key (reinterpret_cast<const char*>(key), sizeof(key));
    std::string result;
    check(db_->Get(leveldb::ReadOptions(), db_key, &result));
    assert(result.size() == sizeof(chunk_height));

    return deserialize_as<chunk_height>(result);
}
void
persistence_leveldb::store (data_type type, chunk_coordinates xyz,
                            const compressed_data& data)
{
    uint32_t key[4];
    key[0] = type;
    key[1] = xyz.x;
    key[2] = xyz.y;
    key[3] = xyz.z;

    auto serialized (serialize(data));
    leveldb::Slice db_key   (reinterpret_cast<const char*>(key), sizeof(key));
    leveldb::Slice db_value (reinterpret_cast<const char*>(&*serialized.begin()), serialized.size());

    check(db_->Put(leveldb::WriteOptions(), db_key, db_value));
}
bool
persistence_leveldb::is_available (data_type type, chunk_coordinates xyz)
{
    uint32_t key[4];
    key[0] = type;
    key[1] = xyz.x;
    key[2] = xyz.y;
    key[3] = xyz.z;

    leveldb::Slice db_key (reinterpret_cast<const char*>(key), sizeof(key));
    std::string result;

    auto rc (db_->Get(leveldb::ReadOptions(), db_key, &result));

    return !rc.IsNotFound();
}
compressed_data
persistence_leveldb::retrieve (data_type type, chunk_coordinates xyz)
{
    uint32_t key[4];
    key[0] = type;
    key[1] = xyz.x;
    key[2] = xyz.y;
    key[3] = xyz.z;

    leveldb::Slice db_key (reinterpret_cast<const char*>(key), sizeof(key));
    std::string result;

    check(db_->Get(leveldb::ReadOptions(), db_key, &result));

    return deserialize_as<compressed_data>(result);
}
void
persistence_leveldb::store (const es::storage& es)
{
    std::vector<char> buffer;
    for (auto i (es.begin()); i != es.end(); ++i)
    {
        uint32_t key[2];
        key[0] = type_entity;
        key[1] = i->first;

        buffer.clear();
        es.serialize(i, buffer);

        leveldb::Slice db_key   (reinterpret_cast<const char*>(key), sizeof(key));
        leveldb::Slice db_value (reinterpret_cast<const char*>(&buffer[0]), buffer.size());

        check(db_->Put(leveldb::WriteOptions(), db_key, db_value));
    }
}
Ejemplo n.º 10
0
void berkeleydb_store<Object>::put(const timestamp& time,
                                   const long lp_id,
                                   const Object& value) {
  boost::lock_guard<boost::mutex> guard(put_mutex_);
  /* generate key */
  std::vector<char> key;
  key_lpid_to_char(time, lp_id, key);

  /* serialize object */
  std::stringstream ss;
  boost::archive::binary_oarchive ar(ss);
  ar << value;
  std::string binary = ss.str();
  std::vector<char> put_event_binary(binary.begin(), binary.end());

  Dbt db_key(&key[0], key.size());
  Dbt data(&put_event_binary[0], put_event_binary.size());
  DbTxn* txn = NULL;
  env->txn_begin(NULL, &txn, 0);
  int ret = db->put(txn, &db_key, &data, 0);
  txn->commit(0);
};
Ejemplo n.º 11
0
void berkeleydb_store<Object>::put_range(const std::vector<timestamp>& keys,
                                         const std::vector<obj_ptr>& values,
                                         const long lp_id) {
  boost::lock_guard<boost::mutex> guard(put_mutex_);
  for (int i = 0; i < keys.size(); ++i) {
    /* generate key */
    std::vector<char> key;
    key_lpid_to_char(keys[i], lp_id, key);

    /* serialize object */
    std::stringstream ss;
    boost::archive::binary_oarchive ar(ss);
    ar << *(values[i]);
    std::string binary = ss.str();
    std::vector<char> put_event_binary(binary.begin(), binary.end());

    Dbt db_key(&key[0], key.size());
    Dbt data(&put_event_binary[0], put_event_binary.size());
    DbTxn* txn = NULL;
    env->txn_begin(NULL, &txn, 0);
    int ret = db->put(txn, &db_key, &data, 0);
    txn->commit(0);
  }
};
Ejemplo n.º 12
0
static void in_stream_node_callback(int type, xode node, void *arg) 
{
	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
	char *tag;
	xode x;

	LM_DBG("instream callback: %d: %s\n",
			type, node ? xode_get_name(node) : "n/a");
	switch (type) {
	case XODE_STREAM_ROOT:
		conn->stream_id = strdup(random_secret());
		net_printf(conn->fd,
			"<?xml version='1.0'?>"
			"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0'"
			" xmlns:db='jabber:server:dialback' id='%s' from='%s'>", conn->stream_id, xmpp_domain);
		net_printf(conn->fd,"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
		break;
	case XODE_STREAM_NODE:
		tag = xode_get_name(node);

		if (!strcmp(tag, "db:result")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			/* char *id = xode_get_attrib(node, "id"); */
			char *type = xode_get_attrib(node, "type");
			char *cdata = xode_get_data(node);
			
			if (!type) {
				if (conn->domain) {
					LM_DBG("connection %d has old domain '%s'\n",conn->fd,
							conn->domain);
					free(conn->domain);
				}
				conn->domain = strdup(from);
				LM_DBG("connection %d set domain '%s'\n",
						conn->fd, conn->domain);

				/* it's a request; send verification over outgoing connection */
				x = xode_new_tag("db:verify");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				//xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
				xode_put_attrib(x, "id", conn->stream_id);
				xode_insert_cdata(x, cdata, -1);
				xode_send_domain(from, x);
			}			
		} else if (!strcmp(tag, "db:verify")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *id = xode_get_attrib(node, "id");
			char *type = xode_get_attrib(node, "type");
			char *cdata = xode_get_data(node);
			
			if (!type) {
				/* it's a request */
				x = xode_new_tag("db:verify");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				xode_put_attrib(x, "id", id);
				//if (cdata && !strcmp(cdata, DB_KEY)) {
				if (cdata && !strcmp(cdata, db_key(local_secret, from, id))) {
					xode_put_attrib(x, "type", "valid");
				} else {
					xode_put_attrib(x, "type", "invalid");
				}
				xode_send(conn->fd, x);
				xode_free(x);
			}			
		} else if (!strcmp(tag, "message")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *type = xode_get_attrib(node, "type");
			xode body = xode_get_tag(node, "body");
			char *msg;
			
			if (!type)
				type = "chat";
			if (!strcmp(type, "error")) {	
				LM_DBG("received message error stanza\n");
				goto out;
			}
			
			if (!from || !to || !body) {
				LM_DBG("invalid <message/> attributes\n");
				goto out;
			}

			if (!(msg = xode_get_data(body)))
				msg = "";
			xmpp_send_sip_msg(
				encode_uri_xmpp_sip(from),
				decode_uri_xmpp_sip(to),
				msg);
		} else if (!strcmp(tag, "presence")) {
			/* run presence callbacks */
		}
		break;

		break;
	case XODE_STREAM_ERROR:
		LM_ERR("instream error\n");
		/* fall-through */
	case XODE_STREAM_CLOSE:
		conn->type = CONN_DEAD;
		break;
	}
out:
	xode_free(node);
}
Ejemplo n.º 13
0
static void out_stream_node_callback(int type, xode node, void *arg)
{
	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
	struct xmpp_connection *in_conn = NULL;
	char *tag;
	xode x;

	LM_DBG("outstream callback: %d: %s\n", type, 
			node?xode_get_name(node):"n/a");

	if (conn->domain)
		in_conn = conn_find_domain(conn->domain, CONN_INBOUND);

	switch (type) {
	case XODE_STREAM_ROOT:
		x = xode_new_tag("db:result");
		xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
		xode_put_attrib(x, "from", xmpp_domain);
		xode_put_attrib(x, "to", conn->domain);
		//xode_insert_cdata(x, DB_KEY, -1);
		xode_insert_cdata(x, db_key(local_secret, conn->domain,
					xode_get_attrib(node, "id")), -1);
		xode_send(conn->fd, x);
		xode_free(x);

		break;
	case XODE_STREAM_NODE:
		tag = xode_get_name(node);

		if (!strcmp(tag, "db:verify")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *id = xode_get_attrib(node, "id");
			char *type = xode_get_attrib(node, "type");
			/* char *cdata = xode_get_data(node); */
			
			if (!strcmp(type, "valid") || !strcmp(type, "invalid")) {
				/* got a reply, report it */
				x = xode_new_tag("db:result");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				xode_put_attrib(x, "id", id);
				xode_put_attrib(x, "type", type);
				if (in_conn)
					xode_send(in_conn->fd, x);
				else
					LM_ERR("need to send reply to domain '%s', but no inbound"
							" connection found\n", from);
				xode_free(x);
			}
		} else if (!strcmp(tag, "db:result")) {
			char *type = xode_get_attrib(node, "type");
			
			if (type && !strcmp(type, "valid")) {
				/* the remote server has successfully authenticated us,
				 * we can now send data */
				for (x = xode_get_firstchild(conn->todo); x;
						x = xode_get_nextsibling(x)) {
					LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
					xode_send(conn->fd, x);
				}
				xode_free(conn->todo);
				conn->todo = NULL;
			}
		}
		break;
	case XODE_STREAM_ERROR:
		LM_ERR("outstream error\n");
		/* fall-through */
	case XODE_STREAM_CLOSE:
		conn->type = CONN_DEAD;
		break;
	}
	xode_free(node);
}