/* Function to blink the LED to show the error code
 * caused by M0 image boot failure
 */
static void booting_m0_failure(uint32_t msec)
{
	int32_t cnt = 60000 / (msec * 2);
	DEBUGSTR("ERROR: Boot failure!!\r\n");
	while (cnt--) {
		Board_LED_Set(ERROR_LED, 1);
		MSleep(msec);
		Board_LED_Set(ERROR_LED, 0);
		MSleep(msec);
	}
}
Example #2
0
bool StorageChunkWriter::WriteDataPages()
{
    unsigned            i;
    StorageDataPage*    dataPage;

    for (i = 0; i < file->numDataPages; i++)
    {
        if (env->shuttingDown)
            return false;
        
        while (env->yieldThreads)
        {
            Log_Trace("Yielding...");
            MSleep(YIELD_TIME);
        }
        
        dataPage = file->dataPages[i];
        writeBuffer.Clear();
        dataPage->Write(writeBuffer);
        //ASSERT(writeBuffer.GetLength() == dataPage->GetSize());

        if (!WriteBuffer())
            return false;
    }
    
    return true;
}
Example #3
0
int HTTPMessage::
GetBytes(unsigned char *b, int len, int retries)
{
    int bytes = recv(sock_, (char *) b, len, 0); 

    if (bytes <= 0) {
        // gestisco socket non bloccante
        if (bytes == 0 || ERRNO != EWOULDBLOCK)
            return -1;
        else
            bytes = 0;
    }

    while (bytes < len && retries > 0) {
        retries--;
        MSleep(20);
        int r = recv(sock_,(char *)b + bytes, len - bytes, 0); 

        // gestisco socket non bloccante
        if (r < 0 && ERRNO == EWOULDBLOCK)
            continue;

        if (r <= 0)
            return -1;

        bytes += r;
    }

    return bytes;
}
Example #4
0
static void ClockThread()
{
    while (clockStarted)
    {
        UpdateClock();
        MSleep(CLOCK_RESOLUTION);
    }
}
Example #5
0
void Watchdog::ThreadFunc()
{
    while (running)
    {
        flag = false;
        MSleep(timeout);
        if (flag == false)
            Call(callback);
    }
}
Example #6
0
int HTTPMessage::
GetMsg(std::string &body)
{
    int rc;
   
    log("Entro in GetMsg con offset di %d\n", offset_);

    do {
        int l = 0;
       
        buffer_[offset_] = 0;

        while (!HeaderAvailable()) {
            l = recv(sock_, buffer_ + offset_, 4, 0);

            if (l < 0 && ERRNO == EWOULDBLOCK) {
                MSleep(10);
                continue;
            }

            if (l <= 0) {
                log("Recv ha ritornato: %d (offset: %d) (errno: %d, %s)\n",
                        l, offset_, errno, strerror(errno));
                return HTTPMessage::QUIT;
            }
            
            offset_ += l;
        }
        
        buffer_[offset_] = 0;

        log( "Ricevuti %d bytes, totali %d\n", l, offset_);

        log("buffer: %s\n", buffer_);

        rc = GetMsg(offset_, body);

        if (rc == HTTPMessage::MORE_DATA) {
            log( "Need more data: %d %d body:%d\n", l, offset_, body.size());
        }
    }
    while(rc == HTTPMessage::MORE_DATA);
    
    return rc;
}
Example #7
0
void CBaseLog::CheckWriteLogFun()
{
	vector<string>* pVect = NULL;
	while (m_bRunning)
	{
		pVect = GetLogVect(false);
		if (NULL != pVect)
		{
			WriteLogToDisk(pVect,false);
		}

		pVect = GetLogVect(true);
		if (NULL != pVect)
		{
			WriteLogToDisk(pVect,true);
		}

		MSleep(200);
	}
}
Example #8
0
bool HTTPMessage::
StripHeader()
{
    do  {
        int l = recv(sock_, buffer_ + offset_, 1, 0);

        if (l < 0 && ERRNO == EWOULDBLOCK) {
            MSleep(10);
            continue;
        }

        if (l <= 0) {
            return false;
        }
        offset_ += l;
        buffer_[offset_] = 0;
    }
    while (!HeaderAvailable());

    return true;
}
// this runs in async thread
void StorageAsyncBulkCursor::AsyncReadFileChunk()
{
    StorageChunkReader      reader;
    StorageDataPage*        dataPage;
    StorageAsyncBulkResult* result;
    Callable                onNextChunk = MFUNC(StorageAsyncBulkCursor, OnNextChunk);
    
    reader.Open(chunkName, MAX_PRELOAD_THRESHOLD);
    
    lastResult = NULL;
    result = new StorageAsyncBulkResult(this);
    dataPage = reader.FirstDataPage();
    
    while (dataPage != NULL)
    {
        // rate control
        while (lastResult != NULL || env->yieldThreads || env->asyncGetThread->GetNumPending() > 0)
        {
            MSleep(1);
        }

        if (isAborted || env->shuttingDown)
        {
            // abort cursor
            delete result;
            delete this;
            return;
        }
    
        TransferDataPage(result, dataPage);
        OnResult(result);
        
        result = new StorageAsyncBulkResult(this);
        dataPage = reader.NextDataPage();
    }
    
    OnResult(result);
    IOProcessor::Complete(&onNextChunk);
}
Example #10
0
int main(int argc, char* argv[])
{
	enum		{ single, replicated, missing } mode;
	int			logTargets;
	const char*	user;
	char		buf[4096];
	bool		deleteDB;
	bool		firstRun;

	firstRun = true;
	
	mode = missing;
	if (argc == 1)
	{
		fprintf(stderr, "You did not specify a config file!\n");
		fprintf(stderr, "Starting in single mode with defaults...\n");
		fprintf(stderr, "Using database.dir = '%s'\n", DATABASE_CONFIG_DIR);
		mode = single;
	}
	else if (argc == 2)	
	{
		if (!Config::Init(argv[1]))
			STOP_FAIL("Cannot open config file", 1);
	}
	else
	{
		fprintf(stderr, "usage: %s <config-file>\n", argv[0]);
		STOP_FAIL("invalid arguments", 1);
	}
	
	if (strcmp("single", Config::GetValue("mode", "")) == 0)
		mode = single;
	else if (strcmp("replicated", Config::GetValue("mode", "")) == 0)
		mode = replicated;
	else if (mode == missing)
	{
		fprintf(stderr, "specify mode = single or mode = replicated\n");
		STOP_FAIL("invalid configuration file", 1);
	}
	
	logTargets = 0;
	if (Config::GetListNum("log.targets") == 0)
		logTargets = LOG_TARGET_STDOUT;
	for (int i = 0; i < Config::GetListNum("log.targets"); i++)
	{
		if (strcmp(Config::GetListValue("log.targets", i, ""), "file") == 0)
		{
			logTargets |= LOG_TARGET_FILE;
			Log_SetOutputFile(Config::GetValue("log.file", NULL), 
							Config::GetBoolValue("log.truncate", false));
		}
		if (strcmp(Config::GetListValue("log.targets", i, NULL), "stdout") == 0)
			logTargets |= LOG_TARGET_STDOUT;
		if (strcmp(Config::GetListValue("log.targets", i, NULL), "stderr") == 0)
			logTargets |= LOG_TARGET_STDERR;
	}
	Log_SetTarget(logTargets);
	Log_SetTrace(Config::GetBoolValue("log.trace", false));
	Log_SetTimestamping(Config::GetBoolValue("log.timestamping", false));

	Log_Message(VERSION_FMT_STRING " started");

	run:
	{
		if (!IOProcessor::Init(Config::GetIntValue("io.maxfd", 1024)))
			STOP_FAIL("Cannot initalize IOProcessor!", 1);

		// after io is initialized, drop root rights
		user = Config::GetValue("daemon.user", NULL);
		if (!ChangeUser(user))
			STOP_FAIL(rprintf("Cannot setuid to %s", user), 1);

		DatabaseConfig dbConfig;
		dbConfig.dir = Config::GetValue("database.dir", DATABASE_CONFIG_DIR);
		dbConfig.pageSize = Config::GetIntValue("database.pageSize", DATABASE_CONFIG_PAGE_SIZE);
		dbConfig.cacheSize = Config::GetIntValue("database.cacheSize", DATABASE_CONFIG_CACHE_SIZE);
		dbConfig.logBufferSize = Config::GetIntValue("database.logBufferSize", DATABASE_CONFIG_LOG_BUFFER_SIZE);
		dbConfig.checkpointTimeout = Config::GetIntValue("database.checkpointTimeout", DATABASE_CONFIG_CHECKPOINT_TIMEOUT);
		dbConfig.verbose = Config::GetBoolValue("database.verbose", DATABASE_CONFIG_VERBOSE);
		dbConfig.directDB = Config::GetBoolValue("database.directDB", DATABASE_CONFIG_DIRECT_DB);
		dbConfig.txnNoSync = Config::GetBoolValue("database.txnNoSync", DATABASE_CONFIG_TXN_NOSYNC);
		dbConfig.txnWriteNoSync = Config::GetBoolValue("database.txnWriteNoSync", DATABASE_CONFIG_TXN_WRITE_NOSYNC);

		if (Config::GetBoolValue("database.warmCache", true) && firstRun)
			WarmCache((char*)dbConfig.dir, dbConfig.cacheSize);

		if (firstRun)
			Log_Message("Opening database...");
		if (!database.Init(dbConfig))
			STOP_FAIL("Cannot initialize database!", 1);
		if (firstRun)
			Log_Message("Database opened");

		dbWriter.Init(1);
		dbReader.Init(Config::GetIntValue("database.numReaders", 20));
		
		if (!RCONF->Init())
			STOP_FAIL("Cannot initialize paxos!", 1);

		KeyspaceDB* kdb;
		if (mode == replicated)
		{
			RLOG->Init(Config::GetBoolValue("paxos.useSoftClock", true));
			kdb = new ReplicatedKeyspaceDB;
		}
		else
		{
			kdb = new SingleKeyspaceDB;
		}

		kdb->Init();
		
		HttpServer protoHttp;
		HttpKeyspaceHandler httpKeyspaceHandler(kdb);
		
		int httpPort = Config::GetIntValue("http.port", 8080);
		if (httpPort)
		{
			protoHttp.Init(httpPort);
			protoHttp.RegisterHandler(&httpKeyspaceHandler);
		}

		KeyspaceServer protoKeyspace;
		protoKeyspace.Init(kdb, Config::GetIntValue("keyspace.port", 7080));
		
		EventLoop::Init();
		EventLoop::Run();
		EventLoop::Shutdown();
			
		if (mode == replicated)
			deleteDB = ((ReplicatedKeyspaceDB*)kdb)->DeleteDB();
		else
			deleteDB = false;
		
		protoKeyspace.Shutdown();
		protoHttp.Shutdown();
		
		kdb->Shutdown();
		delete kdb;
		dbReader.Shutdown();
		dbWriter.Shutdown();
		RLOG->Shutdown();
		database.Shutdown();
		IOProcessor::Shutdown();
		
		if (mode == replicated && deleteDB)
		{
//			snprintf(buf, SIZE(buf), "%s/__*", dbConfig.dir);
//			DeleteWC(buf);
			snprintf(buf, SIZE(buf), "%s/log*", dbConfig.dir);
			DeleteWC(buf);
			snprintf(buf, SIZE(buf), "%s/keyspace", dbConfig.dir);
			DeleteWC(buf);
#ifdef _WIN32
			MSleep(3000); // otherwise Windows won't let use reuse the same ports
#endif
			firstRun = false;
			goto run;
		}
	}

	Log_Message("Keyspace shutting down.");	
	Config::Shutdown();
	Log_Shutdown();

}
Example #11
0
int HTTPMessage::
GetMsg(int len, std::string &body)
{
    log("Avvio il parse del messaggio di %d bytes\n", len); 
    bool zipped = false;
    char *end_header;

    if (!strstr(buffer_, "HTTP/") ||
            !(end_header = strstr(buffer_, "\r\n\r\n")))
        return MORE_DATA; // header non completato.

    int header_len = end_header - buffer_ + 4; // il +4 e' la lunghezza del \r\n\r\n

    char *body_length = strstr(buffer_, "Content-length");

    if (!body_length)
        body_length = strstr(buffer_, "Content-Length");

    if (strstr(buffer_, "Content-Encoding: gzip") ||
        strstr(buffer_, "Content-Encoding: deflate"))
        zipped = true;

    bool ok_on_close = false;
    bool chunked = false;

    int body_len;

    if (!body_length) {
        if (strstr(buffer_, "Encoding: chunked")) {
            body_len = strtol(buffer_ + header_len, NULL, 16);
            char *pos = strchr(buffer_ + header_len, '\n');
            header_len = pos - buffer_ + 1;
            log("Spostato offset buffer, primo char: %d\n", *(buffer_ + header_len));
            chunked = true;
        }
        else {
            log("Procedo senza content length...\n");
            body_len = 1000000;
            ok_on_close = true;    
        }
    }
    else
       body_len = atol(body_length + 16);   // il +16 e' la lunghezza di "Content-Length:"

    log("Calcolato body len di %d bytes\n", body_len);

    len -= header_len;
    char *buffer_ptr = buffer_ + header_len;
    int delta_len = len;


    while (len < body_len) {
        log("Appendo al messaggio %d bytes\n", delta_len);
        if (delta_len > 0)
            body.append(buffer_ptr, delta_len);

        buffer_ptr = buffer_;

        delta_len = recv(sock_, buffer_, 
                std::min<size_t>(body_len - len, sizeof(buffer_) - 1), 0);

        if (delta_len == 0 && ok_on_close) {
            log("Ricevuta terminazione per messaggio dopo %d bytes\n", len);
            body_len = len;
            break;
        }

        if (delta_len < 0 && ERRNO == EWOULDBLOCK) {
            MSleep(10);
            delta_len = 0;
            continue;
        }

        if (delta_len <= 0) {
            log("Ricevuto valore %d da recv, error: %d!\n", delta_len, ERRNO);
            return HTTPMessage::QUIT;
        }

        len += delta_len;
    }

    // gestire la coda del messaggio.
    offset_ = len - body_len;

    if (delta_len) {
        log("Completo il messaggio con %d bytes\n", delta_len);
        body.append(buffer_ptr, delta_len - offset_);
    }

    if (offset_) {
        log("presenti %d bytes in coda al messaggio di %d bytes\n", offset_,
                body_len + header_len);
        memcpy(buffer_, buffer_ptr + delta_len , offset_);
        buffer_[offset_] = 0;
    }

    if (zipped) {
        char window[0x10000];
        z_stream strm;
        std::string source = body;
        std::string dest;
        /* initialize inflate */
        strm.zalloc = Z_NULL;
        strm.zfree = Z_NULL;
        strm.opaque = Z_NULL;
        strm.avail_in = 0;
        strm.next_in = Z_NULL;
        int ret = inflateInit2(&strm, 47);      /* automatic zlib or gzip decoding */
        strm.avail_out = 0;

        if (ret != Z_OK)  {
            std::cerr << "Unable to initialize zlib decoding!\n";
            return HTTPMessage::QUIT;
        }

        strm.avail_in = source.length();
        strm.next_in = (Bytef*)&source[0]; 
        body.clear();

        do {
            if (strm.avail_out == 0) {                
                strm.avail_out = sizeof(window);
                strm.next_out = (Bytef*)window;
            }

            ret = inflate(&strm, Z_BLOCK);

            body.append(window, sizeof(window) - strm.avail_out);

            if (ret == Z_STREAM_END)
                break;
        }
        while (strm.avail_in > 0);

        inflateEnd(&strm);
    }
    // devo scartare i chunk aggiuntivi
    if (chunked) {
        std::string buffer;
        log("Discarding additional chunks...");
        while (buffer.find("\r\n\r\n") == std::string::npos) {
            char c;
            int rc = ::recv(sock_, &c, 1, 0);
            if (rc <= 0) {
                log("Connection close mentre scarto chunk\n");
                break;
            }
            buffer.push_back(c);
        }
        log("Done, bytes discarded %d\n", buffer.size());
    }

    log("Decodifica completata con successo\n");
    return HTTPMessage::OK;
}