-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappframework.h
More file actions
138 lines (116 loc) · 4.29 KB
/
Copy pathappframework.h
File metadata and controls
138 lines (116 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2014 Dialogic Corp.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
* Alternatively see <http://www.gnu.org/licenses/>.
* Or see the LICENSE file included within the source tree.
*
*/
#ifndef APPFRAMEWOK_H
#define APPFRAMEWOK_H
/*------------------------------ Dependencies --------------------------------*/
#include <string>
#include <queue>
#include "getoption.h"
#include "call.h"
/*----------------------------------------------------------------------------*/
// Externs for event handler thread
extern std::queue<std::string> eventQueue;
extern pthread_mutex_t queueLock;
extern pthread_cond_t queueNotEmpty;
extern pthread_t evHandlerThread;
/*!
* \class AppFramework
*
*/
class AppFramework
{
public:
AppFramework ();
~AppFramework ();
// Entry point - work loop
bool run (const GetOptions & opts, int signal_fd);
private:
static void *eventHandlerThread (void *voidPtr);
bool initEventHandlerThread ();
struct MemoryStruct
{
char *memory;
size_t size;
};
/********************************************
// JH - can I consolidate this with the one in dispatchXmsCmd.cpp?
static size_t createEvhandlerReplyContentCallback (void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *) userp;
mem->memory = (char *) realloc (mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL)
{
// out of memory
LOGCRIT ("evhandlerReplyContentCallback - not enough memory (realloc returned NULL)");
return 0;
}
memcpy (&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
*******************************************/
static size_t longPollReplyContentCallback (void *contents, size_t size, size_t nmemb, void *userp)
{
// A typical CURL reply content callback, BUT, this one is used for the logn poll
// GET that remains open. Any content from the GET is an event from XMS, and here
// in the event handling thread, it is enqueued to be read in the main thread where
// all the action is.
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *) userp;
mem->memory = (char *) realloc (mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL)
{
// out of memory!
LOGCRIT ("longPollReplyContentCallback - not enough memory (realloc returned NULL)");
return 0;
}
// Add event to the event queue
char * eventPtr = (char *) malloc((size * nmemb) + 1);
memcpy(eventPtr, contents, size * nmemb);
//*(eventPtr + (size * nmemb) + 1) = 0;
*(eventPtr + size * nmemb) = 0;
//LOGDEBUG("Event length is " << strlen(eventPtr));
//LOGDEBUG("Event is " << eventPtr);
//for (int i=0; i< (int) strlen(eventPtr); i++)
//{
// printf ("%x ", (int) *(eventPtr+i));
//}
std::string strToEnqueue = eventPtr + 4;
// Protected section of code here. Only allow adding to queue
// if event loop is not removing an entry.
// In addition, send a singal to the main thread so that it will
// read an entry out of the queue
//LOGDEBUG("Entering protected enqueue section");
pthread_mutex_lock(&queueLock);
eventQueue.push(strToEnqueue);
pthread_mutex_unlock(&queueLock);
pthread_cond_signal(&queueNotEmpty);
//LOGDEBUG("Out of protected enqueue section");
return realsize;
}
};
#endif // APPFRAMEWOK_H
/* vim:ts=4:set nu:
* EOF
*/