Commit 86cf9c75 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

restructuring project, new makefile

parent 51130f1d
......@@ -4,3 +4,6 @@
[submodule "lib/base64"]
path = lib/base64
url = https://github.com/zhicheng/base64.git
[submodule "json-parser"]
path = json-parser
url = https://github.com/udp/json-parser
CC = gcc
INCLUDES = -I/usr/include/postgresql -I$(LDIR)/gateway_protocol -I$(LDIR)/base64 -I$(LDIR)/task_queue
LFLAGS = -Llib
LDIR = lib
LIBD = -pthread -lpq
SRC = src/gateway.c
OBJ = obj
MAIN = gateway
.PHONY: depend clean
$(LDIR)/gateway_protocol/gateway_protocol.o:
$(CC) -c $(LDIR)/gateway_protocol/gateway_protocol.c -o $(OBJ)/gateway_protocol.o -I$(LDIR)/gateway_protocol
$(CC) -c $(LDIR)/base64/base64.c -o $(OBJ)/base64.o -I$(LDIR)/base64
$(CC) -c $(LDIR)/task_queue/task_queue.c -o $(OBJ)/task_queue.o -I$(LDIR)/task_queue
$(CC) $(SRC) $(OBJ)/gateway_protocol.o $(OBJ)/base64.o $(OBJ)/task_queue.o -o $(MAIN) $(LIBD) $(INCLUDES)
all: $(MAIN)
@echo Compiling gateway project
$(MAIN): $(OBJ)/gateway_protocol.o $(OBJ)/base64.o $(OBJ)/task_queue.o
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
$(RM) *.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
# line needed by makedepend
File added
File deleted
{
"gateway_id" : "fa:16:3e:8c:4c:ef",
"gateway_password" : "12345",
"db_type" : "PostgreSQL",
"db_connection" : {
"db_address" : "127.0.0.1",
"db_port" : "5432",
"db_name" : "iotserver",
"user_name" : "vlad",
"user_password" : "dev"
}
}
#ifndef BASE64_H
#define BASE64_H
#define BASE64_ENCODE_OUT_SIZE(s) ((unsigned int)((((s) + 2) / 3) * 4 + 1))
#define BASE64_DECODE_OUT_SIZE(s) ((unsigned int)(((s) / 4) * 3))
/*
* out is null-terminated encode string.
* return values is out length, exclusive terminating `\0'
*/
unsigned int
base64_encode(const unsigned char *in, unsigned int inlen, char *out);
/*
* return values is out length
*/
unsigned int
base64_decode(const char *in, unsigned int inlen, unsigned char *out);
#endif /* BASE64_H */
#ifndef __GATEWAY_PROTOCOL_H__
#define __GATEWAY_PROTOCOL_H__
#include <stdint.h>
#include <string.h>
#define GATEWAY_PROTOCOL_PACKET_SIZE_MAX 128
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
GATEWAY_PROTOCOL_PACKET_TYPE_DATA_SEND = 0x00,
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ = 0x04,
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND = 0x05,
GATEWAY_PROTOCOL_PACKET_TYPE_STAT = 0x10,
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ = 0x20,
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND = 0x21,
GATEWAY_PROTOCOL_PACKET_TYPE_UNKNOWN = 0xFF
} gateway_protocol_packet_type_t;
typedef enum {
GATEWAY_PROTOCOL_STAT_ACK = 0,
GATEWAY_PROTOCOL_STAT_ACK_PEND,
GATEWAY_PROTOCOL_STAT_NACK = 0xFF
} gateway_protocol_stat_t;
void gateway_protocol_init(const uint8_t *appkey, const uint8_t devid);
void gateway_protocol_packet_encode (
const gateway_protocol_packet_type_t packet_type,
const uint8_t payload_length,
const uint8_t *payload,
uint8_t *packet_length,
uint8_t *packet);
uint8_t gateway_protocol_packet_decode (
gateway_protocol_packet_type_t *packet_type,
uint8_t *payload_length,
uint8_t *payload,
const uint8_t packet_length,
const uint8_t *packet);
#ifdef __cplusplus
}
#endif
#endif // __GATEWAY_PROTOCOL_H__
\ No newline at end of file
/* vim: set et ts=3 sw=3 sts=3 ft=c:
*
* Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
* https://github.com/udp/json-parser
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _JSON_H
#define _JSON_H
#ifndef json_char
#define json_char char
#endif
#ifndef json_int_t
#ifndef _MSC_VER
#include <inttypes.h>
#define json_int_t int64_t
#else
#define json_int_t __int64
#endif
#endif
#include <stdlib.h>
#ifdef __cplusplus
#include <string.h>
extern "C"
{
#endif
typedef struct
{
unsigned long max_memory;
int settings;
/* Custom allocator support (leave null to use malloc/free)
*/
void * (* mem_alloc) (size_t, int zero, void * user_data);
void (* mem_free) (void *, void * user_data);
void * user_data; /* will be passed to mem_alloc and mem_free */
size_t value_extra; /* how much extra space to allocate for values? */
} json_settings;
#define json_enable_comments 0x01
typedef enum
{
json_none,
json_object,
json_array,
json_integer,
json_double,
json_string,
json_boolean,
json_null
} json_type;
extern const struct _json_value json_value_none;
typedef struct _json_object_entry
{
json_char * name;
unsigned int name_length;
struct _json_value * value;
} json_object_entry;
typedef struct _json_value
{
struct _json_value * parent;
json_type type;
union
{
int boolean;
json_int_t integer;
double dbl;
struct
{
unsigned int length;
json_char * ptr; /* null terminated */
} string;
struct
{
unsigned int length;
json_object_entry * values;
#if defined(__cplusplus) && __cplusplus >= 201103L
decltype(values) begin () const
{ return values;
}
decltype(values) end () const
{ return values + length;
}
#endif
} object;
struct
{
unsigned int length;
struct _json_value ** values;
#if defined(__cplusplus) && __cplusplus >= 201103L
decltype(values) begin () const
{ return values;
}
decltype(values) end () const
{ return values + length;
}
#endif
} array;
} u;
union
{
struct _json_value * next_alloc;
void * object_mem;
} _reserved;
#ifdef JSON_TRACK_SOURCE
/* Location of the value in the source JSON
*/
unsigned int line, col;
#endif
/* Some C++ operator sugar */
#ifdef __cplusplus
public:
inline _json_value ()
{ memset (this, 0, sizeof (_json_value));
}
inline const struct _json_value &operator [] (int index) const
{
if (type != json_array || index < 0
|| ((unsigned int) index) >= u.array.length)
{
return json_value_none;
}
return *u.array.values [index];
}
inline const struct _json_value &operator [] (const char * index) const
{
if (type != json_object)
return json_value_none;
for (unsigned int i = 0; i < u.object.length; ++ i)
if (!strcmp (u.object.values [i].name, index))
return *u.object.values [i].value;
return json_value_none;
}
inline operator const char * () const
{
switch (type)
{
case json_string:
return u.string.ptr;
default:
return "";
};
}
inline operator json_int_t () const
{
switch (type)
{
case json_integer:
return u.integer;
case json_double:
return (json_int_t) u.dbl;
default:
return 0;
};
}
inline operator bool () const
{
if (type != json_boolean)
return false;
return u.boolean != 0;
}
inline operator double () const
{
switch (type)
{
case json_integer:
return (double) u.integer;
case json_double:
return u.dbl;
default:
return 0;
};
}
#endif
} json_value;
json_value * json_parse (const json_char * json,
size_t length);
#define json_error_max 128
json_value * json_parse_ex (json_settings * settings,
const json_char * json,
size_t length,
char * error);
void json_value_free (json_value *);
/* Not usually necessary, unless you used a custom mem_alloc and now want to
* use a custom mem_free.
*/
void json_value_free_ex (json_settings * settings,
json_value *);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
#ifndef __TASK_QUEUE_H__
#define __TASK_QUEUE_H__
/* Task queue that create threads for completing tasks
* having an upper threads limit
*/
struct task_queue;
typedef struct task_queue task_queue_t;
typedef void (*task_func_t)(void *arg);
task_queue_t * task_queue_create(const int max_threads);
void task_queue_destroy(task_queue_t *tq);
int task_queue_enqueue(task_queue_t *tq, task_func_t task, void *arg);
void task_queue_suspend(task_queue_t *tq);
void task_queue_unsuspend(task_queue_t *tq);
int task_queue_get_size(task_queue_t *tq);
int task_queue_is_empty(task_queue_t *tq);
#endif // __TASK_QUEUE_H__
[Unit]
Description=HPC&A IoT Gateway
[Service]
User=vlad
Type=simple
ExecStart=/home/vlad/thso.gateway/bin/gateway 1>/dev/null
WorkingDirectory=/home/vlad/thso.gateway
Restart=on-failure
KillSignal=SIGKILL
[Install]
WantedBy=multi-user.target
json-parser @ e6426aef
Subproject commit e6426aefb6ded8dcc549d847e2a453aea5dd04a6
CC = gcc
CFLAGS = -Wall
LFLAGS = -pthread -lpq -lm
INC_DIR = ../inc
OBJ_DIR = ../obj
BIN_DIR = ../bin
SRC_DIR = .
INCLUDES = -I/usr/include/postgresql \
-I$(INC_DIR)
TARGET = $(BIN_DIR)/gateway
OBJS = $(wildcard $(OBJ_DIR)/*.o)
#
#SRCS = $(wildcard $(SRC_DIR)/*.c)
#OBJS = $(SRCS:.c=.o)
#
$(TARGET) : $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) $(OBJS) -o $(TARGET) $(LFLAGS)
$(OBJ_DIR)/%.o : $(SRC_DIR)/%.c
$(CC) -c -MD $(CFLAGS) $(INCLUDES) $< -o $@
-include $(OBJ_DIR)/*.d
.PHONY: clean print
clean :
rm -f $(BIN_DIR)/* $(OBJ_DIR)/*
print :
$(info $$OBJS is [${OBJS}])
/* This is a public domain base64 implementation written by WEI Zhicheng. */
#include "base64.h"
#define BASE64_PAD '='
#define BASE64DE_FIRST '+'
#define BASE64DE_LAST 'z'
/* BASE 64 encode table */
static const char base64en[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/',
};
/* ASCII order for BASE 64 decode, 255 in unused character */
static const unsigned char base64de[] = {
/* nul, soh, stx, etx, eot, enq, ack, bel, */
255, 255, 255, 255, 255, 255, 255, 255,
/* bs, ht, nl, vt, np, cr, so, si, */
255, 255, 255, 255, 255, 255, 255, 255,
/* dle, dc1, dc2, dc3, dc4, nak, syn, etb, */
255, 255, 255, 255, 255, 255, 255, 255,
/* can, em, sub, esc, fs, gs, rs, us, */
255, 255, 255, 255, 255, 255, 255, 255,
/* sp, '!', '"', '#', '$', '%', '&', ''', */
255, 255, 255, 255, 255, 255, 255, 255,
/* '(', ')', '*', '+', ',', '-', '.', '/', */
255, 255, 255, 62, 255, 255, 255, 63,
/* '0', '1', '2', '3', '4', '5', '6', '7', */
52, 53, 54, 55, 56, 57, 58, 59,
/* '8', '9', ':', ';', '<', '=', '>', '?', */
60, 61, 255, 255, 255, 255, 255, 255,
/* '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', */
255, 0, 1, 2, 3, 4, 5, 6,
/* 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', */
7, 8, 9, 10, 11, 12, 13, 14,
/* 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', */
15, 16, 17, 18, 19, 20, 21, 22,
/* 'X', 'Y', 'Z', '[', '\', ']', '^', '_', */
23, 24, 25, 255, 255, 255, 255, 255,
/* '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', */
255, 26, 27, 28, 29, 30, 31, 32,
/* 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', */
33, 34, 35, 36, 37, 38, 39, 40,
/* 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', */
41, 42, 43, 44, 45, 46, 47, 48,
/* 'x', 'y', 'z', '{', '|', '}', '~', del, */
49, 50, 51, 255, 255, 255, 255, 255
};
unsigned int
base64_encode(const unsigned char *in, unsigned int inlen, char *out)
{
int s;
unsigned int i;
unsigned int j;
unsigned char c;
unsigned char l;
s = 0;
l = 0;
for (i = j = 0; i < inlen; i++) {
c = in[i];
switch (s) {
case 0:
s = 1;
out[j++] = base64en[(c >> 2) & 0x3F];
break;
case 1:
s = 2;
out[j++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
break;
case 2:
s = 0;
out[j++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
out[j++] = base64en[c & 0x3F];
break;
}
l = c;
}
switch (s) {
case 1:
out[j++] = base64en[(l & 0x3) << 4];
out[j++] = BASE64_PAD;
out[j++] = BASE64_PAD;
break;
case 2:
out[j++] = base64en[(l & 0xF) << 2];
out[j++] = BASE64_PAD;
break;
}
out[j] = 0;
return j;
}
unsigned int
base64_decode(const char *in, unsigned int inlen, unsigned char *out)
{
unsigned int i;
unsigned int j;
unsigned char c;
if (inlen & 0x3) {
return 0;
}
for (i = j = 0; i < inlen; i++) {
if (in[i] == BASE64_PAD) {
break;
}
if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST) {
return 0;
}
c = base64de[(unsigned char)in[i]];
if (c == 255) {
return 0;
}
switch (i & 0x3) {
case 0:
out[j] = (c << 2) & 0xFF;
break;
case 1:
out[j++] |= (c >> 4) & 0x3;
out[j] = (c & 0xF) << 4;
break;
case 2:
out[j++] |= (c >> 2) & 0xF;
out[j] = (c & 0x3) << 6;
break;
case 3:
out[j++] |= c;
break;
}
}
return j;
}
......@@ -255,66 +255,30 @@ void process_packet(void *request) {
// send the msg until ack is received
uint8_t received_ack = 0;
uint8_t pend_send_retries = PEND_SEND_RETRIES_MAX;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 300000; // 300ms ack recv timeout
do {
packet_encode(
req->gch.app_key,
req->gch.dev_id,
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND,
payload_length, payload,
&(req->packet_length), req->packet);
do {
send_gcom_ch(&(req->gch), req->packet, req->packet_length);
// set timeout
if (setsockopt(req->gch.server_desc, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt error");
}
if (recv_gcom_ch(&(req->gch),
req->packet,
&(req->packet_length),
DEVICE_DATA_MAX_LENGTH) > 9)
{ /* min packet size. timeout -> -1 */
uint8_t recv_app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE +1];
uint8_t recv_dev_id = 0xFF;
if (packet_decode(
recv_app_key,
&recv_dev_id,
&(req->packet_type),
&(req->packet_length), req->packet,
req->packet_length, req->packet))
{
if (!memcmp(recv_app_key, req->gch.app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE) &&
recv_dev_id == req->gch.dev_id &&
req->packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_STAT &&
req->packet_length == 1 &&
req->packet[0] == GATEWAY_PROTOCOL_STAT_ACK)
{
snprintf(db_query, sizeof(db_query),
"UPDATE pend_msgs SET ack = True WHERE app_key = '%s' AND dev_id = %d AND msg = '%s'",
(char *)req->gch.app_key, req->gch.dev_id, msg_cont
);
// 300 ms
usleep(300000);
pthread_mutex_lock(&mutex);
res = PQexec(conn, db_query);
pthread_mutex_unlock(&mutex);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "error db deleting : %s", PQerrorMessage(conn));
}
PQclear(res);
if (PQresultStatus(res) == PGRES_TUPLES_OK) {
if (!PQntuples(res) || strcmp(PQgetvalue(res, 0, 2), msg_cont)) {
received_ack = 1;
printf("ACK received\n");
} else {
printf("error: packet_type = %02X, not STAT\n");
}
}
}
PQclear(res);
printf("received_ack = %d, retries = %d\n", received_ack, pend_send_retries);
} while (!received_ack && pend_send_retries--);
// cancel timeout
tv.tv_usec = 0;
if (setsockopt(req->gch.server_desc, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt error");
}
} else {
gateway_protocol_mk_stat(
&(req->gch),
......@@ -325,6 +289,34 @@ void process_packet(void *request) {
printf("nothing for app %s dev %d\n", (char *)req->gch.app_key, req->gch.dev_id);
}
} else if (req->packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_STAT) {
// TODO change to ACK_PEND = 0x01
if (payload[0] == 0x00) {
char db_query[200];
snprintf(db_query, sizeof(db_query),
"SELECT * FROM pend_msgs WHERE app_key = '%s' AND dev_id = %d AND ack = False",
(char *)req->gch.app_key, req->gch.dev_id
);
pthread_mutex_lock(&mutex);
res = PQexec(conn, db_query);
pthread_mutex_unlock(&mutex);
if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res)) {
snprintf(db_query, sizeof(db_query),
"UPDATE pend_msgs SET ack = True WHERE app_key = '%s' AND dev_id = %d AND msg = '%s'",
(char *)req->gch.app_key, req->gch.dev_id, PQgetvalue(res, 0, 2)
);
PQclear(res);
pthread_mutex_lock(&mutex);
res = PQexec(conn, db_query);
pthread_mutex_unlock(&mutex);
if (PQresultStatus(res) == PGRES_COMMAND_OK) {
printf("pend_msgs updated\n");
} else {
fprintf(stderr, "database error : %s\n", PQerrorMessage(conn));
}
}
PQclear(res);
}
} else {
gateway_protocol_mk_stat(
&(req->gch),
......
#include <gateway_protocol.h>
#define GATEWAY_PROTOCOL_APP_KEY_SIZE 8
static uint8_t app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE];
static uint8_t dev_id = 0xFF;
void gateway_protocol_init(const uint8_t *appkey, const uint8_t devid) {
memcpy(app_key, appkey, GATEWAY_PROTOCOL_APP_KEY_SIZE);
dev_id = devid;
}
void gateway_protocol_packet_encode (
const gateway_protocol_packet_type_t packet_type,
const uint8_t payload_length,
const uint8_t *payload,
uint8_t *packet_length,
uint8_t *packet)
{
*packet_length = 0;
memcpy(&packet[*packet_length], app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE);
(*packet_length) += GATEWAY_PROTOCOL_APP_KEY_SIZE;
packet[*packet_length] = dev_id;
(*packet_length)++;
packet[*packet_length] = packet_type;
(*packet_length)++;
packet[*packet_length] = payload_length;
(*packet_length)++;
memcpy(&packet[*packet_length], payload, payload_length);
(*packet_length) += payload_length;
}
uint8_t gateway_protocol_packet_decode (
gateway_protocol_packet_type_t *packet_type,
uint8_t *payload_length,
uint8_t *payload,
const uint8_t packet_length,
const uint8_t *packet)
{
uint8_t p_len = 0;
uint8_t appkey[GATEWAY_PROTOCOL_APP_KEY_SIZE];
uint8_t dev;
memcpy(appkey, &packet[p_len], GATEWAY_PROTOCOL_APP_KEY_SIZE);
p_len += GATEWAY_PROTOCOL_APP_KEY_SIZE;
dev = packet[p_len];
p_len++;
*packet_type = (gateway_protocol_packet_type_t) packet[p_len];
p_len++;
p_len++;
*payload_length = packet_length - p_len;
memcpy(payload, &packet[p_len], *payload_length);
p_len += *payload_length;
return (memcmp(appkey, app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE) &&
dev == dev_id &&
p_len == packet_length);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
#include<stdint.h>
#include<pthread.h>
#include<gateway_protocol.h>
#include<sys/time.h>
void * connection_handler (void *args);
int main (int argc, char **argv) {
int server_desc, client_desc;
struct sockaddr_in server, client;
socklen_t client_socklen;
char buf[1024] = "";
if ((server_desc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket creation error");
return EXIT_FAILURE;
}
server.sin_family = AF_INET;
server.sin_port = htons(9043);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(server_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("binding error");
return EXIT_FAILURE;
}
listen(server_desc, 3);
printf("liteninig...\n");
while ((client_desc = accept(server_desc, (struct sockaddr *) &client, &client_socklen))) {
//strncpy(buf, "Hello, client!", sizeof(buf));
//write(client_desc, buf, strlen(buf));
printf("packet received!\n");
pthread_t thr;
if (pthread_create(&thr, NULL, connection_handler, (void *) &client_desc) < 0) {
perror("thread creation failed");
return EXIT_FAILURE;
}
pthread_join(thr, NULL);
}
return EXIT_SUCCESS;
}
void *connection_handler(void *args) {
int client_desc = *(int *)args;
uint8_t buf[128] = "";
uint8_t buf_len = 0;
uint8_t payload[128];
uint8_t payload_length = 0;
//strncpy(buf, "connection handler greetings!", sizeof(buf));
//write(client_desc, buf, strlen(buf));
if ((buf_len = recv(client_desc, buf, sizeof(buf), 0)) > 0) {
uint8_t dev_id = 0xFF;
gateway_protocol_packet_type_t packet_type;
if (gateway_protocol_packet_decode(
&dev_id,
&packet_type,
&payload_length, payload,
buf_len, buf))
{
if (packet_type == GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ) {
printf("TIME REQ received\n");
struct timeval tv;
buf_len = 0;
buf[0] = dev_id;
buf_len++;
buf[1] = GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND;
buf_len++;
gettimeofday(&tv, NULL);
memcpy(&buf[buf_len], &tv.tv_sec, sizeof(uint32_t));
buf_len += sizeof(uint32_t);
write(client_desc, buf, buf_len);
} else {
perror("packet type error");
}
} else {
perror("packet decode error");
}
}
}
This diff is collapsed.
#include <stdlib.h>
#include <pthread.h>
#include "task_queue.h"
struct queue_job {
task_func_t func;
void *arg;
struct queue_job *next;
};
typedef struct queue_job queue_job_t;
struct task_queue {
queue_job_t *next;
queue_job_t *last;
pthread_mutex_t mutex;
int active_tasks;
int size;
int suspended;
};
static int max_active_tasks;
static queue_job_t * queue_job_create(task_func_t func, void *arg);
static void queue_job_destroy(queue_job_t *qj);
static queue_job_t * queue_job_get_next(task_queue_t *tq);
static void * queue_job_exec(void *arg_tq);
task_queue_t * task_queue_create(const int max_threads) {
task_queue_t *tq = (task_queue_t *)malloc(sizeof(task_queue_t));
if (!tq) {
return NULL;
}
tq->next = NULL;
tq->last = NULL;
tq->active_tasks = 0;
tq->size = 0;
tq->suspended = 0;
pthread_mutex_init(&(tq->mutex), NULL);
max_active_tasks = max_threads > 0 ? max_threads : 1;
return tq;
}
void task_queue_destroy(task_queue_t *tq) {
queue_job_t *qj1, *qj2;
if (!tq) {
return;
}
pthread_mutex_lock(&(tq->mutex));
tq->suspended = 1;
qj1 = tq->next;
while (qj1) {
qj2 = qj1->next;
queue_job_destroy(qj1);
qj1 = qj2;
}
pthread_mutex_destroy(&(tq->mutex));
free(tq);
}
int task_queue_enqueue(task_queue_t *tq, task_func_t task, void *arg) {
queue_job_t *qj;
pthread_t thread;
int i, can_run, need_run, run, ret;
if (!tq || !task) {
return -1;
}
qj = queue_job_create(task, arg);
if (!qj) {
return -1;
}
pthread_mutex_lock(&(tq->mutex));
if (!tq->next) {
tq->next = qj;
tq->last = qj;
} else {
tq->last->next = qj; // assign next
tq->last = qj; // move pointer
}
tq->size++;
if ((tq->active_tasks < max_active_tasks) & !tq->suspended) {
can_run = max_active_tasks - tq->active_tasks;
need_run = tq->size - tq->active_tasks;
run = need_run < can_run ? need_run : can_run;
for (i = 0; i < run; i++) {
if (pthread_create(&thread, NULL, queue_job_exec, tq) < 0) {
// perror("new thread creation error");
} else {
pthread_detach(thread);
tq->active_tasks++;
}
}
}
ret = tq->active_tasks;
pthread_mutex_unlock(&(tq->mutex));
return ret;
}
void task_queue_suspend(task_queue_t *tq) {
if (!tq) {
return;
}
pthread_mutex_lock(&(tq->mutex));
tq->suspended = 1;
pthread_mutex_unlock(&(tq->mutex));
}
void task_queue_unsuspend(task_queue_t *tq) {
pthread_t thread;
int i, can_run, need_run, run;
if (!tq) {
return;
}
pthread_mutex_lock(&(tq->mutex));
tq->suspended = 0;
if (tq->size && tq->active_tasks < max_active_tasks) {
can_run = max_active_tasks - tq->active_tasks;
need_run = tq->size - tq->active_tasks;
run = need_run < can_run ? need_run : can_run;
for (i = 0; i < run; i++) {
if (pthread_create(&thread, NULL, &queue_job_exec, tq) < 0) {
// perror("new thread creation error");
} else {
pthread_detach(thread);
tq->active_tasks++;
}
}
}
pthread_mutex_unlock(&(tq->mutex));
}
int task_queue_get_size(task_queue_t *tq) {
int res;
pthread_mutex_lock(&(tq->mutex));
res = tq->size;
pthread_mutex_unlock(&(tq->mutex));
return res;
}
int task_queue_is_empty(task_queue_t *tq) {
return task_queue_get_size(tq) == 0;
}
static queue_job_t * queue_job_create(task_func_t func, void *arg) {
queue_job_t *qj;
if (!func) {
return NULL;
}
qj = (queue_job_t *)malloc(sizeof(queue_job_t));
qj->func = func;
qj->arg = arg;
qj->next = NULL;
return qj;
}
static void queue_job_destroy(queue_job_t *qj) {
if (!qj) {
return;
}
free(qj);
}
static queue_job_t * queue_job_get_next(task_queue_t *tq) {
queue_job_t *qj;
if (!tq) {
return NULL;
}
pthread_mutex_lock(&(tq->mutex));
if (tq->next) {
qj = tq->next;
tq->next = qj->next;
} else {
qj = NULL;
}
pthread_mutex_unlock(&(tq->mutex));
return qj;
}
static void * queue_job_exec(void *arg_tq) {
task_queue_t *tq = (task_queue_t *)arg_tq;
queue_job_t *qj;
pthread_t thread;
int i, can_run, need_run, run;
if (!tq) {
return NULL;
}
qj = queue_job_get_next(tq);
if (qj && qj->func) {
qj->func(qj->arg);
}
queue_job_destroy(qj);
pthread_mutex_lock(&(tq->mutex));
tq->active_tasks--;
tq->size--;
if (tq->size && tq->active_tasks < max_active_tasks && !tq->suspended) {
can_run = max_active_tasks - tq->active_tasks;
need_run = tq->size - tq->active_tasks;
run = need_run < can_run ? need_run : can_run;
for (i = 0; i < run; i++) {
if (pthread_create(&thread, NULL, &queue_job_exec, tq) < 0) {
// perror("new thread creation error");
} else {
pthread_detach(thread);
tq->active_tasks++;
}
}
}
pthread_mutex_unlock(&(tq->mutex));
return NULL;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment