Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Calling a member function from begin_request_handler

34 views
Skip to first unread message

Jared

unread,
Jul 10, 2014, 7:20:49 AM7/10/14
to mongoos...@googlegroups.com
Hi,

I'm new to mongoose. I edited hello.c and added a Web class into it and moved some original code around. I was able to build and execute it like its original behavior. However, when I added a member function and called in from begin_request_handler, the new function could not be called. Can you please help me? Thanks!

    Web::get_Instance()->callback();   // unable to call this function


================= hello.cpp ====================
#include <stdio.h>
#include <string.h>
#include "mongoose.h"

class Web
{
private:
    Web();
   ~Web() {};
    static Web* m_instance;
public:
    void callback (void);
    static Web* get_Instance();
};
Web* Web::m_instance = 0;

void Web::callback (void)
{
    printf ("%s\n", __FUNCTION__);
}

// This function will be called by mongoose on every new request.
static int begin_request_handler(struct mg_connection *conn) 
{
    Web::get_Instance()->callback();   // unable to call this function

  const struct mg_request_info *request_info = mg_get_request_info(conn);
  char content[100];

  // Prepare the message we're going to send
  int content_length = snprintf(content, sizeof(content),
                                "Hello from mongoose! Remote port: %d",
                                request_info->remote_port);

  // Send HTTP reply to the client
  mg_printf(conn,
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/plain\r\n"
            "Content-Length: %d\r\n"        // Always set Content-Length
            "\r\n"
            "%s",
            content_length, content);

  // Returning non-zero tells mongoose that our function has replied to
  // the client, and mongoose should not send client any more data.
  return 1;
}

Web::Web()
{
  struct mg_context *ctx;
  struct mg_callbacks callbacks;

  // List of options. Last element must be NULL.
  const char *options[] = {"listening_ports", "8080", NULL};

  // Prepare callbacks structure. We have only one callback, the rest are NULL.
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.begin_request = begin_request_handler;

  // Start the web server.
  ctx = mg_start(&callbacks, NULL, options);

  // Wait until user hits "enter". Server is running in separate thread.
  // Navigating to http://localhost:8080 will invoke begin_request_handler().
  getchar();

  // Stop the server.
  mg_stop(ctx);
}

Web* Web::get_Instance()
{
    if (!m_instance)
        m_instance = new Web;

    return m_instance;
}

int main(void) 
{
    Web::get_Instance();

    return 0;
}

Ulrich Hertlein

unread,
Jul 14, 2014, 6:44:57 AM7/14/14
to mongoos...@googlegroups.com
Hi Jared,

the code looks fine to me.

What do you mean 'unable to call'?  Are you getting a compiler error?  Or a crash at runtime?

Cheers,
/ulrich


--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Jared

unread,
Jul 15, 2014, 4:28:56 AM7/15/14
to mongoos...@googlegroups.com
Hi Ulrich,

I'm able to compile it successfully. And not a crash.

This is what the output looks like if I added printf("%s\n",__FUNCTION); to each function and I did not call the callback() function.
main
get_Instance
Web
begin_request_handler
begin_request_handler


This is how it looks like if I added a call to the callback() function.
main
get_Instance
Web
begin_request_handler
get_Instance
Web

Ulrich Hertlein

unread,
Jul 15, 2014, 9:07:47 AM7/15/14
to mongoos...@googlegroups.com
Hi Jared,

I noticed that you have a 'getchar' in the Web constructor.

This prevents the Web ctor from ever finishing (well, until someone has pressed a key).
Since the constructor never returns, the 'm_instance' variable is never set and every call to 'get_Instance' will try to create a new instance.

The usual way would be to call 'mg_start' in the constructor and 'mg_stop' in the destructor.  The main can then call 'getchar' to wait for completion.

Cheers,
/ulrich

Jared

unread,
Jul 17, 2014, 12:47:22 AM7/17/14
to mongoos...@googlegroups.com
Hi Ulrich,

Your solution worked! Thank you.

Have a great day!
--
Jared
Reply all
Reply to author
Forward
0 new messages