void deposit(string rec) {
		while (count == lenght - 1) {
			not_full.wait();
		}
		
		buffer[rear] = rec;
		if (rear < lenght)
			rear++;
		if (rear == lenght)
			rear = 0;
		count++;
		not_empty.post();
	}	
	string fetch() {
		while (count == 0) {
			not_empty.wait();
		}
		string result = buffer[front];

		if (front < lenght)
			front = (front + 1) % lenght;
		if (front == lenght)
			front = 0;
		count--;
		not_full.post();
		//buffer[front] = "";
		return result;
	}
Exemple #3
0
NMBLSEXPORT void handleUnLoadLibrary(void)
{
    done = true;
    sign.post();

    // Currently throwing: what():  boost: mutex lock failed in pthread_mutex_lock: Invalid argument
    // But given we are using an atomic done flag - is this needed?
    // worker_thread.join_all();

    for ( auto it = dbhandles.begin(); it != dbhandles.end(); it++)
    {
        sqlite3_close_v2(it->second);
    }

    std::cout << "Oppertunity to clean up..." << std::endl;
}
Exemple #4
0
NMBLSEXPORT bool handleWebRequest(std::string method, websockcon ref, std::string webfilename, ValuePair &queryparams, ValuePair &httpheaders, boost::function<void(std::string, std::string, std::string)> postResponceCallback)
{
    // Only supported method (at the moment?)
    if ( "GET" != method )
    {
        return false;
    }
    // First check if we have a statement to process for this request.
    std::string filename;
    // This should be long enough
    filename.reserve(512);

    filename += docroot;
    filename += boost::filesystem::path::preferred_separator;

    // To get here we must have got the host header
    std::string host = httpheaders["host"];
    filename += host;
    filename += boost::filesystem::path::preferred_separator;

    filename += "sqlite";
    filename += boost::filesystem::path::preferred_separator;
    std::string hostroot = filename;

    filename += "files";
    filename += boost::filesystem::path::preferred_separator;
    filename += webfilename;

    if (is_running_as_debug) std::cout << "Checking for sqlite file " << filename << std::endl;

    try{
        if ( !boost::filesystem::is_regular_file(filename) )
        {
            if ( !boost::filesystem::is_directory(filename) )
            {
                return false;
            }
            else
            {
                filename += boost::filesystem::path::preferred_separator;
                filename += "index";

                if ( !boost::filesystem::is_regular_file(filename) )
                {
                    // nothing to see here!
                    return false;
                }
            }
        }
    }
    catch (boost::filesystem::filesystem_error)
    {
        return false;
    }

    // Second check we have not created too much of a backlog
    if ( backlog >= WORKERBACKLOGSIZE )
    {
        // Post a 500 error of some form...
        return true;
    }

    // Now post it to the workers.
    int currentindex = nextavailable % WORKERBACKLOGSIZE ;
    nextavailable++;
    backlog++;

    auto it = requests.begin() + currentindex;
    it->ref = ref;
    it->filename = filename;
    it->host = host;
    it->hostroot = hostroot;
    it->postResponceCallback = postResponceCallback;
    it->http_headers = httpheaders;
    it->params = queryparams;

    workqueue.push(currentindex);
    sign.post();

    return true;
}
Exemple #5
0
void sqliteworker(void)
{
    while(!done)
    {
        sign.wait();
        int nexttoworkon;
        while (workqueue.pop(nexttoworkon))
        {
            auto workingon = requests.begin() + nexttoworkon;
            std::string sqlfile = workingon->hostroot +  "data.sqlite";
            std::string sqlinitfile = workingon->hostroot +  "data.init";

            void (*addJsonRowFunction)(std::ostringstream &output, int row, sqlite3_stmt *stmt) = addJsonTableRow;
            void (*finiJsonFunction)(std::ostringstream &output, bool haverows) = finiJsonTableRow;

            try
            {
                workingon->params.at("formatdatatable");
                addJsonRowFunction = addGDataTableRow;
                finiJsonFunction = finiGDataTable;
            }
            catch (const std::out_of_range &x)
            {

            }

            boost::posix_time::ptime starttime;
            measureuSTime(starttime, is_running_as_debug);

            sqlite3 *dbhandle = NULL;
            try
            {
                dbhandle = dbhandles.at(workingon->host);
            }
            catch(const std::out_of_range &x)
            {
                bool needscreating = false;
                if ( !boost::filesystem::is_regular_file(sqlfile) )
                {
                    needscreating = true;
                }

                std::cout << "Need to open db" << std::endl;
                if ( SQLITE_OK == sqlite3_open_v2(sqlfile.c_str(),  &dbhandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))
                {
                    dbhandles[workingon->host] = dbhandle;
                    dbhandle = dbhandle;

                    if (needscreating)
                    {
                        char *errmsg = 0;
                        std::ifstream insql(sqlinitfile);
                        if ( !insql.fail())
                        {
                            std::string createsql(static_cast<std::stringstream const&>(std::stringstream() << insql.rdbuf()).str());
                            int rc = sqlite3_exec(dbhandle, createsql.c_str(), NULL, 0, &errmsg);
                            if( rc != SQLITE_OK )
                            {
                                std::cerr << "Error running SQL: " <<  errmsg <<std::endl;

                                std::ostringstream doc;
                                genHttpDoc("", doc);

                                workingon->postResponceCallback("500", errmsg, doc.str());
                                sqlite3_free(errmsg);

                                sqlite3_close_v2(dbhandle);
                                backlog--;
                                continue;
                            }

                            measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_exec - init)");
                        }
                    }
                }
            }

            if ( NULL != dbhandle )
            {
                measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_open_v2)");

                try
                {
                    std::string insql;
                    readFile(workingon->filename, insql);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (read sql)");

                    sqlite3_stmt *stmt;
                    const char *sqlltail;
                    int rc = sqlite3_prepare_v2(dbhandle, insql.c_str(), insql.size(), &stmt, &sqlltail);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_prepare_v2)");

                    if( rc != SQLITE_OK )
                    {
                        std::string errormsg = "{error: \"";
                        errormsg += "Error preparing SQL statement";

                        std::cerr << "Error preparing: " << insql << std::endl;

                        errormsg +=  "\"}";

                        std::ostringstream doc;
                        genHttpDoc("", doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                    else
                    {
                        getAndBindParamsFromSql(insql, stmt, workingon);

                        measureuSTime(starttime, is_running_as_debug, "Time taken (bind)");

                        std::ostringstream jsondoc;
                        bool haverows = false;

                        // Have some sencible limit
                        for ( int row = 0; row < 10000; row++)
                        {
                            int step_result = sqlite3_step(stmt);
                            if ( SQLITE_ROW != step_result )
                            {
                                // Probably SQLITE_DONE
                                break;
                            }

                            haverows = true;
                            addJsonRowFunction(jsondoc, row, stmt);
                            //addGDataTableRow(jsondoc, row, stmt);
                        }
                        sqlite3_finalize(stmt);

                        //finiGDataTable(jsondoc, haverows);
                        finiJsonFunction(jsondoc, haverows);

                        std::ostringstream doc;
                        genHttpDoc(jsondoc.str(), doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                }
                catch (std::ifstream::failure e)
                {
                    std::cerr << "Exception opening/reading/closing file: " << workingon->filename << std::endl;
                }
            }
            else
            {
                workingon->postResponceCallback("404", "Not found", "Something bad just happened...");
            }

            //sqlite3_close_v2(dbhandle);
            backlog--;
        }
    }
}
Exemple #6
0
void worker(const std::string& s) 
{
	g_iosem.wait();
	std::cout<<s<<std::endl;
}