45 return "WebSocketDataConnector";
48 static int callback_http(
50 enum lws_callback_reasons reason,
55 Broker** broker_ptr = (
Broker**)lws_context_user(lws_get_context(wsi));
61 case LWS_CALLBACK_HTTP:
63 std::istringstream request( (
char*)in );
64 std::string left,middle,right;
65 std::getline(request, left,
'/');
66 std::getline(request, left,
'/');
67 std::getline(request, middle,
'/');
68 std::getline(request, right,
'/');
69 std::string description = broker->
getStream(left,middle,right);
70 char buf[LWS_SEND_BUFFER_PRE_PADDING + 2048 + LWS_SEND_BUFFER_POST_PADDING];
71 char* use = &(buf[LWS_SEND_BUFFER_PRE_PADDING]);
72 sprintf(use,
"HTTP/1.1 200 OK\n\n%s",description.c_str());
73 lws_write(wsi, (
unsigned char*) use, strlen(use), LWS_WRITE_HTTP);
76 lws_get_peer_addresses(wsi,lws_get_socket_fd(wsi),name,256,rip,256);
77 printf(
"HTTP Connection from %s (%s)!\n",name,rip);
92 enum lws_callback_reasons reason,
99 LWS_SEND_BUFFER_POST_PADDING];
100 char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
102 Broker** broker_ptr = (
Broker**)lws_context_user(lws_get_context(wsi));
105 broker = *broker_ptr;
109 case LWS_CALLBACK_ESTABLISHED:
110 printf(
"callback_isaac: LWS_CALLBACK_ESTABLISHED\n");
112 lws_get_peer_addresses(wsi,lws_get_socket_fd(wsi),dummy,32,pss->
url,32);
115 case LWS_CALLBACK_SERVER_WRITEABLE:
127 printf(
"WebSocketDataConnector: Dropped one dropable package!\n");
134 char* buffer = json_dumps( message->
json_root, 0 );
137 sprintf(p,
"%s",buffer);
138 m = lws_write(wsi, (
unsigned char*)p, n, LWS_WRITE_TEXT);
142 lwsl_err(
"ERROR %d writing to socket\n", n);
149 while (l > 1 && !lws_send_pipe_choked(wsi));
155 case LWS_CALLBACK_RECEIVE:
158 json_t* input = json_loads((
const char *)in, 0, NULL);
161 json_object_set_new( message->
json_root,
"url", json_string( pss->
url ) );
168 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
172 lws_get_peer_addresses(wsi,lws_get_socket_fd(wsi),name,256,rip,256);
173 printf(
"ISAAC Connection from %s (%s)!\n",name,rip);
185 static struct lws_protocols protocols[] = {
193 "isaac-json-protocol",
203 setlogmask(LOG_UPTO (LOG_DEBUG));
204 openlog(
"lwsts", LOG_PID | LOG_PERROR, LOG_DAEMON);
205 lws_set_log_level(7, lwsl_emit_syslog);
206 struct lws_context_creation_info info;
207 memset(&info, 0,
sizeof info);
208 info.protocols = protocols;
209 #ifndef LWS_NO_EXTENSIONS 210 info.extensions = NULL;
212 info.user = (
void*)(&
broker);
216 if (interface.compare(std::string(
"*")) != 0)
217 info.iface = interface.c_str();
218 context = lws_create_context(&info);
219 if (context == NULL) {
220 lwsl_err(
"libwebsocket init failed\n");
229 bool force_exit =
false;
230 while (n >= 0 && !force_exit)
232 n = lws_service(context, 0);
233 lws_callback_on_writable_all_protocol(context,&protocols[1]);
242 lws_context_destroy(context);
MessageTemplate * clientGetMessage()
errorCode clientSendMessage(MessageTemplate *message)
#define ISAAC_MAX_RECEIVE
std::string getStream(std::string connector, std::string name, std::string ref)
ThreadList< MessageTemplate * > messagesIn
static pthread_mutex_t deep_copy_mutex
MetaDataClient * addDataClient()
errorCode init(int port, std::string interface)