This commit is contained in:
2016-09-17 15:04:12 +03:00
parent 95a6f1fe97
commit 2442ceaee2
4 changed files with 127 additions and 0 deletions

53
job_queue.c Normal file
View File

@@ -0,0 +1,53 @@
#include <stdlib.h>
#include <string.h>
#include "sql.h"
#include "job_queue.h"
struct msg_t **slot;
int total_queues = 0;
int start_msg_queue(void)
{
int i;
for (i = 0; i < total_queues; i++) {
if (slot[i] == NULL) {
slot[i] = (struct msg_t *)calloc(100, sizeof(struct msg_t));
if (slot[i] == NULL)
return -1;
return i;
}
}
return -1;
}
void free_queue(int id)
{
free(slot[id]);
}
int start_job_queue(int poolsize)
{
slot = (struct msg_t **)calloc(poolsize, sizeof(struct msg_t *));
if (slot == NULL)
return -1;
total_queues = poolsize;
return 0;
}
int add_msg_to_queue(int id, struct msg_t buf)
{
int i;
for (i = 0; i < 100; i++) {
if (slot[id][i].meta.is_recv) {
memcpy(&slot[id][i], &buf, sizeof(struct msg_t));
return 0;
}
}
return -1;
}
void create_job(struct msg_t buf)
{
// add_job_in_db();
}

12
job_queue.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef JOB_QUEUE_H
#define JOB_QUEUE_H
#include "protocol.h"
//void* create_job(void *arg);
int start_msg_queue(void);
void free_queue(int id);
int start_job_queue(int poolsize);
int add_msg_to_queue(int id, struct msg_t buf);
#endif /* JOB_QUEUE_H */

55
sql.c Normal file
View File

@@ -0,0 +1,55 @@
#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>
#include "log.h"
#include "sql.h"
#include "confparser.h"
int add_user()
{
MYSQL *con = mysql_init(NULL);
char sql[200];
if (con == NULL) {
log(ERROR, "Failed to add user: %s", mysql_error(con));
return -1;
}
if (mysql_real_connect(con, conf_db_hostname(), "rmps", conf_db_pass(),
NULL, 0, NULL, 0) == NULL) {
log(ERROR, "Failed to add user: %s", mysql_error(con));
mysql_close(con);
return -1;
}
sprintf(sql, "call addUser(`%s`, `%s`, `%s`, `%s`, `%s`, `%s`)",
"user", "noob", "asd@asd.asd", "asdsadasdassda", "salt", "more");
if (mysql_query(con, sql)) {
//fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
log(ERROR, "Failed to add user: %s", mysql_error(con));
return -1;
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
MYSQL_FIELD *field;
while ((row = mysql_fetch_row(result))) {
int i;
for (i = 0; i < num_fields; i++) {
if (i == 0) {
while(field = mysql_fetch_field(result)) {
printf("| %s ", field->name);
}
printf("\n");
}
printf("| %s ", row[i] ? row[i] : "NULL");
}
}
mysql_free_result(result);
mysql_close(con);
exit(0);
}

7
sql.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef SQL_H
#define SQL_H
int add_user();
#endif /* SQL_H */