Hi,

Attached is the http_srv code under question. I think the source of the problem
is when lwip_accept is called again after creating the thread. The HTTP thread
does not execute at all when lwip_accept in main() is invoked again. However,
when i insert a call to sleep in the infinite loop, the HTTP thread executes. I
also suspect I might not have done enough to make the server truly
multithreaded.

Peter

Quoting Christian Helmuth <[email protected]>:

> Hello Peter,
>
> On Wed, Apr 28, 2010 at 04:47:00PM +0930, Peter Nguyen wrote:
> > Any ideas as to what's going on?
>
> From your description, I guess your code is buggy, but you (again) did
> not send sources. Maybe someone on the list could help you, if you
> send the relevant code snippets from your program.
>
> Ciao
> --
> Christian Helmuth
> Genode Labs
>
> http://www.genode-labs.com/ · http://genode.org/
>
> Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden
> Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Genode-main mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/genode-main
>


/*
 * \brief  Minimal HTTP server lwIP demonstration
 * \author lwIP Team
 * \author Stefan Kalkowski
 * \date   2009-10-23
 *
 * This small example shows how to use the LwIP in Genode directly.
 * If you simply want to use LwIP's socket API, you might use
 * Genode's libc together with its LwIP backend, especially useful
 * when porting legacy code.
 */

/*
 * Copyright (C) 2009-2010 Genode Labs GmbH
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

/* Genode includes */
#include <base/printf.h>
#include <base/thread.h>
#include <util/string.h>

/* LwIP includes */
extern "C" {
#include <lwip/sockets.h>
#include <lwip/api.h>
#include <stdlib.h>
#include <stdio.h>
}

#include <lwip/genode.h>

#include <timer_session/connection.h>
#include <sampling/netsampling.h>
#include <sampling/database.h>
#include <timer.h>

#include <sampling_session/connection.h>

const static char http_html_hdr[] =
	"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"; /* HTTP response header */

const static char http_index_html[] =
	"<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page.</body></html>"; /* HTML page */

ProductDatabase database;

void *operator new(Genode::size_t size)
{
    void *addr = Genode::env()->heap()->alloc(size);
    if (!addr) {
        PERR("env()->heap() has consumed %zd", Genode::env()->heap()->consumed());
        PERR("env()->ram_session()->quota = %zd", Genode::env()->ram_session()->quota());
        throw Genode::Allocator::Out_of_memory();
    }
    return addr;
}

Okl4::L4_ThreadId_t *handler_ptr;
static Genode::Lock _lock;

Timer::Connection timer;

class http_thread : public Thread<4096>
{
	public:
		int conn;
	
		void thread_fn();
		void entry();
		
		http_thread(int conn_number)
		{
			conn = conn_number;
		}
};

/**
 * Handle a single client's request.
 *
 * \param conn  socket connected to the client
 */
void http_thread::thread_fn()
{
	char    buf[1024];
	ssize_t buflen;

	buflen = lwip_recv(conn, buf, 1024, 0);
	PDBG("Packet received!");

	/* Ignore all receive errors */
	if (buflen > 0) {
	
		//NEED TO TEST PROFILING CODE. ALSO: THINK OF MORE PROBES TO ADD
		//FOR PROFILING

		/* Is this an HTTP GET command? (only check the first 5 chars, since
		   there are other formats for GET, and we're keeping it very simple)*/
		if (buflen >= 5 &&
			buf[0] == 'G' &&
			buf[1] == 'E' &&
			buf[2] == 'T' &&
			buf[3] == ' ' &&
			buf[4] == '/' ) {
		
			/* Send http header */
			lwip_send(conn, http_html_hdr, Genode::strlen(http_html_hdr), 0);
			
			/* Send our HTML page */
			lwip_send(conn, http_index_html, Genode::strlen(http_index_html), 0);
		}
	}
	lwip_close(conn);
}

void http_thread::entry()
{
	thread_fn();
}

int main()
{
	int s;

	/* Initialize network stack and do DHCP */
	if (lwip_nic_init(0, 0, 0)) {
		PERR("We got no IP address!");
		sleep_forever();
	}

	PDBG("Create new socket ...");
	if((s = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		PERR("No socket available!");
		sleep_forever();
	}

	PDBG("Now, I will bind ...");
	struct sockaddr_in in_addr;
    in_addr.sin_family = AF_INET;
    in_addr.sin_port = htons(80);
    in_addr.sin_addr.s_addr = INADDR_ANY;
	if(lwip_bind(s, (struct sockaddr*)&in_addr, sizeof(in_addr))) {
		sleep_forever();
	}

	PDBG("Now, I will listen ...");
	if(lwip_listen(s, 5)) {
		PERR("listen failed!");
		sleep_forever();
	}

	PDBG("Start the server loop ...");
	while(true) {

		struct sockaddr addr;
		socklen_t len = sizeof(addr);
		
		int client = lwip_accept(s, &addr, &len);

		if(client < 0) {
			PWRN("Invalid socket from accept!");
			sleep_forever();
		}
		//Create the HTTP thread to handle the request
		http_thread thr(client);
		thr.start();

		//http_server_serve(client, (void*)ptr);
		//lwip_close(client); -> THIS IS INVOKED IN THE HTTP THREAD (http_thread)
	}
		

}
------------------------------------------------------------------------------
_______________________________________________
Genode-main mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/genode-main

Reply via email to