<commit_before>/*

Copyright (c) 2007-2014, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/assert.hpp"
#include "libtorrent/puff.hpp"

#include <vector>
#include <string>

namespace
{
	enum
	{
		FTEXT = 0x01,
		FHCRC = 0x02,
		FEXTRA = 0x04,
		FNAME = 0x08,
		FCOMMENT = 0x10,
		FRESERVED = 0xe0,

		GZIP_MAGIC0 = 0x1f,
		GZIP_MAGIC1 = 0x8b
	};

}

namespace libtorrent
{
	// returns -1 if gzip header is invalid or the header size in bytes
	int gzip_header(const char* buf, int size)
	{
		TORRENT_ASSERT(buf != 0);

		const unsigned char* buffer = reinterpret_cast<const unsigned char*>(buf);
		const int total_size = size;

		// The zip header cannot be shorter than 10 bytes
		if (size < 10 || buf == 0) return -1;

		// check the magic header of gzip
		if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1;

		int method = buffer[2];
		int flags = buffer[3];

		// check for reserved flag and make sure it's compressed with the correct metod
		if (method != 8 || (flags & FRESERVED) != 0) return -1;

		// skip time, xflags, OS code
		size -= 10;
		buffer += 10;

		if (flags & FEXTRA)
		{
			int extra_len;

			if (size < 2) return -1;

			extra_len = (buffer[1] << 8) | buffer[0];

			if (size < (extra_len+2)) return -1;
			size -= (extra_len + 2);
			buffer += (extra_len + 2);
		}

		if (flags & FNAME)
		{
			while (size && *buffer)
			{
				--size;
				++buffer;
			}
			if (!size || *buffer) return -1;

			--size;
			++buffer;
		}

		if (flags & FCOMMENT)
		{
			while (size && *buffer)
			{
				--size;
				++buffer;
			}
			if (!size || *buffer) return -1;

			--size;
			++buffer;
		}

		if (flags & FHCRC)
		{
			if (size < 2) return -1;

			size -= 2;
//			buffer += 2;
		}

		return total_size - size;
	}

	// TODO: 2 it would be nice to use proper error handling here
	bool inflate_gzip(
		char const* in
		, int size
		, std::vector<char>& buffer
		, int maximum_size
		, std::string& error)
	{
		TORRENT_ASSERT(maximum_size > 0);

		int header_len = gzip_header(in, size);
		if (header_len < 0)
		{
			error = "invalid gzip header";
			return true;
		}

		// start off with 4 kilobytes and grow
		// if needed
		boost::uint32_t destlen = 4096;
		int ret = 0;
		boost::uint32_t srclen = size - header_len;
		in += header_len;

		do
		{
			TORRENT_TRY {
				buffer.resize(destlen);
			} TORRENT_CATCH(std::exception& e) {
				error = "out of memory";
				return true;
			}

			ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen);

			// if the destination buffer wasn't large enough, double its
			// size and try again. Unless it's already at its max, in which
			// case we fail
			if (ret == 1) // 1:  output space exhausted before completing inflate
			{
				if (destlen == maximum_size)
				{
					error = "inflated data too big";
					return true;
				}

				destlen *= 2;
				if (destlen > maximum_size)
					destlen = maximum_size;
			}
		} while (ret == 1);

		if (ret != 0)
		{
			error = "error while inflating data";
			return true;
		}

		if (destlen > buffer.size())
		{
			error = "internal gzip error";
			return true;
		}

		buffer.resize(destlen);

		return false;
	}

}

<commit_msg>fix inflate_gzip export for unit test<commit_after>/*

Copyright (c) 2007-2014, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/assert.hpp"
#include "libtorrent/puff.hpp"

#include <vector>
#include <string>

namespace
{
	enum
	{
		FTEXT = 0x01,
		FHCRC = 0x02,
		FEXTRA = 0x04,
		FNAME = 0x08,
		FCOMMENT = 0x10,
		FRESERVED = 0xe0,

		GZIP_MAGIC0 = 0x1f,
		GZIP_MAGIC1 = 0x8b
	};

}

namespace libtorrent
{
	// returns -1 if gzip header is invalid or the header size in bytes
	int gzip_header(const char* buf, int size)
	{
		TORRENT_ASSERT(buf != 0);

		const unsigned char* buffer = reinterpret_cast<const unsigned char*>(buf);
		const int total_size = size;

		// The zip header cannot be shorter than 10 bytes
		if (size < 10 || buf == 0) return -1;

		// check the magic header of gzip
		if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1;

		int method = buffer[2];
		int flags = buffer[3];

		// check for reserved flag and make sure it's compressed with the correct metod
		if (method != 8 || (flags & FRESERVED) != 0) return -1;

		// skip time, xflags, OS code
		size -= 10;
		buffer += 10;

		if (flags & FEXTRA)
		{
			int extra_len;

			if (size < 2) return -1;

			extra_len = (buffer[1] << 8) | buffer[0];

			if (size < (extra_len+2)) return -1;
			size -= (extra_len + 2);
			buffer += (extra_len + 2);
		}

		if (flags & FNAME)
		{
			while (size && *buffer)
			{
				--size;
				++buffer;
			}
			if (!size || *buffer) return -1;

			--size;
			++buffer;
		}

		if (flags & FCOMMENT)
		{
			while (size && *buffer)
			{
				--size;
				++buffer;
			}
			if (!size || *buffer) return -1;

			--size;
			++buffer;
		}

		if (flags & FHCRC)
		{
			if (size < 2) return -1;

			size -= 2;
//			buffer += 2;
		}

		return total_size - size;
	}

	// TODO: 2 it would be nice to use proper error handling here
	TORRENT_EXTRA_EXPORT bool inflate_gzip(
		char const* in
		, int size
		, std::vector<char>& buffer
		, int maximum_size
		, std::string& error)
	{
		TORRENT_ASSERT(maximum_size > 0);

		int header_len = gzip_header(in, size);
		if (header_len < 0)
		{
			error = "invalid gzip header";
			return true;
		}

		// start off with 4 kilobytes and grow
		// if needed
		boost::uint32_t destlen = 4096;
		int ret = 0;
		boost::uint32_t srclen = size - header_len;
		in += header_len;

		do
		{
			TORRENT_TRY {
				buffer.resize(destlen);
			} TORRENT_CATCH(std::exception& e) {
				error = "out of memory";
				return true;
			}

			ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen);

			// if the destination buffer wasn't large enough, double its
			// size and try again. Unless it's already at its max, in which
			// case we fail
			if (ret == 1) // 1:  output space exhausted before completing inflate
			{
				if (destlen == maximum_size)
				{
					error = "inflated data too big";
					return true;
				}

				destlen *= 2;
				if (destlen > maximum_size)
					destlen = maximum_size;
			}
		} while (ret == 1);

		if (ret != 0)
		{
			error = "error while inflating data";
			return true;
		}

		if (destlen > buffer.size())
		{
			error = "internal gzip error";
			return true;
		}

		buffer.resize(destlen);

		return false;
	}

}

<|endoftext|>


<commit_before>#include "helix-c/helix.h"

#include "helix/nasdaq/nordic_itch.hh"
#include "helix/net.hh"

#include <cassert>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

inline helix_order_book_t wrap(helix::core::order_book* ob)
{
	return reinterpret_cast<helix_order_book_t>(ob);
}

inline helix::core::order_book* unwrap(helix_order_book_t ob)
{
	return reinterpret_cast<helix::core::order_book*>(ob);
}

inline helix_trade_t wrap(helix::core::trade* ob)
{
	return reinterpret_cast<helix_trade_t>(ob);
}

inline helix::core::trade* unwrap(helix_trade_t ob)
{
	return reinterpret_cast<helix::core::trade*>(ob);
}

inline helix_protocol_t wrap(helix::core::protocol* proto)
{
	return reinterpret_cast<helix_protocol_t>(proto);
}

inline helix::core::protocol* unwrap(helix_protocol_t proto)
{
	return reinterpret_cast<helix::core::protocol*>(proto);
}

inline helix_session_t wrap(helix::core::session* session)
{
	return reinterpret_cast<helix_session_t>(session);
}

inline helix::core::session* unwrap(helix_session_t session)
{
	return reinterpret_cast<helix::core::session*>(session);
}

helix_protocol_t helix_protocol_lookup(const char *name)
{
	if (!strcmp(name, "nasdaq-nordic-moldudp-itch") || !strcmp(name, "nasdaq-nordic-soupfile-itch")) {
		return wrap(new helix::nasdaq::nordic_itch_protocol{name});
	}
	return NULL;
}

helix_session_t
helix_session_create(helix_protocol_t proto, const char *symbol, helix_order_book_callback_t ob_callback, helix_trade_callback_t trade_callback, void *data)
{
	vector<string> symbols;
	symbols.emplace_back(string{symbol});
        auto session = unwrap(proto)->new_session(symbols, data);
        session->register_callback([session, ob_callback](const helix::core::order_book& ob) {
            ob_callback(wrap(session), wrap(const_cast<helix::core::order_book*>(&ob)));
        });
        session->register_callback([session, trade_callback](const helix::core::trade& trade) {
            trade_callback(wrap(session), wrap(const_cast<helix::core::trade*>(&trade)));
        });
        return wrap(session);
}

void *helix_session_data(helix_session_t session)
{
	return unwrap(session)->data();
}

size_t helix_session_process_packet(helix_session_t session, const char* buf, size_t len)
{
	return unwrap(session)->process_packet(helix::net::packet_view{buf, len});
}

const char *helix_order_book_symbol(helix_order_book_t ob)
{
	return unwrap(ob)->symbol().c_str();
}

uint64_t helix_order_book_timestamp(helix_order_book_t ob)
{
	return unwrap(ob)->timestamp();
}

size_t helix_order_book_bid_levels(helix_order_book_t ob)
{
	return unwrap(ob)->bid_levels();
}

size_t helix_order_book_ask_levels(helix_order_book_t ob)
{
	return unwrap(ob)->ask_levels();
}

uint64_t helix_order_book_bid_price(helix_order_book_t ob, size_t level)
{
	return unwrap(ob)->bid_price(level);
}

uint64_t helix_order_book_bid_size(helix_order_book_t ob, size_t level)
{
	return unwrap(ob)->bid_size(level);
}

uint64_t helix_order_book_ask_price(helix_order_book_t ob, size_t level)
{
	return unwrap(ob)->ask_price(level);
}

uint64_t helix_order_book_ask_size(helix_order_book_t ob, size_t level)
{
	return unwrap(ob)->ask_size(level);
}

uint64_t helix_order_book_midprice(helix_order_book_t ob, size_t level)
{
	return unwrap(ob)->midprice(level);
}

helix_trading_state_t helix_order_book_state(helix_order_book_t ob)
{
	switch (unwrap(ob)->state()) {
	case helix::core::trading_state::unknown: return HELIX_TRADING_STATE_UNKNOWN;
	case helix::core::trading_state::halted:  return HELIX_TRADING_STATE_HALTED;
	case helix::core::trading_state::trading: return HELIX_TRADING_STATE_TRADING;
	case helix::core::trading_state::auction: return HELIX_TRADING_STATE_AUCTION;
	}
        assert(0);
}

const char *helix_trade_symbol(helix_trade_t trade)
{
	return unwrap(trade)->symbol.c_str();
}

uint64_t helix_trade_timestamp(helix_trade_t trade)
{
	return unwrap(trade)->timestamp;
}

uint64_t helix_trade_price(helix_trade_t trade)
{
	return unwrap(trade)->price;
}

uint64_t helix_trade_size(helix_trade_t trade)
{
	return unwrap(trade)->size;
}

helix_trade_sign_t helix_trade_sign(helix_trade_t trade)
{
	switch (unwrap(trade)->sign) {
	case helix::core::trade_sign::buyer_initiated:    return HELIX_TRADE_SIGN_BUYER_INITIATED;
	case helix::core::trade_sign::seller_initiated:   return HELIX_TRADE_SIGN_SELLER_INITIATED;
	case helix::core::trade_sign::crossing:           return HELIX_TRADE_SIGN_CROSSING;
	case helix::core::trade_sign::non_displayable:    return HELIX_TRADE_SIGN_NON_DISPLAYABLE;
	}
        assert(0);
}
<commit_msg>helix.cc: Fix formatting issues<commit_after>#include "helix-c/helix.h"

#include "helix/nasdaq/nordic_itch.hh"
#include "helix/net.hh"

#include <cassert>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

inline helix_order_book_t wrap(helix::core::order_book* ob)
{
    return reinterpret_cast<helix_order_book_t>(ob);
}

inline helix::core::order_book* unwrap(helix_order_book_t ob)
{
    return reinterpret_cast<helix::core::order_book*>(ob);
}

inline helix_trade_t wrap(helix::core::trade* ob)
{
    return reinterpret_cast<helix_trade_t>(ob);
}

inline helix::core::trade* unwrap(helix_trade_t ob)
{
    return reinterpret_cast<helix::core::trade*>(ob);
}

inline helix_protocol_t wrap(helix::core::protocol* proto)
{
    return reinterpret_cast<helix_protocol_t>(proto);
}

inline helix::core::protocol* unwrap(helix_protocol_t proto)
{
    return reinterpret_cast<helix::core::protocol*>(proto);
}

inline helix_session_t wrap(helix::core::session* session)
{
    return reinterpret_cast<helix_session_t>(session);
}

inline helix::core::session* unwrap(helix_session_t session)
{
    return reinterpret_cast<helix::core::session*>(session);
}

helix_protocol_t helix_protocol_lookup(const char *name)
{
    if (!strcmp(name, "nasdaq-nordic-moldudp-itch") || !strcmp(name, "nasdaq-nordic-soupfile-itch")) {
        return wrap(new helix::nasdaq::nordic_itch_protocol{name});
    }
    return NULL;
}

helix_session_t
helix_session_create(helix_protocol_t proto, const char *symbol, helix_order_book_callback_t ob_callback, helix_trade_callback_t trade_callback, void *data)
{
    vector<string> symbols;
    symbols.emplace_back(string{symbol});
    auto session = unwrap(proto)->new_session(symbols, data);
    session->register_callback([session, ob_callback](const helix::core::order_book& ob) {
        ob_callback(wrap(session), wrap(const_cast<helix::core::order_book*>(&ob)));
    });
    session->register_callback([session, trade_callback](const helix::core::trade& trade) {
        trade_callback(wrap(session), wrap(const_cast<helix::core::trade*>(&trade)));
    });
    return wrap(session);
}

void *helix_session_data(helix_session_t session)
{
    return unwrap(session)->data();
}

size_t helix_session_process_packet(helix_session_t session, const char* buf, size_t len)
{
    return unwrap(session)->process_packet(helix::net::packet_view{buf, len});
}

const char *helix_order_book_symbol(helix_order_book_t ob)
{
    return unwrap(ob)->symbol().c_str();
}

uint64_t helix_order_book_timestamp(helix_order_book_t ob)
{
    return unwrap(ob)->timestamp();
}

size_t helix_order_book_bid_levels(helix_order_book_t ob)
{
    return unwrap(ob)->bid_levels();
}

size_t helix_order_book_ask_levels(helix_order_book_t ob)
{
    return unwrap(ob)->ask_levels();
}

uint64_t helix_order_book_bid_price(helix_order_book_t ob, size_t level)
{
    return unwrap(ob)->bid_price(level);
}

uint64_t helix_order_book_bid_size(helix_order_book_t ob, size_t level)
{
    return unwrap(ob)->bid_size(level);
}

uint64_t helix_order_book_ask_price(helix_order_book_t ob, size_t level)
{
    return unwrap(ob)->ask_price(level);
}

uint64_t helix_order_book_ask_size(helix_order_book_t ob, size_t level)
{
    return unwrap(ob)->ask_size(level);
}

uint64_t helix_order_book_midprice(helix_order_book_t ob, size_t level)
{
    return unwrap(ob)->midprice(level);
}

helix_trading_state_t helix_order_book_state(helix_order_book_t ob)
{
    switch (unwrap(ob)->state()) {
    case helix::core::trading_state::unknown: return HELIX_TRADING_STATE_UNKNOWN;
    case helix::core::trading_state::halted:  return HELIX_TRADING_STATE_HALTED;
    case helix::core::trading_state::trading: return HELIX_TRADING_STATE_TRADING;
    case helix::core::trading_state::auction: return HELIX_TRADING_STATE_AUCTION;
    }
    assert(0);
}

const char *helix_trade_symbol(helix_trade_t trade)
{
    return unwrap(trade)->symbol.c_str();
}

uint64_t helix_trade_timestamp(helix_trade_t trade)
{
    return unwrap(trade)->timestamp;
}

uint64_t helix_trade_price(helix_trade_t trade)
{
    return unwrap(trade)->price;
}

uint64_t helix_trade_size(helix_trade_t trade)
{
    return unwrap(trade)->size;
}

helix_trade_sign_t helix_trade_sign(helix_trade_t trade)
{
    switch (unwrap(trade)->sign) {
    case helix::core::trade_sign::buyer_initiated:  return HELIX_TRADE_SIGN_BUYER_INITIATED;
    case helix::core::trade_sign::seller_initiated: return HELIX_TRADE_SIGN_SELLER_INITIATED;
    case helix::core::trade_sign::crossing:         return HELIX_TRADE_SIGN_CROSSING;
    case helix::core::trade_sign::non_displayable:  return HELIX_TRADE_SIGN_NON_DISPLAYABLE;
    }
    assert(0);
}
<|endoftext|>


<commit_before>#include "nanocv.h"
#include "tasks/task_dummy.h"
#include <boost/program_options.hpp>

int main(int argc, char *argv[])
{
        ncv::init();

        using namespace ncv;

        // parse the command line
        boost::program_options::options_description po_desc("", 160);
        po_desc.add_options()("help,h", "test program");
        po_desc.add_options()("threads,t",
                boost::program_options::value<size_t>()->default_value(1),
                "number of threads to use [1, 64], 0 - use all available threads");
        po_desc.add_options()("samples,s",
                boost::program_options::value<size_t>()->default_value(100000),
                "number of samples to use [1000, 100000]");
        po_desc.add_options()("forward",
                "evaluate the \'forward\' pass (output)");
        po_desc.add_options()("backward",
                "evaluate the \'backward' pass (gradient)");

        boost::program_options::variables_map po_vm;
        boost::program_options::store(
                boost::program_options::command_line_parser(argc, argv).options(po_desc).run(),
                po_vm);
        boost::program_options::notify(po_vm);

        // check arguments and options
        if (	po_vm.empty() ||
                po_vm.count("help"))
        {
                std::cout << po_desc;
                return EXIT_FAILURE;
        }

        const size_t cmd_threads = math::clamp(po_vm["threads"].as<size_t>(), 0, 64);
        const size_t cmd_samples = math::clamp(po_vm["samples"].as<size_t>(), 1000, 100 * 1000);
        const bool cmd_forward = po_vm.count("forward");
        const bool cmd_backward = po_vm.count("backward");

        if (!cmd_forward && !cmd_backward)
        {
                std::cout << po_desc;
                return EXIT_FAILURE;
        }

        dummy_task_t task;
        task.set_rows(28);
        task.set_cols(28);
        task.set_color(color_mode::luma);
        task.set_outputs(10);
        task.set_folds(1);
        task.set_size(cmd_samples * 100);
        task.setup();

        const size_t cmd_outputs = task.n_outputs();

        const string_t lmodel0;
        const string_t lmodel1 = lmodel0 + "linear:dims=100;act-snorm;";
        const string_t lmodel2 = lmodel1 + "linear:dims=100;act-snorm;";
        const string_t lmodel3 = lmodel2 + "linear:dims=100;act-snorm;";
        const string_t lmodel4 = lmodel3 + "linear:dims=100;act-snorm;";
        const string_t lmodel5 = lmodel4 + "linear:dims=100;act-snorm;";

        string_t cmodel;
        cmodel = cmodel + "conv:dims=16,rows=6,cols=6,type=full;act-snorm;pool-max;";
        cmodel = cmodel + "conv:dims=32,rows=5,cols=5,type=full;act-snorm;pool-max;";
        cmodel = cmodel + "conv:dims=64,rows=4,cols=4,type=full;act-snorm;";

        string_t rmodel;
        rmodel = rmodel + "conv:dims=16,rows=6,cols=6,type=rand;act-snorm;pool-max;";
        rmodel = rmodel + "conv:dims=32,rows=5,cols=5,type=rand;act-snorm;pool-max;";
        rmodel = rmodel + "conv:dims=64,rows=4,cols=4,type=rand;act-snorm;";

        string_t mmodel;
        mmodel = mmodel + "conv:dims=16,rows=6,cols=6,type=mask;act-snorm;pool-max;";
        mmodel = mmodel + "conv:dims=32,rows=5,cols=5,type=mask;act-snorm;pool-max;";
        mmodel = mmodel + "conv:dims=64,rows=4,cols=4,type=mask;act-snorm;";

        const string_t outlayer = "linear:dims=" + text::to_string(cmd_outputs) + ";";

        strings_t cmd_networks =
        {
                lmodel0 + outlayer,
                lmodel1 + outlayer,
                lmodel2 + outlayer,
                lmodel3 + outlayer,
                lmodel4 + outlayer,
                lmodel5 + outlayer,

                cmodel + outlayer,
                rmodel + outlayer,
                mmodel + outlayer
        };

        const rloss_t loss = loss_manager_t::instance().get("logistic");
        assert(loss);

        for (const string_t& cmd_network : cmd_networks)
        {
                log_info() << "<<< running network [" << cmd_network << "] ...";

                // create feed-forward network
                const rmodel_t model = model_manager_t::instance().get("forward-network", cmd_network);
                assert(model);
                model->resize(task, true);

                // select random samples
                samples_t samples;
                {
                        const ncv::timer_t timer;

                        sampler_t sampler(task);
                        sampler.setup(sampler_t::stype::uniform, cmd_samples).setup(sampler_t::atype::annotated);

                        samples = sampler.get();

                        log_info() << "<<< selected [" << samples.size()
                                   << "] random samples in " << timer.elapsed() << ".";
                }

                // simulate parameter loading & saving
                {
                        const ncv::timer_t timer;

                        vector_t params(model->psize());

                        const size_t tests = 1024;
                        for (size_t t = 0; t < tests; t ++)
                        {
                                model->save_params(params);
                                model->load_params(params);
                        }

                        log_info() << "<<< loaded & saved " << tests << "x " << model->psize()
                                   << " parameters in " << timer.elapsed() << ".";
                }

                // process the samples
                if (cmd_forward)
                {
                        accumulator_t ldata(*model, cmd_threads, "l2n-reg", criterion_t::type::value, 0.1);

                        const ncv::timer_t timer;
                        ldata.update(task, samples, *loss);

                        log_info() << "<<< processed [" << ldata.count() << "] forward samples in " << timer.elapsed() << ".";
                }

                if (cmd_backward)
                {
                        accumulator_t gdata(*model, cmd_threads, "l2n-reg", criterion_t::type::vgrad, 0.1);

                        const ncv::timer_t timer;
                        gdata.update(task, samples, *loss);

                        log_info() << "<<< processed [" << gdata.count() << "] backward samples in " << timer.elapsed() << ".";
                }

                log_info();
        }

        // OK
        log_info() << done;
        return EXIT_SUCCESS;
}
<commit_msg>default number of samples<commit_after>#include "nanocv.h"
#include "tasks/task_dummy.h"
#include <boost/program_options.hpp>

int main(int argc, char *argv[])
{
        ncv::init();

        using namespace ncv;

        // parse the command line
        boost::program_options::options_description po_desc("", 160);
        po_desc.add_options()("help,h", "test program");
        po_desc.add_options()("threads,t",
                boost::program_options::value<size_t>()->default_value(1),
                "number of threads to use [1, 64], 0 - use all available threads");
        po_desc.add_options()("samples,s",
                boost::program_options::value<size_t>()->default_value(10000),
                "number of samples to use [1000, 100000]");
        po_desc.add_options()("forward",
                "evaluate the \'forward\' pass (output)");
        po_desc.add_options()("backward",
                "evaluate the \'backward' pass (gradient)");

        boost::program_options::variables_map po_vm;
        boost::program_options::store(
                boost::program_options::command_line_parser(argc, argv).options(po_desc).run(),
                po_vm);
        boost::program_options::notify(po_vm);

        // check arguments and options
        if (	po_vm.empty() ||
                po_vm.count("help"))
        {
                std::cout << po_desc;
                return EXIT_FAILURE;
        }

        const size_t cmd_threads = math::clamp(po_vm["threads"].as<size_t>(), 0, 64);
        const size_t cmd_samples = math::clamp(po_vm["samples"].as<size_t>(), 1000, 100 * 1000);
        const bool cmd_forward = po_vm.count("forward");
        const bool cmd_backward = po_vm.count("backward");

        if (!cmd_forward && !cmd_backward)
        {
                std::cout << po_desc;
                return EXIT_FAILURE;
        }

        dummy_task_t task;
        task.set_rows(28);
        task.set_cols(28);
        task.set_color(color_mode::luma);
        task.set_outputs(10);
        task.set_folds(1);
        task.set_size(cmd_samples * 100);
        task.setup();

        const size_t cmd_outputs = task.n_outputs();

        const string_t lmodel0;
        const string_t lmodel1 = lmodel0 + "linear:dims=100;act-snorm;";
        const string_t lmodel2 = lmodel1 + "linear:dims=100;act-snorm;";
        const string_t lmodel3 = lmodel2 + "linear:dims=100;act-snorm;";
        const string_t lmodel4 = lmodel3 + "linear:dims=100;act-snorm;";
        const string_t lmodel5 = lmodel4 + "linear:dims=100;act-snorm;";

        string_t cmodel;
        cmodel = cmodel + "conv:dims=16,rows=6,cols=6,type=full;act-snorm;pool-max;";
        cmodel = cmodel + "conv:dims=32,rows=5,cols=5,type=full;act-snorm;pool-max;";
        cmodel = cmodel + "conv:dims=64,rows=4,cols=4,type=full;act-snorm;";

        string_t rmodel;
        rmodel = rmodel + "conv:dims=16,rows=6,cols=6,type=rand;act-snorm;pool-max;";
        rmodel = rmodel + "conv:dims=32,rows=5,cols=5,type=rand;act-snorm;pool-max;";
        rmodel = rmodel + "conv:dims=64,rows=4,cols=4,type=rand;act-snorm;";

        string_t mmodel;
        mmodel = mmodel + "conv:dims=16,rows=6,cols=6,type=mask;act-snorm;pool-max;";
        mmodel = mmodel + "conv:dims=32,rows=5,cols=5,type=mask;act-snorm;pool-max;";
        mmodel = mmodel + "conv:dims=64,rows=4,cols=4,type=mask;act-snorm;";

        const string_t outlayer = "linear:dims=" + text::to_string(cmd_outputs) + ";";

        strings_t cmd_networks =
        {
                lmodel0 + outlayer,
                lmodel1 + outlayer,
                lmodel2 + outlayer,
                lmodel3 + outlayer,
                lmodel4 + outlayer,
                lmodel5 + outlayer,

                cmodel + outlayer,
                rmodel + outlayer,
                mmodel + outlayer
        };

        const rloss_t loss = loss_manager_t::instance().get("logistic");
        assert(loss);

        for (const string_t& cmd_network : cmd_networks)
        {
                log_info() << "<<< running network [" << cmd_network << "] ...";

                // create feed-forward network
                const rmodel_t model = model_manager_t::instance().get("forward-network", cmd_network);
                assert(model);
                model->resize(task, true);

                // select random samples
                samples_t samples;
                {
                        const ncv::timer_t timer;

                        sampler_t sampler(task);
                        sampler.setup(sampler_t::stype::uniform, cmd_samples).setup(sampler_t::atype::annotated);

                        samples = sampler.get();

                        log_info() << "<<< selected [" << samples.size()
                                   << "] random samples in " << timer.elapsed() << ".";
                }

                // simulate parameter loading & saving
                {
                        const ncv::timer_t timer;

                        vector_t params(model->psize());

                        const size_t tests = 1024;
                        for (size_t t = 0; t < tests; t ++)
                        {
                                model->save_params(params);
                                model->load_params(params);
                        }

                        log_info() << "<<< loaded & saved " << tests << "x " << model->psize()
                                   << " parameters in " << timer.elapsed() << ".";
                }

                // process the samples
                if (cmd_forward)
                {
                        accumulator_t ldata(*model, cmd_threads, "l2n-reg", criterion_t::type::value, 0.1);

                        const ncv::timer_t timer;
                        ldata.update(task, samples, *loss);

                        log_info() << "<<< processed [" << ldata.count() << "] forward samples in " << timer.elapsed() << ".";
                }

                if (cmd_backward)
                {
                        accumulator_t gdata(*model, cmd_threads, "l2n-reg", criterion_t::type::vgrad, 0.1);

                        const ncv::timer_t timer;
                        gdata.update(task, samples, *loss);

                        log_info() << "<<< processed [" << gdata.count() << "] backward samples in " << timer.elapsed() << ".";
                }

                log_info();
        }

        // OK
        log_info() << done;
        return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
 *
 * Original Authors:
 *   Allen Bierbaum, Christopher Just,
 *   Patrick Hartling, Kevin Meinert,
 *   Carolina Cruz-Neira, Albert Baker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------------------------------------------------
 * File:          $RCSfile$
 * Date modified: $Date$
 * Version:       $Revision$
 * -----------------------------------------------------------------
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <gadget/Devices/DriverConfig.h>

#include <algorithm>
#include <boost/concept_check.hpp>

#include <gadget/Util/Debug.h>
#include <gadget/Type/DeviceConstructor.h>
#include <jccl/Config/ConfigElement.h>
#include <vpr/vpr.h>
#include <vpr/System.h>

// Fakespace pinch driver
#include <drivers/Fakespace/PinchGlove/PinchGloveStandalone.h>
// Gadgeteer pinch driver
#include <drivers/Fakespace/PinchGlove/PinchGlove.h>

extern "C"
{
   GADGET_DRIVER_EXPORT(void) initDevice(gadget::InputManager* inputMgr)
   {
      new gadget::DeviceConstructor<gadget::PinchGlove>(inputMgr);
   }
}

namespace gadget
{

std::string PinchGlove::getElementType()
{
   return "pinch_glove";
}

bool PinchGlove::config(jccl::ConfigElementPtr e)
{
   Input::config(e);
   mPortName = e->getProperty<std::string>("port");
   mBaud = e->getProperty<int>("baud");

   // This should have been set by Input(c)
   vprASSERT(mThread == NULL);

   return true;
}

PinchGlove::~PinchGlove ()
{
   stopSampling();      // Stop the glove
   delete mGlove;       // Delete the glove
}

bool PinchGlove::startSampling()
{
   if (NULL != mThread)
   {
      // Already sampling
      return 0;
   }
   else
   {
      // Create a new PinchGloveStandalone device.
      mGlove = new PinchGloveStandalone();

      // Formatting so that the [OK] or [FAILED] lines up with the rest.
      const int port_width(17);

      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] Connecting to PinchGlove on port: "
         << std::setiosflags(std::ios::right) << std::setfill(' ')
         << std::setw(port_width) << mPortName << "  ";

      // Attempt to connect to the PinchGlove.
      if(mGlove->connect(mPortName, mBaud).success())
      {
         vprDEBUG_CONTnl(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL) << "[ " << clrSetNORM(clrGREEN) << "OK" << clrRESET << " ]"
            << std::endl << vprDEBUG_FLUSH;
      }
      else
      {
         vprDEBUG_CONTnl(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL) << "[ " << clrSetNORM(clrRED) << "FAILED" << clrRESET << " ]"
            << std::endl << vprDEBUG_FLUSH;
         return 0;
      }

      // Create a vector of strings to hold information about PinchGlove.
      std::vector<std::string> info(0);

      // Query the PinchGlove for its harware information.
      if(!mGlove->printHardwareInformation(info).success())
      {
         return 0;
      }

      // Time stamps are on by default. (Pinch Glove Manual p. 8)
      if(!mGlove->setTimestampsOn(true).success())
      {
         return 0;
      }

      // Print out the hardware information.
      for(std::vector<std::string>::iterator itr = info.begin() ; itr != info.end() ; ++itr)
      {
         vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
            << (*itr) << std::endl << vprDEBUG_FLUSH;
      }

      mExitFlag = false;
      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_LVL)
      << "[PinchGlove] Spawning control thread." << std::endl  << vprDEBUG_FLUSH;

      // Create a functor that points at the sample method.
      vpr::ThreadMemberFunctor<PinchGlove>* memberFunctor =
         new vpr::ThreadMemberFunctor<PinchGlove>(this, &PinchGlove::controlLoop, NULL);

      // Create a new thread to handle the control.
      mThread = new vpr::Thread(memberFunctor);

      if (!mThread->valid())
      {
         return 0;
      }
      else
      {
         // We want to add an open hand sample first because the pinch glove
         // will not return data until there is a pinch. And until then, the
         // hand will be open.
         std::vector<DigitalData> digital_sample(10, 0);
         addDigitalSample(digital_sample);

         vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_LVL)
            << "[PinchGlove] PinchGlove is active " << std::endl
            << vprDEBUG_FLUSH;
         return 1;
      }
   }
}

void PinchGlove::controlLoop(void* nullParam)
{
   boost::ignore_unused_variable_warning(nullParam);

   vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << "[PinchGlove] Entered control thread"
      << std::endl << vprDEBUG_FLUSH;

   // Looping until we are told to exit.
   while ( !mExitFlag )
   {
      sample();
   }
}

bool PinchGlove::sample()
{
   // Create a vector of DigitalData's to hold our sample.
   std::vector<DigitalData> digital_sample(10);

   // Create a vector to retrieve data from the PinchGloveStandalone
   // driver with.
   std::vector<int> data(10, 0);

   // NOTE: The timestamp retrieved from the PinchGlove is not used at this
   //       time. This is partly because it saturates at 16382 ticks.
   //       (13.6511206 seconds) (p. 10 PinchGlove Manual)"
   int timestamp = 0;

   // Get data from PinchGlove;
   if(mGlove->sample(data, timestamp).success())
   {
      // Copy the data into a new digital sample.
      std::copy(data.begin(), data.end(), digital_sample.begin());

      // Add a new digital sample to the buffer.
      addDigitalSample(digital_sample);

      std::vector<GloveData> gloveData=getGloveDataFromDigitalData(digital_sample);
      addGloveSample(gloveData);

      // Debug print.
      //for(unsigned int i = 0 ; i < data.size() ; ++i)
      //{
      //   std::cout << (data[i] > 0 ? "1" : "0");
      //}
      //std::cout << std::endl;
      return 1;
   }
   return 0;
}

void PinchGlove::updateData()
{
   swapDigitalBuffers();
   swapGloveBuffers();
   return;
}

bool PinchGlove::stopSampling()
{
   if ( mThread != NULL )
   {
      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] Stopping PinchGlove." << std::endl << vprDEBUG_FLUSH;

      //Signal to thread that it should exit
      mExitFlag = true;

      mThread->join();
      delete mThread;
      mThread = NULL;

      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] PinchGlove stopped." << std::endl << vprDEBUG_FLUSH;
   }
   return 1;
}

} // namespace gadget
<commit_msg>Bug fxe: possible deadlock because a debug output statement was not calling vprDEBUG_FLUSH	to unlock the stream.<commit_after>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
 *
 * Original Authors:
 *   Allen Bierbaum, Christopher Just,
 *   Patrick Hartling, Kevin Meinert,
 *   Carolina Cruz-Neira, Albert Baker
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------------------------------------------------
 * File:          $RCSfile$
 * Date modified: $Date$
 * Version:       $Revision$
 * -----------------------------------------------------------------
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <gadget/Devices/DriverConfig.h>

#include <algorithm>
#include <boost/concept_check.hpp>

#include <gadget/Util/Debug.h>
#include <gadget/Type/DeviceConstructor.h>
#include <jccl/Config/ConfigElement.h>
#include <vpr/vpr.h>
#include <vpr/System.h>

// Fakespace pinch driver
#include <drivers/Fakespace/PinchGlove/PinchGloveStandalone.h>
// Gadgeteer pinch driver
#include <drivers/Fakespace/PinchGlove/PinchGlove.h>

extern "C"
{
   GADGET_DRIVER_EXPORT(void) initDevice(gadget::InputManager* inputMgr)
   {
      new gadget::DeviceConstructor<gadget::PinchGlove>(inputMgr);
   }
}

namespace gadget
{

std::string PinchGlove::getElementType()
{
   return "pinch_glove";
}

bool PinchGlove::config(jccl::ConfigElementPtr e)
{
   Input::config(e);
   mPortName = e->getProperty<std::string>("port");
   mBaud = e->getProperty<int>("baud");

   // This should have been set by Input(c)
   vprASSERT(mThread == NULL);

   return true;
}

PinchGlove::~PinchGlove ()
{
   stopSampling();      // Stop the glove
   delete mGlove;       // Delete the glove
}

bool PinchGlove::startSampling()
{
   if (NULL != mThread)
   {
      // Already sampling
      return 0;
   }
   else
   {
      // Create a new PinchGloveStandalone device.
      mGlove = new PinchGloveStandalone();

      // Formatting so that the [OK] or [FAILED] lines up with the rest.
      const int port_width(17);

      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] Connecting to PinchGlove on port: "
         << std::setiosflags(std::ios::right) << std::setfill(' ')
         << std::setw(port_width) << mPortName << "  " << vprDEBUG_FLUSH;

      // Attempt to connect to the PinchGlove.
      if(mGlove->connect(mPortName, mBaud).success())
      {
         vprDEBUG_CONTnl(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL) << "[ " << clrSetNORM(clrGREEN) << "OK" << clrRESET << " ]"
            << std::endl << vprDEBUG_FLUSH;
      }
      else
      {
         vprDEBUG_CONTnl(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL) << "[ " << clrSetNORM(clrRED) << "FAILED" << clrRESET << " ]"
            << std::endl << vprDEBUG_FLUSH;
         return 0;
      }

      // Create a vector of strings to hold information about PinchGlove.
      std::vector<std::string> info(0);

      // Query the PinchGlove for its harware information.
      if(!mGlove->printHardwareInformation(info).success())
      {
         return 0;
      }

      // Time stamps are on by default. (Pinch Glove Manual p. 8)
      if(!mGlove->setTimestampsOn(true).success())
      {
         return 0;
      }

      // Print out the hardware information.
      for(std::vector<std::string>::iterator itr = info.begin() ; itr != info.end() ; ++itr)
      {
         vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
            << (*itr) << std::endl << vprDEBUG_FLUSH;
      }

      mExitFlag = false;
      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_LVL)
      << "[PinchGlove] Spawning control thread." << std::endl  << vprDEBUG_FLUSH;

      // Create a functor that points at the sample method.
      vpr::ThreadMemberFunctor<PinchGlove>* memberFunctor =
         new vpr::ThreadMemberFunctor<PinchGlove>(this, &PinchGlove::controlLoop, NULL);

      // Create a new thread to handle the control.
      mThread = new vpr::Thread(memberFunctor);

      if (!mThread->valid())
      {
         return 0;
      }
      else
      {
         // We want to add an open hand sample first because the pinch glove
         // will not return data until there is a pinch. And until then, the
         // hand will be open.
         std::vector<DigitalData> digital_sample(10, 0);
         addDigitalSample(digital_sample);

         vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_LVL)
            << "[PinchGlove] PinchGlove is active " << std::endl
            << vprDEBUG_FLUSH;
         return 1;
      }
   }
}

void PinchGlove::controlLoop(void* nullParam)
{
   boost::ignore_unused_variable_warning(nullParam);

   vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << "[PinchGlove] Entered control thread"
      << std::endl << vprDEBUG_FLUSH;

   // Looping until we are told to exit.
   while ( !mExitFlag )
   {
      sample();
   }
}

bool PinchGlove::sample()
{
   // Create a vector of DigitalData's to hold our sample.
   std::vector<DigitalData> digital_sample(10);

   // Create a vector to retrieve data from the PinchGloveStandalone
   // driver with.
   std::vector<int> data(10, 0);

   // NOTE: The timestamp retrieved from the PinchGlove is not used at this
   //       time. This is partly because it saturates at 16382 ticks.
   //       (13.6511206 seconds) (p. 10 PinchGlove Manual)"
   int timestamp = 0;

   // Get data from PinchGlove;
   if(mGlove->sample(data, timestamp).success())
   {
      // Copy the data into a new digital sample.
      std::copy(data.begin(), data.end(), digital_sample.begin());

      // Add a new digital sample to the buffer.
      addDigitalSample(digital_sample);

      std::vector<GloveData> gloveData=getGloveDataFromDigitalData(digital_sample);
      addGloveSample(gloveData);

      // Debug print.
      //for(unsigned int i = 0 ; i < data.size() ; ++i)
      //{
      //   std::cout << (data[i] > 0 ? "1" : "0");
      //}
      //std::cout << std::endl;
      return 1;
   }
   return 0;
}

void PinchGlove::updateData()
{
   swapDigitalBuffers();
   swapGloveBuffers();
   return;
}

bool PinchGlove::stopSampling()
{
   if ( mThread != NULL )
   {
      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] Stopping PinchGlove." << std::endl << vprDEBUG_FLUSH;

      //Signal to thread that it should exit
      mExitFlag = true;

      mThread->join();
      delete mThread;
      mThread = NULL;

      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CONFIG_STATUS_LVL)
         << "[PinchGlove] PinchGlove stopped." << std::endl << vprDEBUG_FLUSH;
   }
   return 1;
}

} // namespace gadget
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: CTables.cxx,v $
 *
 *  $Revision: 1.9 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 05:35:59 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_CALC_TABLES_HXX_
#include "calc/CTables.hxx"
#endif
#ifndef _CONNECTIVITY_CALC_TABLE_HXX_
#include "calc/CTable.hxx"
#endif
#ifndef _CONNECTIVITY_FILE_CATALOG_HXX_
#include "file/FCatalog.hxx"
#endif
#ifndef _CONNECTIVITY_FILE_BCONNECTION_HXX_
#include "file/FConnection.hxx"
#endif
#ifndef _CONNECTIVITY_CALC_CATALOG_HXX_
#include "calc/CCatalog.hxx"
#endif
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif

using namespace ::comphelper;
using namespace connectivity;
using namespace connectivity::calc;
using namespace connectivity::file;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
namespace starutil      = ::com::sun::star::util;

sdbcx::ObjectType OCalcTables::createObject(const ::rtl::OUString& _rName)
{
    return new OCalcTable(this,(OCalcConnection*)static_cast<OFileCatalog&>(m_rParent).getConnection(),
                                        _rName,::rtl::OUString::createFromAscii("TABLE"));
}
// -------------------------------------------------------------------------

<commit_msg>INTEGRATION: CWS pchfix02 (1.9.166); FILE MERGED 2006/09/01 17:22:01 kaib 1.9.166.1: #i68856# Added header markers and pch files<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: CTables.cxx,v $
 *
 *  $Revision: 1.10 $
 *
 *  last change: $Author: obo $ $Date: 2006-09-17 02:20:11 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_connectivity.hxx"

#ifndef _CONNECTIVITY_CALC_TABLES_HXX_
#include "calc/CTables.hxx"
#endif
#ifndef _CONNECTIVITY_CALC_TABLE_HXX_
#include "calc/CTable.hxx"
#endif
#ifndef _CONNECTIVITY_FILE_CATALOG_HXX_
#include "file/FCatalog.hxx"
#endif
#ifndef _CONNECTIVITY_FILE_BCONNECTION_HXX_
#include "file/FConnection.hxx"
#endif
#ifndef _CONNECTIVITY_CALC_CATALOG_HXX_
#include "calc/CCatalog.hxx"
#endif
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif

using namespace ::comphelper;
using namespace connectivity;
using namespace connectivity::calc;
using namespace connectivity::file;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
namespace starutil      = ::com::sun::star::util;

sdbcx::ObjectType OCalcTables::createObject(const ::rtl::OUString& _rName)
{
    return new OCalcTable(this,(OCalcConnection*)static_cast<OFileCatalog&>(m_rParent).getConnection(),
                                        _rName,::rtl::OUString::createFromAscii("TABLE"));
}
// -------------------------------------------------------------------------

<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: MTable.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: hjs $ $Date: 2004-06-25 18:30:39 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_MOZAB_TABLE_HXX_
#define _CONNECTIVITY_MOZAB_TABLE_HXX_

#ifndef _CONNECTIVITY_SDBCX_TABLE_HXX_
#include "connectivity/sdbcx/VTable.hxx"
#endif
#ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
#endif
#ifndef _CONNECTIVITY_MOZAB_BCONNECTION_HXX_
#include "MConnection.hxx"
#endif

namespace connectivity
{
    namespace mozab
    {
        typedef connectivity::sdbcx::OTable OTable_TYPEDEF;

        ::rtl::OUString getTypeString(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xColProp);

        class OTable :  public OTable_TYPEDEF
        {
            ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData > m_xMetaData;
            OConnection* m_pConnection;

        public:
            OTable( sdbcx::OCollection* _pTables, OConnection* _pConnection);
            OTable( sdbcx::OCollection* _pTables,
                    OConnection* _pConnection,
                    const ::rtl::OUString& _Name,
                    const ::rtl::OUString& _Type,
                    const ::rtl::OUString& _Description = ::rtl::OUString(),
                    const ::rtl::OUString& _SchemaName = ::rtl::OUString(),
                    const ::rtl::OUString& _CatalogName = ::rtl::OUString()
                );

            OConnection* getConnection() { return m_pConnection;}

            sal_Bool isReadOnly() const { return sal_False; }
            virtual void refreshColumns();

            ::rtl::OUString getTableName() const { return m_Name; }
            ::rtl::OUString getSchema() const { return m_SchemaName; }

            // com::sun::star::lang::XUnoTunnel
            virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException);
            static ::com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
        };
    }
}
#endif // _CONNECTIVITY_MOZAB_TABLE_HXX_

<commit_msg>INTEGRATION: CWS ooo19126 (1.3.138); FILE MERGED 2005/09/05 17:24:25 rt 1.3.138.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: MTable.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 06:21:36 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_MOZAB_TABLE_HXX_
#define _CONNECTIVITY_MOZAB_TABLE_HXX_

#ifndef _CONNECTIVITY_SDBCX_TABLE_HXX_
#include "connectivity/sdbcx/VTable.hxx"
#endif
#ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
#endif
#ifndef _CONNECTIVITY_MOZAB_BCONNECTION_HXX_
#include "MConnection.hxx"
#endif

namespace connectivity
{
    namespace mozab
    {
        typedef connectivity::sdbcx::OTable OTable_TYPEDEF;

        ::rtl::OUString getTypeString(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xColProp);

        class OTable :  public OTable_TYPEDEF
        {
            ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData > m_xMetaData;
            OConnection* m_pConnection;

        public:
            OTable( sdbcx::OCollection* _pTables, OConnection* _pConnection);
            OTable( sdbcx::OCollection* _pTables,
                    OConnection* _pConnection,
                    const ::rtl::OUString& _Name,
                    const ::rtl::OUString& _Type,
                    const ::rtl::OUString& _Description = ::rtl::OUString(),
                    const ::rtl::OUString& _SchemaName = ::rtl::OUString(),
                    const ::rtl::OUString& _CatalogName = ::rtl::OUString()
                );

            OConnection* getConnection() { return m_pConnection;}

            sal_Bool isReadOnly() const { return sal_False; }
            virtual void refreshColumns();

            ::rtl::OUString getTableName() const { return m_Name; }
            ::rtl::OUString getSchema() const { return m_SchemaName; }

            // com::sun::star::lang::XUnoTunnel
            virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException);
            static ::com::sun::star::uno::Sequence< sal_Int8 > getUnoTunnelImplementationId();
        };
    }
}
#endif // _CONNECTIVITY_MOZAB_TABLE_HXX_

<|endoftext|>


<commit_before>/*****************************************************************************
 * Copyright (C) 2011-2012 Michael Krufky
 *
 * Author: Michael Krufky <mkrufky@linuxtv.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "output.h"
#include "log.h"

#define dprintf(fmt, arg...) __dprintf(DBG_OUTPUT, fmt, ##arg)

output_stream::output_stream()
  : f_kill_thread(false)
  , sock(-1)
  , ringbuffer()
  , stream_method(OUTPUT_STREAM_UDP)
{
	dprintf("()");
	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);
}

output_stream::~output_stream()
{
	dprintf("()");

	stop();
}

#if 1
output_stream::output_stream(const output_stream&)
{
	dprintf("(copy)");
	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);
}

output_stream& output_stream::operator= (const output_stream& cSource)
{
	dprintf("(operator=)");

	if (this == &cSource)
		return *this;

	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);

	return *this;
}
#endif

//static
void* output_stream::output_stream_thread(void *p_this)
{
	return static_cast<output_stream*>(p_this)->output_stream_thread();
}

#define OUTPUT_STREAM_PACKET_SIZE ((stream_method == OUTPUT_STREAM_UDP) ? 188*7 : 188*175)
void* output_stream::output_stream_thread()
{
	uint8_t *data = NULL;
	int buf_size;

	dprintf("()");

	/* push data from output_stream buffer to target */
	while (!f_kill_thread) {

		buf_size = ringbuffer.get_size();
		if (buf_size < OUTPUT_STREAM_PACKET_SIZE) {
			usleep(50*1000);
			continue;
		}

		buf_size = ringbuffer.read_ptr((void**)&data, OUTPUT_STREAM_PACKET_SIZE);
		stream(data, buf_size);
		ringbuffer.advance_read_ptr();
	}
	pthread_exit(NULL);
}

int output_stream::start()
{
	dprintf("()");

	int ret = pthread_create(&h_thread, NULL, output_stream_thread, this);

	if (0 != ret)
		perror("pthread_create() failed");

	return ret;
}

void output_stream::stop()
{
	dprintf("()");

	stop_without_wait();
#if 0
	while (-1 != sock_fd) {
		usleep(20*1000);
	}
#endif
	return;
}

int output_stream::push(uint8_t* p_data, int size)
{
	/* push data into output_stream buffer */
	return ringbuffer.write(p_data, size);
}

int output_stream::stream(uint8_t* p_data, int size)
{
	int ret = -1;

	/* stream data to target */
	switch (stream_method) {
	case OUTPUT_STREAM_UDP:
		ret = sendto(sock, p_data, size, 0, (struct sockaddr*) &ip_addr, sizeof(ip_addr));
		break;
	case OUTPUT_STREAM_TCP:
		while (0 >= ret) {
		  fd_set fds;
		  FD_ZERO(&fds);
		  FD_SET(sock, &fds);

		  struct timeval sel_timeout = {0, 10000};

		  ret = select(sock + 1, NULL, &fds, NULL, &sel_timeout);
		  if (ret < 0)
		    perror("error!");
		  usleep(20*1000);
		}
		ret = send(sock, p_data, size, 0);
		break;
	}
	return ret;
}

int output_stream::add(char* target)
{
	char *ip;
	uint16_t port;
	bool b_tcp = false;
	bool b_udp = false;

	dprintf("(-->%s)", target);

	if (strstr(target, ":")) {
		ip = strtok(target, ":");
		if (strstr(ip, "tcp"))
			b_tcp = true;
		else
		if (strstr(ip, "udp"))
			b_udp = true;

		if ((b_tcp) || (b_udp)) {
			ip = strtok(NULL, ":");
			if (strstr(ip, "//") == ip)
				ip += 2;
		}
		// else ip = proto;
		port = atoi(strtok(NULL, ":"));
	} else {
		b_tcp = false;
		ip = target;
		port = 1234;
	}

	if (sock >= 0)
		close(sock);

	sock = socket(AF_INET, (b_tcp) ? SOCK_STREAM : SOCK_DGRAM, (b_tcp) ? IPPROTO_TCP : IPPROTO_UDP);
	if (sock >= 0) {

		int fl = fcntl(sock, F_GETFL, 0);
		if (fcntl(sock, F_SETFL, fl | O_NONBLOCK) < 0)
			perror("set non-blocking failed");

		memset(&ip_addr, 0, sizeof(ip_addr));
		ip_addr.sin_family = AF_INET;
		ip_addr.sin_port   = htons(port);
		if (inet_aton(ip, &ip_addr.sin_addr) == 0) {

			perror("ip address translation failed");
			return -1;
		} else
			ringbuffer.reset();

		if (b_tcp) {
			if ((connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr)) < 0) && (errno != EINPROGRESS)) {
				perror("failed to connect to server");
				return -1;
			}
			stream_method = OUTPUT_STREAM_TCP;
		} else {
			stream_method = OUTPUT_STREAM_UDP;
		}
	} else {
		perror("socket failed");
		return -1;
	}
	dprintf("~(-->%s)", target);

	return 0;
}

/* ----------------------------------------------------------------- */

output::output()
  : f_kill_thread(false)
  , ringbuffer()
  , num_targets(0)
  , options(OUTPUT_NONE)
{
	dprintf("()");

	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE*2);

	memset(&output_streams, 0, sizeof(output_streams));

	output_streams.clear();
}

output::~output()
{
	dprintf("()");

	stop();

	output_streams.clear();
}

#if 0
output::output(const output&)
{
	dprintf("(copy)");

	output_streams.clear();
}

output& output::operator= (const output& cSource)
{
	dprintf("(operator=)");

	if (this == &cSource)
		return *this;

	output_streams.clear();

	return *this;
}
#endif

//static
void* output::output_thread(void *p_this)
{
	return static_cast<output*>(p_this)->output_thread();
}

void* output::output_thread()
{
	int buf_size;
	uint8_t *data = NULL;

	/* push data from main output buffer into output_stream buffers */
	while (!f_kill_thread) {

		buf_size = ringbuffer.get_size();
		//data = NULL;
		if (buf_size) {
			buf_size = ringbuffer.read_ptr((void**)&data, buf_size);

			for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
				iter->second.push(data, buf_size);
			ringbuffer.advance_read_ptr();
		} else {
			usleep(50*1000);
		}

	}
	pthread_exit(NULL);
}

int output::start()
{
	dprintf("()");

	int ret = pthread_create(&h_thread, NULL, output_thread, this);

	if (0 != ret)
		perror("pthread_create() failed");

	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.start();

	return ret;
}

void output::stop()
{
	dprintf("()");

	/* call stop_without_wait() on everybody first before we call stop() on everybody, which is a blocking function */
	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.stop_without_wait();

	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.stop();

	stop_without_wait();
#if 0
	while (-1 != sock_fd) {
		usleep(20*1000);
	}
#endif
	return;
}

int output::push(uint8_t* p_data, int size)
{
	/* push data into output buffer */
	return ringbuffer.write(p_data, size);
}

int output::push(uint8_t* p_data, enum output_options opt)
{
	return (((!options) || (!opt)) || (opt & options)) ? push(p_data, 188) : 0;
}

int output::add(char* target)
{
	dprintf("(%d->%s)", num_targets, target);
	/* push data into output buffer */
	int ret = output_streams[num_targets].add(target);
	if (ret == 0)
		num_targets++;
	else
		dprintf("failed to add target #%d: %s", num_targets, target);

	dprintf("~(%d->%s)", (ret == 0) ? num_targets - 1 : num_targets, target);

	return ret;
}
<commit_msg>output: dont allocate buffers unless at least one target has been specified<commit_after>/*****************************************************************************
 * Copyright (C) 2011-2012 Michael Krufky
 *
 * Author: Michael Krufky <mkrufky@linuxtv.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "output.h"
#include "log.h"

#define dprintf(fmt, arg...) __dprintf(DBG_OUTPUT, fmt, ##arg)

output_stream::output_stream()
  : f_kill_thread(false)
  , sock(-1)
  , ringbuffer()
  , stream_method(OUTPUT_STREAM_UDP)
{
	dprintf("()");
	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);
}

output_stream::~output_stream()
{
	dprintf("()");

	stop();
}

#if 1
output_stream::output_stream(const output_stream&)
{
	dprintf("(copy)");
	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);
}

output_stream& output_stream::operator= (const output_stream& cSource)
{
	dprintf("(operator=)");

	if (this == &cSource)
		return *this;

	memset(&ringbuffer, 0, sizeof(ringbuffer));
	ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE);

	return *this;
}
#endif

//static
void* output_stream::output_stream_thread(void *p_this)
{
	return static_cast<output_stream*>(p_this)->output_stream_thread();
}

#define OUTPUT_STREAM_PACKET_SIZE ((stream_method == OUTPUT_STREAM_UDP) ? 188*7 : 188*175)
void* output_stream::output_stream_thread()
{
	uint8_t *data = NULL;
	int buf_size;

	dprintf("()");

	/* push data from output_stream buffer to target */
	while (!f_kill_thread) {

		buf_size = ringbuffer.get_size();
		if (buf_size < OUTPUT_STREAM_PACKET_SIZE) {
			usleep(50*1000);
			continue;
		}

		buf_size = ringbuffer.read_ptr((void**)&data, OUTPUT_STREAM_PACKET_SIZE);
		stream(data, buf_size);
		ringbuffer.advance_read_ptr();
	}
	pthread_exit(NULL);
}

int output_stream::start()
{
	dprintf("()");

	int ret = pthread_create(&h_thread, NULL, output_stream_thread, this);

	if (0 != ret)
		perror("pthread_create() failed");

	return ret;
}

void output_stream::stop()
{
	dprintf("()");

	stop_without_wait();
#if 0
	while (-1 != sock_fd) {
		usleep(20*1000);
	}
#endif
	return;
}

int output_stream::push(uint8_t* p_data, int size)
{
	/* push data into output_stream buffer */
	return ringbuffer.write(p_data, size);
}

int output_stream::stream(uint8_t* p_data, int size)
{
	int ret = -1;

	/* stream data to target */
	switch (stream_method) {
	case OUTPUT_STREAM_UDP:
		ret = sendto(sock, p_data, size, 0, (struct sockaddr*) &ip_addr, sizeof(ip_addr));
		break;
	case OUTPUT_STREAM_TCP:
		while (0 >= ret) {
		  fd_set fds;
		  FD_ZERO(&fds);
		  FD_SET(sock, &fds);

		  struct timeval sel_timeout = {0, 10000};

		  ret = select(sock + 1, NULL, &fds, NULL, &sel_timeout);
		  if (ret < 0)
		    perror("error!");
		  usleep(20*1000);
		}
		ret = send(sock, p_data, size, 0);
		break;
	}
	return ret;
}

int output_stream::add(char* target)
{
	char *ip;
	uint16_t port;
	bool b_tcp = false;
	bool b_udp = false;

	dprintf("(-->%s)", target);

	if (strstr(target, ":")) {
		ip = strtok(target, ":");
		if (strstr(ip, "tcp"))
			b_tcp = true;
		else
		if (strstr(ip, "udp"))
			b_udp = true;

		if ((b_tcp) || (b_udp)) {
			ip = strtok(NULL, ":");
			if (strstr(ip, "//") == ip)
				ip += 2;
		}
		// else ip = proto;
		port = atoi(strtok(NULL, ":"));
	} else {
		b_tcp = false;
		ip = target;
		port = 1234;
	}

	if (sock >= 0)
		close(sock);

	sock = socket(AF_INET, (b_tcp) ? SOCK_STREAM : SOCK_DGRAM, (b_tcp) ? IPPROTO_TCP : IPPROTO_UDP);
	if (sock >= 0) {

		int fl = fcntl(sock, F_GETFL, 0);
		if (fcntl(sock, F_SETFL, fl | O_NONBLOCK) < 0)
			perror("set non-blocking failed");

		memset(&ip_addr, 0, sizeof(ip_addr));
		ip_addr.sin_family = AF_INET;
		ip_addr.sin_port   = htons(port);
		if (inet_aton(ip, &ip_addr.sin_addr) == 0) {

			perror("ip address translation failed");
			return -1;
		} else
			ringbuffer.reset();

		if (b_tcp) {
			if ((connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr)) < 0) && (errno != EINPROGRESS)) {
				perror("failed to connect to server");
				return -1;
			}
			stream_method = OUTPUT_STREAM_TCP;
		} else {
			stream_method = OUTPUT_STREAM_UDP;
		}
	} else {
		perror("socket failed");
		return -1;
	}
	dprintf("~(-->%s)", target);

	return 0;
}

/* ----------------------------------------------------------------- */

output::output()
  : f_kill_thread(false)
  , ringbuffer()
  , num_targets(0)
  , options(OUTPUT_NONE)
{
	dprintf("()");

	memset(&output_streams, 0, sizeof(output_streams));

	output_streams.clear();
}

output::~output()
{
	dprintf("()");

	stop();

	output_streams.clear();
}

#if 0
output::output(const output&)
{
	dprintf("(copy)");

	output_streams.clear();
}

output& output::operator= (const output& cSource)
{
	dprintf("(operator=)");

	if (this == &cSource)
		return *this;

	output_streams.clear();

	return *this;
}
#endif

//static
void* output::output_thread(void *p_this)
{
	return static_cast<output*>(p_this)->output_thread();
}

void* output::output_thread()
{
	int buf_size;
	uint8_t *data = NULL;

	/* push data from main output buffer into output_stream buffers */
	while (!f_kill_thread) {

		buf_size = ringbuffer.get_size();
		//data = NULL;
		if (buf_size) {
			buf_size = ringbuffer.read_ptr((void**)&data, buf_size);

			for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
				iter->second.push(data, buf_size);
			ringbuffer.advance_read_ptr();
		} else {
			usleep(50*1000);
		}

	}
	pthread_exit(NULL);
}

int output::start()
{
	dprintf("()");

	int ret = pthread_create(&h_thread, NULL, output_thread, this);

	if (0 != ret)
		perror("pthread_create() failed");

	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.start();

	return ret;
}

void output::stop()
{
	dprintf("()");

	/* call stop_without_wait() on everybody first before we call stop() on everybody, which is a blocking function */
	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.stop_without_wait();

	for (output_stream_map::iterator iter = output_streams.begin(); iter != output_streams.end(); ++iter)
		iter->second.stop();

	stop_without_wait();
#if 0
	while (-1 != sock_fd) {
		usleep(20*1000);
	}
#endif
	return;
}

int output::push(uint8_t* p_data, int size)
{
	/* push data into output buffer */
	return ringbuffer.write(p_data, size);
}

int output::push(uint8_t* p_data, enum output_options opt)
{
	return (((!options) || (!opt)) || (opt & options)) ? push(p_data, 188) : 0;
}

int output::add(char* target)
{
	dprintf("(%d->%s)", num_targets, target);

	/* allocates out buffer if and only if we have at least one target */
	if (ringbuffer.get_capacity() <= 0)
		ringbuffer.set_capacity(OUTPUT_STREAM_BUF_SIZE*2);

	/* push data into output buffer */
	int ret = output_streams[num_targets].add(target);
	if (ret == 0)
		num_targets++;
	else
		dprintf("failed to add target #%d: %s", num_targets, target);

	dprintf("~(%d->%s)", (ret == 0) ? num_targets - 1 : num_targets, target);

	return ret;
}
<|endoftext|>


<commit_before>// coding: utf-8

#ifndef TASK_MECHANICS_HPP
#	error	"Don't include this file directly, use 'task_mechanics.hpp' instead!"
#endif

task::Mechanics::Mechanics(Leds &leds, Buttons &buttons)
:	leds(leds), buttons(buttons),
	isCalibratedX(false), isCalibratedZ(false),
	correctionX(-20), correctionZ(13),
	xMotor(&OCR1AL, &TCCR1B, t1_steps), zMotor(&OCR0A, &TCCR0B, t0_steps),
	motorTimeout(30000)
{
}

void
task::Mechanics::initialize()
{
	// setup CTC mode for Z axis
	TCCR0A = (1 << COM0A0) | (1 << WGM01);
	TCCR0B = 0;
	OCR0A = 0;
	TIMSK0 = (1 << OCIE0A);

	// setup CTC mode for X axis
	TCCR1A = (1 << COM1A0);
	TCCR1B = (1 << WGM12);
	OCR1A = 0;
	TIMSK1 = (1 << OCIE1A);

	XZ_Enable::setOutput(xpcc::Gpio::Low);

	releaseMotors();
	xMotor.initialize();
	zMotor.initialize();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateX(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	if (buttons.isX_AxisLimitPressed())
	{
		xMotor.rotateBy(100, 1000);
		CO_WAIT_WHILE(xMotor.isRunning());
	}

	xMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isX_AxisLimitPressed() || timeout.isExpired());
	xMotor.stop();

	if ( (isCalibratedX = !timeout.isExpired()) )
	{
		xMotor.rotateBy(correctionX, 200);
		CO_WAIT_WHILE(xMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateZ(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.stop();
	if (buttons.isZ_AxisLimitPressed())
	{
		zMotor.rotateBy(100, 1000);
		CO_WAIT_WHILE(zMotor.isRunning());
		zMotor.stop();
	}

	zMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isZ_AxisLimitPressed() || timeout.isExpired());

	zMotor.stop();
	if ( (isCalibratedZ = !timeout.isExpired()) )
	{
		zMotor.rotateBy(correctionZ, 300);
		CO_WAIT_WHILE(zMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::rotateForward(void *ctx)
{
	CO_BEGIN(ctx);

	if (!isCalibrated())
		CO_RETURN(false);

	startMotors();

	xMotor.rotateBy(3600, 10000);
	CO_WAIT_WHILE(xMotor.isRunning());

	zMotor.rotateBy(3600, 10000);
	CO_WAIT_WHILE(zMotor.isRunning());

	leds.resetBusy();
	isCalibratedX = false;
	isCalibratedZ = false;
	CO_RETURN(true);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::rotateBackward(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.rotateBy(-3600, 10000);
	CO_WAIT_WHILE(zMotor.isRunning());

	xMotor.rotateBy(-3600, 10000);
	CO_WAIT_WHILE(xMotor.isRunning());

	releaseMotors();
	CO_RETURN(true);

	CO_END();
}

void
task::Mechanics::startMotors()
{
	leds.setBusy();
	XZ_Enable::set();
	motorTimeout.restart(30000);
}

void
task::Mechanics::stopMotors()
{
	xMotor.stop();
	zMotor.stop();
	stopCoroutine();
	isCalibratedX = false;
	isCalibratedZ = false;
	leds.resetBusy();
}

void
task::Mechanics::releaseMotors()
{
	stopMotors();
	XZ_Enable::reset();
	leds.resetMechanicalError();
}

void
task::Mechanics::update()
{
	xMotor.update();
	zMotor.update();
	this->run();
}

bool
task::Mechanics::run()
{
	PT_BEGIN();

	while(true)
	{
		if (!xMotor.isRunning() && !zMotor.isRunning() && motorTimeout.isExpired())
		{
			stopMotors();
			XZ_Enable::reset();
		}

		PT_YIELD();
	}

	PT_END();
}
<commit_msg>Make mechanics a little faster.<commit_after>// coding: utf-8

#ifndef TASK_MECHANICS_HPP
#	error	"Don't include this file directly, use 'task_mechanics.hpp' instead!"
#endif

task::Mechanics::Mechanics(Leds &leds, Buttons &buttons)
:	leds(leds), buttons(buttons),
	isCalibratedX(false), isCalibratedZ(false),
	correctionX(-40), correctionZ(15),
	xMotor(&OCR1AL, &TCCR1B, t1_steps), zMotor(&OCR0A, &TCCR0B, t0_steps),
	motorTimeout(30000)
{
}

void
task::Mechanics::initialize()
{
	// setup CTC mode for Z axis
	TCCR0A = (1 << COM0A0) | (1 << WGM01);
	TCCR0B = 0;
	OCR0A = 0;
	TIMSK0 = (1 << OCIE0A);

	// setup CTC mode for X axis
	TCCR1A = (1 << COM1A0);
	TCCR1B = (1 << WGM12);
	OCR1A = 0;
	TIMSK1 = (1 << OCIE1A);

	XZ_Enable::setOutput(xpcc::Gpio::Low);

	releaseMotors();
	xMotor.initialize();
	zMotor.initialize();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateX(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	if (buttons.isX_AxisLimitPressed())
	{
		xMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(xMotor.isRunning());
	}

	xMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isX_AxisLimitPressed() || timeout.isExpired());
	xMotor.stop();

	if ( (isCalibratedX = !timeout.isExpired()) )
	{
		xMotor.rotateBy(correctionX, 200);
		CO_WAIT_WHILE(xMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::calibrateZ(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.stop();
	if (buttons.isZ_AxisLimitPressed())
	{
		zMotor.rotateBy(100, 500);
		CO_WAIT_WHILE(zMotor.isRunning());
		zMotor.stop();
	}

	zMotor.setSpeed(-100);
	timeout.restart(18000);
	CO_WAIT_UNTIL(buttons.isZ_AxisLimitPressed() || timeout.isExpired());

	zMotor.stop();
	if ( (isCalibratedZ = !timeout.isExpired()) )
	{
		zMotor.rotateBy(correctionZ, 300);
		CO_WAIT_WHILE(zMotor.isRunning());
		leds.resetMechanicalError();
		leds.resetBusy();
		CO_RETURN(true);
	}

	leds.setMechanicalError();
	leds.resetBusy();
	CO_RETURN(false);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::rotateForward(void *ctx)
{
	CO_BEGIN(ctx);

	if (!isCalibrated())
		CO_RETURN(false);

	startMotors();

	xMotor.rotateBy(3600, 10000);
	CO_WAIT_WHILE(xMotor.isRunning());

	zMotor.rotateBy(3600, 10000);
	CO_WAIT_WHILE(zMotor.isRunning());

	leds.resetBusy();
	isCalibratedX = false;
	isCalibratedZ = false;
	CO_RETURN(true);

	CO_END();
}

xpcc::co::Result<bool>
task::Mechanics::rotateBackward(void *ctx)
{
	CO_BEGIN(ctx);
	startMotors();

	zMotor.rotateBy(-3600, 5000);
	CO_WAIT_WHILE(zMotor.isRunning());

	xMotor.rotateBy(-3600, 5000);
	CO_WAIT_WHILE(xMotor.isRunning());

	releaseMotors();
	CO_RETURN(true);

	CO_END();
}

void
task::Mechanics::startMotors()
{
	leds.setBusy();
	XZ_Enable::set();
	motorTimeout.restart(30000);
}

void
task::Mechanics::stopMotors()
{
	xMotor.stop();
	zMotor.stop();
	stopCoroutine();
	isCalibratedX = false;
	isCalibratedZ = false;
	leds.resetBusy();
}

void
task::Mechanics::releaseMotors()
{
	stopMotors();
	XZ_Enable::reset();
	leds.resetMechanicalError();
}

void
task::Mechanics::update()
{
	xMotor.update();
	zMotor.update();
	this->run();
}

bool
task::Mechanics::run()
{
	PT_BEGIN();

	while(true)
	{
		if (!xMotor.isRunning() && !zMotor.isRunning() && motorTimeout.isExpired())
		{
			stopMotors();
			XZ_Enable::reset();
		}

		PT_YIELD();
	}

	PT_END();
}
<|endoftext|>


<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/tab_contents/render_view_context_menu.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"

namespace {

class TestRenderViewContextMenu : public RenderViewContextMenu {
 public:
  TestRenderViewContextMenu(TabContents* tab_contents, ContextMenuParams params)
      : RenderViewContextMenu(tab_contents, params) { }

  virtual void PlatformInit() { }
  virtual bool GetAcceleratorForCommandId(
      int command_id,
      ui::Accelerator* accelerator) {
    return false;
  }

  bool IsItemPresent(int command_id) {
    return menu_model_.GetIndexOfCommandId(command_id) != -1;
  }
};

class RegisterProtocolHandlerBrowserTest : public InProcessBrowserTest {
 public:
  RegisterProtocolHandlerBrowserTest() {
  }

  TestRenderViewContextMenu* CreateContextMenu(GURL url) {
    ContextMenuParams params;
    params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
    params.link_url = url;
    TabContents* tab_contents = browser()->GetSelectedTabContents();
    params.page_url = tab_contents->controller().GetActiveEntry()->url();
#if defined(OS_MACOSX)
    params.writing_direction_default = 0;
    params.writing_direction_left_to_right = 0;
    params.writing_direction_right_to_left = 0;
#endif  // OS_MACOSX
    TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
        browser()->GetSelectedTabContents(), params);
    menu->Init();
    return menu;
  }
};

IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
    ContextMenuEntryAppearsForHandledUrls) {
  scoped_ptr<TestRenderViewContextMenu> menu(
      CreateContextMenu(GURL("http://www.google.com/")));
  ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));

  ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(
        std::string("web+search"),
        GURL("http://www.google.com/%s"),
        UTF8ToUTF16(std::string("Test handler")));
  ProtocolHandlerRegistry* registry =
      browser()->profile()->GetProtocolHandlerRegistry();

  GURL url("web+search:testing");
  registry->OnAcceptRegisterProtocolHandler(handler);
  ASSERT_TRUE(registry->IsHandledProtocol("web+search"));
  ASSERT_EQ(registry->GetHandlersFor(url.scheme()).size(), (size_t) 1);
  menu.reset(CreateContextMenu(url));
  ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
}

}  // namespace
<commit_msg>Fix formatting nit I forgot to fix during code review.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/tab_contents/render_view_context_menu.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"

namespace {

class TestRenderViewContextMenu : public RenderViewContextMenu {
 public:
  TestRenderViewContextMenu(TabContents* tab_contents, ContextMenuParams params)
      : RenderViewContextMenu(tab_contents, params) { }

  virtual void PlatformInit() { }
  virtual bool GetAcceleratorForCommandId(
      int command_id,
      ui::Accelerator* accelerator) {
    return false;
  }

  bool IsItemPresent(int command_id) {
    return menu_model_.GetIndexOfCommandId(command_id) != -1;
  }
};

class RegisterProtocolHandlerBrowserTest : public InProcessBrowserTest {
 public:
  RegisterProtocolHandlerBrowserTest() { }

  TestRenderViewContextMenu* CreateContextMenu(GURL url) {
    ContextMenuParams params;
    params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
    params.link_url = url;
    TabContents* tab_contents = browser()->GetSelectedTabContents();
    params.page_url = tab_contents->controller().GetActiveEntry()->url();
#if defined(OS_MACOSX)
    params.writing_direction_default = 0;
    params.writing_direction_left_to_right = 0;
    params.writing_direction_right_to_left = 0;
#endif  // OS_MACOSX
    TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
        browser()->GetSelectedTabContents(), params);
    menu->Init();
    return menu;
  }
};

IN_PROC_BROWSER_TEST_F(RegisterProtocolHandlerBrowserTest,
    ContextMenuEntryAppearsForHandledUrls) {
  scoped_ptr<TestRenderViewContextMenu> menu(
      CreateContextMenu(GURL("http://www.google.com/")));
  ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));

  ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(
        std::string("web+search"),
        GURL("http://www.google.com/%s"),
        UTF8ToUTF16(std::string("Test handler")));
  ProtocolHandlerRegistry* registry =
      browser()->profile()->GetProtocolHandlerRegistry();

  GURL url("web+search:testing");
  registry->OnAcceptRegisterProtocolHandler(handler);
  ASSERT_TRUE(registry->IsHandledProtocol("web+search"));
  ASSERT_EQ(registry->GetHandlersFor(url.scheme()).size(), (size_t) 1);
  menu.reset(CreateContextMenu(url));
  ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKWITH));
}

}  // namespace
<|endoftext|>