Attached file has a serving thread. Below code has a new thread for pushing
events in live connections and uses wakeup_server
static int websocket_data_push(struct mg_connection *conn, enum mg_event ev)
{
OCCIF_DEBUG("inside websocket_data_push \n");
if ((ev == MG_POLL) && (NULL != conn->uri) && (NULL != eventToOCC))
{
OCCIF_DEBUG("Under the URI %s \n",URI_Event);
if (strcmp(conn->uri, URI_Event) == 0 && conn->is_websocket) {
OCCIF_INFO("Sending Event %s \n" ,eventToOCC);
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, eventToOCC,
strlen( eventToOCC));
}
}
return MG_TRUE;
}
void* sendEvent_mg(void*)
{
if( NULL != eventToOCC)
{
mg_wakeup_server_ex(mgserver, websocket_data_push, "%lu %s",
(unsigned long) time(NULL), (const char *)
eventToOCC);
}
}
jsonParser::jsonParser()
{
OCCIF_DEBUG("Inside jsonPArser constructor");
mgserver = getServerInstance();
}
jsonParser::~jsonParser()
{
}
void jsonParser::sendEventsToOCC(char* uri_local, char* evString)
{
OCCIF_INFO("Inside jsonParser::sendEventsToOCC \n");
if(evString != NULL)
{
URI_Event = uri_local;
eventToOCC = evString;
// OCCIF_DEBUG("EVENT SENT is ---%s---\n",evString);
// mg_wakeup_server_ex(mgserver,
websocket_data_push, "%lu %s",
// (unsigned long) time(NULL),
(const char *) evString);
thread_response =
pthread_create(&thread_write_websocket,NULL,sendEvent_mg,(void*)NULL);
//
mg_iterate_over_connections(mgserver,websocket_data_push,(void *)evString);
// mg_start_thread(sendEvent_mg,(void*)evString);
}
else
{
OCCIF_DEBUG("Connection Pointer is NULL\n");
}
}
void jsonParser::receiveWSrequest(struct mg_connection *conn)
{
conn_jsonParser = conn;
if (conn->is_websocket) {
char* subString;
subString = (char*) malloc(conn->content_len+1);
memset(subString,0,conn->content_len+1);
memcpy(subString,conn->content,conn->content_len);
if(strlen(subString) > SIZE)
{
OCCIF_INFO("CONTENT RECEIVED IS :-->%s<--\n",subString);
}
jsonparserString(subString);
if(JS_NOT_A_JSON == p_Status)
{
if(strlen(subString) > SIZE)
{
mg_websocket_write(conn, 1, subString, strlen(subString));
printf("RESPONSE IS ------->%s<-------\n\n\n\n",subString);
}
}
else
{
if( NULL != conn)
{
if( 0 == strcmp(conn->uri,URI_ACTIVATION))
{
OCCIF_DEBUG("Inside wsrequest for Activation uri
matched\n");
((activation*)(appinterfaceFactory::makeComponent(URIObjects::eACTIVATION_OBJ)))->receiveJSONString(conn,receivedJSONrequest);
}
else if( 0 == strcmp(conn->uri,URI_TTS))
{
OCCIF_DEBUG("Inside wsrequest for TTS uri matched\n");
((tts*)(appinterfaceFactory::makeComponent(URIObjects::eTTS_OBJ)))->receiveJSONString(conn,receivedJSONrequest);
}
else if( 0 == strcmp(conn->uri,URI_MEDIAPLAY))
{
OCCIF_DEBUG("Inside wsrequest for mediaplayer uri
matched\n");
((mediaplayer*)(appinterfaceFactory::makeComponent(URIObjects::eMEDIA_OBJ)))->receiveJSONString(conn,receivedJSONrequest);
}
else if( 0 == strcmp(conn->uri,URI_TMX_RECEIVER))
{
OCCIF_DEBUG("Inside wsrequest for tmx-receiver uri
matched\n");
((tmxrcvr*)(appinterfaceFactory::makeComponent(URIObjects::eTMX_OBJ)))->receiveJSONString(conn,receivedJSONrequest);
}
else if(0 == strcmp(conn->uri,URI_OPKG_INSTALL))
{
OCCIF_DEBUG("Inside wsrequest for opkg-install uri
matched\n");
((opkg*)(appinterfaceFactory::makeComponent(URIObjects::eOPKG_OBJ)))->receiveJSONString(conn,receivedJSONrequest);
}
else
{
OCCIF_DEBUG("NO Valid WS URI matched\n");
}
}
else
{
OCCIF_DEBUG("Connection Pointer in NULL \n");
}
}
free(subString);
subString = NULL;
metName = NULL;
reqId = NULL;
receivedJSONrequest = NULL;
}
}
Thanks,
Kunal
On Thursday, September 25, 2014 1:36:06 AM UTC-4, Sergey Lyubka wrote:
>
> Hi Kunal,
> Joshi's mail has an example code which doesn't have mg_wakeup_server_ex()
> call.
> You however do mention mg_wakeup_server_ex().
> I am confused - please clarify. Showing the code is the best way to go.
>
> On Wed, Sep 24, 2014 at 4:10 PM, kk <[email protected] <javascript:>
> > wrote:
>
>> Hi Sergey,
>>
>> Thankful for your replies. It is a proof of concept in which we chose
>> mongoose to handle use-cases. We might extend the project and will look for
>> more (commercial) support also. For now, it will be grateful whatever
>> inputs we can get from you. The OS is linux and platform has opera a UI
>> client. However, we also test on desktop chrome or opera to simulate the
>> use-cases.
>>
>>
>> Regards
>>
>> On Wednesday, September 24, 2014 10:49:14 AM UTC-4, Sergey Lyubka wrote:
>>>
>>> Ok, thanks.
>>>
>>> Could you clarify what OS you're running on?
>>>
>>> Note that you can avail for the commercial support if you'd like to get
>>> a guaranteed response and dedicated engineering time. Mailing list works
>>> too but it is at the bottom of the priority list to follow up on mailing
>>> list queries.
>>>
>>> On Wed, Sep 24, 2014 at 3:34 PM, kk <[email protected]> wrote:
>>>
>>>> Hi Sergey,
>>>>
>>>> Yes, She is working with me on this project where we want to support
>>>> frequent incoming requests. Please let us know your ideas on this.
>>>>
>>>>
>>>> Thanks,
>>>> Kunal
>>>>
>>>> On Wednesday, September 24, 2014 2:10:27 AM UTC-4, Sergey Lyubka wrote:
>>>>>
>>>>> Hi Kunal,
>>>>> Does this recent message seem relevant to the issue you're seeing:
>>>>>
>>>>> https://groups.google.com/forum/#!topic/mongoose-users/NxNf_ST5y78 ?
>>>>>
>>>>> On Tue, Sep 23, 2014 at 8:06 PM, kk <[email protected]> wrote:
>>>>>
>>>>>> Hi Sergey,
>>>>>>
>>>>>> Even after updating to latest mongoose version, I see the same issue
>>>>>> i.e. high frequency data communication makes the serving thread go in
>>>>>> blocking state. I am looking for help. Please let me know if you have
>>>>>> any
>>>>>> questions.
>>>>>>
>>>>>>
>>>>>> Regards,
>>>>>> Kunal
>>>>>>
>>>>>> On Thursday, September 18, 2014 10:57:38 AM UTC-4, Sergey Lyubka
>>>>>> wrote:
>>>>>>>
>>>>>>> Ah, ok.
>>>>>>> Date timestamp was introduced by me recently, so just update to the
>>>>>>> recent version, just to be on the same page.
>>>>>>>
>>>>>>> On Thu, Sep 18, 2014 at 2:54 PM, kk <[email protected]> wrote:
>>>>>>>
>>>>>>>> Hi Sergey,
>>>>>>>>
>>>>>>>> I do not find a date timestamp in my mongoose.c. Mongoose release
>>>>>>>> notes say the below:
>>>>>>>>
>>>>>>>> ## Release 5.4, July 28 2014
>>>>>>>>
>>>>>>>> How should I find date and timestmap?
>>>>>>>>
>>>>>>>> Regards,
>>>>>>>> Kunal
>>>>>>>>
>>>>>>>> On Thursday, September 18, 2014 4:25:22 AM UTC-4, Sergey Lyubka
>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>> Hi Kunal, what is the $Date$ timestamp of your mongoose.c file?
>>>>>>>>>
>>>>>>>>> On Thu, Sep 18, 2014 at 12:28 AM, kk <[email protected]>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> I am working on an application in which I am using
>>>>>>>>>> mg_wakeup_server API to push data over websocket connection. As
>>>>>>>>>> guided for
>>>>>>>>>> Mongoose usage, I have a poll_server in main thread and a separate
>>>>>>>>>> thread
>>>>>>>>>> for pushing data to live websocket URIs. There are a couple of
>>>>>>>>>> client
>>>>>>>>>> applications which interface with my application written on mongoose
>>>>>>>>>> server. These different applications work on different URIs. For
>>>>>>>>>> most of
>>>>>>>>>> applications, the server is able to utilize capability of
>>>>>>>>>> wakeup_server to
>>>>>>>>>> push events when required. However, one application demands high
>>>>>>>>>> frequency
>>>>>>>>>> data. There, I observe after sometime the polling stops and the
>>>>>>>>>> interface
>>>>>>>>>> blocks i.e. there is no communication happening after this. Is this
>>>>>>>>>> behavior seen by anyone? How should this be solved?
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Regards,
>>>>>>>>>> Kunal
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> 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 [email protected].
>>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>>> Visit this group at http://groups.google.com/group/mongoose-users
>>>>>>>>>> .
>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>> 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 [email protected].
>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>> Visit this group at http://groups.google.com/group/mongoose-users.
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>> 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 [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>> Visit this group at http://groups.google.com/group/mongoose-users.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>> --
>>>> 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 [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at http://groups.google.com/group/mongoose-users.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/mongoose-users.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.
#include <sys/stat.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
/*
Added for OCC
*/
#include "URIenums.h"
#include "appinterfaceFactory.h"
#include "required.h"
#include "occif_logger.h"
#ifdef _WIN32
#define snprintf _snprintf
#endif
#include <sys/wait.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h> // For chdir()
#include <winsvc.h>
#include <shlobj.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#ifndef S_ISDIR
#define S_ISDIR(x) ((x) & _S_IFDIR)
#endif
#define DIRSEP '\\'
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define sleep(x) Sleep((x) * 1000)
#define abs_path(rel, abs, abs_size) _fullpath((abs), (rel), (abs_size))
#define SIGCHLD 0
typedef struct _stat file_stat_t;
#define stat(x, y) _stat((x), (y))
#else
typedef struct stat file_stat_t;
#include <sys/wait.h>
#include <unistd.h>
#define DIRSEP '/'
#define __cdecl
#define abs_path(rel, abs, abs_size) realpath((rel), (abs))
#endif // _WIN32
#define MAX_OPTIONS 100
#define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
static int exit_flag;
static char server_name[50]; // Set by init_server_name()
static char config_file[PATH_MAX]; // Set by process_command_line_arguments()
static struct mg_server *server; // Set by start_mongoose()
static const char *s_default_document_root = ".";
static const char *s_default_listening_port = "8888";
#if !defined(CONFIG_FILE)
#define CONFIG_FILE "mongoose.conf"
#endif /* !CONFIG_FILE */
static void __cdecl signal_handler(int sig_num) {
// Reinstantiate signal handler
signal(sig_num, signal_handler);
#ifndef _WIN32
// Do not do the trick with ignoring SIGCHLD, cause not all OSes (e.g. QNX)
// reap zombies if SIGCHLD is ignored. On QNX, for example, waitpid()
// fails if SIGCHLD is ignored, making system() non-functional.
if (sig_num == SIGCHLD) {
do {} while (waitpid(-1, &sig_num, WNOHANG) > 0);
} else
#endif
OCCIF_DEBUG("Got signal %i\n", sig_num);
{ exit_flag = sig_num; }
}
static void show_usage_and_exit(void) {
const char **names;
int i;
fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka, built on %s\n",
MONGOOSE_VERSION, __DATE__);
fprintf(stderr, "Usage:\n");
#ifndef MONGOOSE_NO_AUTH
fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
#endif
fprintf(stderr, " mongoose [config_file]\n");
fprintf(stderr, " mongoose [-option value ...]\n");
fprintf(stderr, "\nOPTIONS:\n");
names = mg_get_valid_option_names();
for (i = 0; names[i] != NULL; i += 2) {
fprintf(stderr, " -%s %s\n",
names[i], names[i + 1] == NULL ? "<empty>" : names[i + 1]);
}
exit(EXIT_FAILURE);
}
static char *sdup(const char *str) {
char *p;
if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
strcpy(p, str);
}
return p;
}
static void set_option(char **options, const char *name, const char *value) {
int i;
for (i = 0; i < MAX_OPTIONS - 3; i++) {
if (options[i] == NULL) {
options[i] = sdup(name);
options[i + 1] = sdup(value);
options[i + 2] = NULL;
break;
} else if (!strcmp(options[i], name)) {
free(options[i + 1]);
options[i + 1] = sdup(value);
break;
}
}
if (i == MAX_OPTIONS - 3) {
OCCIF_DEBUG("Too many options specified");
}
}
static void process_command_line_arguments(char *argv[], char **options) {
char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)], *p;
FILE *fp = NULL;
size_t i, cmd_line_opts_start = 1, line_no = 0;
// Should we use a config file ?
if (argv[1] != NULL && argv[1][0] != '-') {
snprintf(config_file, sizeof(config_file), "%s", argv[1]);
cmd_line_opts_start = 2;
} else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
// No command line flags specified. Look where binary lives
snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE);
} else {
snprintf(config_file, sizeof(config_file), "%.*s%c%s",
(int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
}
fp = fopen(config_file, "r");
// If config file was set in command line and open failed, die
if (cmd_line_opts_start == 2 && fp == NULL) {
OCCIF_DEBUG("Cannot open config file %s: %s", config_file, strerror(errno));
}
// Load config file settings first
if (fp != NULL) {
fprintf(stderr, "Loading config file %s\n", config_file);
// Loop over the lines in config file
while (fgets(line, sizeof(line), fp) != NULL) {
line_no++;
// Ignore empty lines and comments
for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
if (line[i] == '#' || line[i] == '\0') {
continue;
}
if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
OCCIF_DEBUG("%s: line %d is invalid, ignoring it:\n %s",
config_file, (int) line_no, line);
} else {
set_option(options, opt, val);
}
}
fclose(fp);
}
// If we're under MacOS and started by launchd, then the second
// argument is process serial number, -psn_.....
// In this case, don't process arguments at all.
if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
// Handle command line flags.
// They override config file and default settings.
for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
if (argv[i][0] != '-' || argv[i + 1] == NULL) {
show_usage_and_exit();
}
set_option(options, &argv[i][1], argv[i + 1]);
}
}
}
static char *get_option(char **options, const char *option_name) {
int i;
for (i = 0; options[i] != NULL; i++)
if (!strcmp(options[i], option_name))
return options[i + 1];
return NULL;
}
static int is_path_absolute(const char *path) {
#ifdef _WIN32
return path != NULL &&
((path[0] == '\\' && path[1] == '\\') || // UNC path, e.g. \\server\dir
(isalpha(path[0]) && path[1] == ':' && path[2] == '\\')); // E.g. X:\dir
#else
return path != NULL && path[0] == '/';
#endif
}
static void set_absolute_path(char *options[], const char *option_name,
const char *path_to_mongoose_exe) {
char path[PATH_MAX], abs[PATH_MAX], *option_value;
const char *p;
// Check whether option is already set
option_value = get_option(options, option_name);
// If option is already set and it is an absolute path,
// leave it as it is -- it's already absolute.
if (option_value != NULL && !is_path_absolute(option_value)) {
// Not absolute. Use the directory where mongoose executable lives
// be the relative directory for everything.
// Extract mongoose executable directory into path.
if ((p = strrchr(path_to_mongoose_exe, DIRSEP)) == NULL) {
getcwd(path, sizeof(path));
} else {
snprintf(path, sizeof(path), "%.*s", (int) (p - path_to_mongoose_exe),
path_to_mongoose_exe);
}
strncat(path, "/", sizeof(path) - 1);
strncat(path, option_value, sizeof(path) - 1);
// Absolutize the path, and set the option
abs_path(path, abs, sizeof(abs));
set_option(options, option_name, abs);
}
}
static void init_server_name(void) {
const char *descr = "";
snprintf(server_name, sizeof(server_name), "Mongoose web server v.%s%s",
MONGOOSE_VERSION, descr);
}
static int path_exists(const char *path, int is_dir) {
file_stat_t st;
return path == NULL || (stat(path, &st) == 0 &&
((S_ISDIR(st.st_mode) ? 1 : 0) == is_dir));
}
static void verify_existence(char **options, const char *name, int is_dir) {
const char *path = get_option(options, name);
if (!path_exists(path, is_dir)) {
OCCIF_DEBUG("Invalid path for %s: [%s]: (%s). Make sure that path is either "
"absolute, or it is relative to mongoose executable.",
name, path, strerror(errno));
}
}
////////////////////////////// End of Mongoose Implementation //////////////////////////////
/***
Methods to implement http & ws requests
***/
static void occ_printRequest(struct mg_connection *conn,char * typeRequest)
{
OCCIF_DEBUG("********************* inside %s Request *********************************\n",typeRequest);
OCCIF_DEBUG("-->Request method %s\n",conn->request_method);
OCCIF_INFO("-->URI %s \n",conn->uri);
OCCIF_DEBUG("-->HTTP Version -->%s<--\n\n",conn->http_version);
OCCIF_DEBUG("-->Query String-->%s<-- \n\n",conn->query_string);
OCCIF_DEBUG("-->HTTP Version-->%s<-- \n\n",conn->http_version);
OCCIF_DEBUG("-->Header name -->%s<-- \n\n",conn->http_headers->name);
OCCIF_DEBUG("-->Header value -->%s<--\n\n",conn->http_headers->value);
OCCIF_DEBUG("-->Header Content -->%s<--\n\n",conn->content);
OCCIF_DEBUG("-->local_ip -->%s<--\n\n",conn->local_ip);
OCCIF_DEBUG("-->remote_ip -->%s<--\n\n",conn->remote_ip);
OCCIF_DEBUG("-->remote_port -->%d<--\n\n",conn->remote_port);
OCCIF_DEBUG("-->local_port-->%d<--\n\n",conn->local_port);
OCCIF_DEBUG("********************* %s Request ends *********************************\n",typeRequest);
}
static int handleWSRequest(struct mg_connection *conn) {
if (conn->is_websocket) {
((jsonParser*)(appinterfaceFactory::makeComponent(URIObjects::eJSONPARSER_OBJ)))->receiveWSrequest(conn);
}
return MG_TRUE;
}
int checkURI(const char* uri)
{
if(0 == strcmp(URI_ACTIVATION,uri))
return 1;
else if(0 == strcmp(URI_TTS,uri))
return 1;
else if(0 == strcmp(URI_LOGGER,uri))
return 1;
else if(0 == strcmp(URI_MEDIAPLAY,uri))
return 1;
else if(0 == strcmp(URI_TMX_RECEIVER,uri))
return 1;
else if(0 == strcmp(URI_APP_FIRST_LAUNCH,uri))
return 1;
else if(0 == strcmp(URI_GET_INSTALLED_APP_JSON,uri))
return 1;
else if(0 == strcmp(URI_REST_SEND,uri))
return 1;
else if(0 == strcmp(URI_REST_HEAD,uri))
return 1;
else if(0 == strcmp(URI_REST_RESOLVE,uri))
return 1;
else if(0 == strcmp(URI_PROP_SET,uri))
return 1;
else if(0 == strcmp(URI_PROP_GET,uri))
return 1;
else if(0 == strcmp(URI_PROP_REMOVE,uri))
return 1;
else if(0 == strcmp(URI_PROP_CLEAR,uri))
return 1;
else
return 0;
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if(!conn->is_websocket)
{
if (ev == MG_REQUEST)
{
OCCIF_DEBUG("\n---------1 MG REQUEST----------\n");
occ_printRequest(conn,"http");
if ((0 == strcmp(conn->uri, "/ui/installedApplicationsJSON")) || (0 == strcmp(conn->uri, "/ui/oc-installedApplicationsJSON")) )
{
if(MG_TRUE == ((httpparser*)(appinterfaceFactory::makeComponent(URIObjects::eHTTP_OBJ)))->installedApplicationsJSON(conn))
{
return MG_TRUE;
}
else
return MG_FALSE;
}
else if (0 == strcmp(conn->uri, "/ui/appFirstLaunch"))
{
if(MG_TRUE == ((httpparser*)(appinterfaceFactory::makeComponent(URIObjects::eHTTP_OBJ)))->appFirstLaunch(conn))
return MG_TRUE;
else
return MG_FALSE;
}
else if (0 == strcmp(conn->uri, "/profile/getVehicleName"))
{
if(MG_TRUE == ((httpparser*)(appinterfaceFactory::makeComponent(URIObjects::eHTTP_OBJ)))->getVehicleName(conn))
return MG_TRUE;
else
return MG_FALSE;
}
else if (0 == strcmp(conn->uri, "/sci/ext"))
{
if(MG_TRUE == ((httpparser*)(appinterfaceFactory::makeComponent(URIObjects::eHTTP_OBJ)))->appFirstLaunch(conn))
return MG_TRUE;
else
return MG_FALSE;
}
else if (0 == strcmp(conn->uri, "/rest/send"))
{
int result_val = 0;
printf("Inside rest/send");
char url[256+1];
char method[256+1];
mg_get_var(conn, "url", url, sizeof(url)); // fixme: what if not passed?
mg_get_var(conn, "method", method, sizeof(method));
OCCIF_DEBUG("url passed to gateway = %s\n", url);
OCCIF_DEBUG("Conn url passed to gateway = %s\n", conn->uri);
result_val = gateway(conn, url);
OCCIF_DEBUG("Returned back from gateway\n");
return MG_TRUE;
}
else
{
}
}
else if (ev == MG_AUTH)
{
OCCIF_DEBUG("\n---------2 MG AUTH----------\n");
if( checkURI(conn->uri) )
{
OCCIF_DEBUG("\n---------ADDING A RELEVANT URI----------\n");
}
else
{
OCCIF_DEBUG("\n---------DID NOT ADD A RELEVANT URI----------\n");
}
return MG_TRUE;
}
else if (ev == MG_CONNECT)
{
OCCIF_DEBUG("\n---------3 MG CONNECT----------\n");
return MG_TRUE;
}
else if (ev == MG_HTTP_ERROR)
{
OCCIF_DEBUG("\n---------4 HTTP ERRROR----------\n");
return MG_FALSE;
}
else if (ev == MG_CLOSE)
{
OCCIF_DEBUG("\n--------5 -MG CLOSE----------\n");
return MG_FALSE;
}
}
else // if(conn->is_websocket)
{
if (ev == MG_REQUEST)
{
OCCIF_DEBUG("\n---------6 MG REQUEST----------\n");
occ_printRequest(conn,"ws");
return handleWSRequest(conn);
}
else if (ev == MG_AUTH)
{
OCCIF_DEBUG("\n---------7 MG AUTH----------\n");
if( checkURI(conn->uri) )
{
OCCIF_DEBUG("\n---------ADDING A RELEVANT URI----------\n");
}
else
{
OCCIF_DEBUG("\n---------DID NOT ADD A RELEVANT URI----------\n");
}
return MG_TRUE;
}
else if (ev == MG_CONNECT)
{
OCCIF_DEBUG("\n---------8 MG CONNECT----------\n");
return MG_TRUE;
}
else if (ev == MG_CLOSE)
{
OCCIF_DEBUG("\n---------9 MG CLOSE----------\n");
return MG_FALSE;
}
else
{
//printf("In Else condition in a ws connection---No Event matched\n\n");
}
}
return MG_FALSE;
}
static void start_mongoose(int argc, char *argv[]) {
char *options[MAX_OPTIONS];
int i;
if ((server = mg_create_server(NULL, ev_handler)) == NULL) {
OCCIF_DEBUG("Failed to start Mongoose.");
}
// Show usage if -h or --help options are specified
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
show_usage_and_exit();
}
options[0] = NULL;
set_option(options, "document_root", s_default_document_root);
set_option(options, "listening_port", s_default_listening_port);
// Update config based on command line arguments
process_command_line_arguments(argv, options);
// Make sure we have absolute paths for files and directories
// https://github.com/valenok/mongoose/issues/181
set_absolute_path(options, "document_root", argv[0]);
set_absolute_path(options, "dav_auth_file", argv[0]);
set_absolute_path(options, "cgi_interpreter", argv[0]);
set_absolute_path(options, "access_log_file", argv[0]);
set_absolute_path(options, "global_auth_file", argv[0]);
set_absolute_path(options, "ssl_certificate", argv[0]);
if (!path_exists(get_option(options, "document_root"), 1)) {
set_option(options, "document_root", s_default_document_root);
set_absolute_path(options, "document_root", argv[0]);
OCCIF_DEBUG("Setting document_root to [%s]\n",
mg_get_option(server, "document_root"));
}
// Make extra verification for certain options
verify_existence(options, "document_root", 1);
verify_existence(options, "cgi_interpreter", 0);
verify_existence(options, "ssl_certificate", 0);
for (i = 0; options[i] != NULL; i += 2) {
const char *msg = mg_set_option(server, options[i], options[i + 1]);
if (msg != NULL) {
OCCIF_DEBUG("Failed to set option [%s] to [%s]: %s\n",
options[i], options[i + 1], msg);
if (!strcmp(options[i], "listening_port")) {
mg_set_option(server, "listening_port", s_default_listening_port);
OCCIF_DEBUG("Setting %s to [%s]\n", options[i], s_default_listening_port);
}
}
free(options[i]);
free(options[i + 1]);
}
// Change current working directory to document root. This way,
// scripts can use relative paths.
chdir(mg_get_option(server, "document_root"));
// Add an ability to pass listening socket to mongoose
{
const char *env = getenv("MONGOOSE_LISTENING_SOCKET");
if (env != NULL && atoi(env) > 0 ) {
mg_set_listening_socket(server, atoi(env));
}
}
// Setup signal handler: quit on Ctrl-C
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
#ifndef _WIN32
signal(SIGCHLD, signal_handler);
#endif
}
struct mg_server* getServerInstance(void)
{
return server;
}
static void *serving_thread_func(void *param) {
struct mg_server *srv = (struct mg_server *) param;
while (exit_flag != 2) {
// OCCIF_INFO("Polling \n");
mg_poll_server(srv, 100);
}
return NULL;
}
int main(int argc, char *argv[]) {
start_occappmanager();
// Initialize Mongoose Server and Start
init_server_name();
start_mongoose(argc, argv);
OCCIF_INFO("%s serving [%s] on port %s\n",
server_name, mg_get_option(server, "document_root"),
mg_get_option(server, "listening_port"));
//------------------------- Factory Objects creation----------------------------
appinterfaceFactory::makeComponent(URIObjects::eACTIVATION_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eOPKG_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eMEDIA_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eTTS_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eJSONPARSER_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eHTTP_OBJ);
appinterfaceFactory::makeComponent(URIObjects::eTMX_OBJ);
//------------------------------------------------------------------------------
serving_thread_func(server);
OCCIF_INFO("Exiting on signal %d ...", exit_flag);
fflush(stdout);
mg_destroy_server(&server);
return 0;
}