Skip to content

Commit

Permalink
successfully added mysql support for cmarketcap
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Mar 20, 2018
1 parent a8ed6ff commit ee0d719
Show file tree
Hide file tree
Showing 15 changed files with 651 additions and 735 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cmarketcap
*.db-journal

test/url_token_test
test/mysql_api_test
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CFLAGS=-Wall -g $(shell pkg-config --cflags jansson sqlite3 libcurl) -DCM_DEBUG_
LDFLAGS=$(shell pkg-config --libs jansson sqlite3 libcurl) -lpthread
CFLAGS=-Wall -g $(shell pkg-config --cflags jansson libcurl) $(shell mysql_config --cflags) -DCM_DEBUG_
LDFLAGS=$(shell pkg-config --libs jansson libcurl) $(shell mysql_config --libs) -lpthread

CC=gcc

TARGETS=cmarketcap

cmarketcap: signal_handler.o timer.o httpd.o cm_debug.o sql_api.o json_parser.o cmarketcap.c
cmarketcap: mysql_api.o signal_handler.o timer.o httpd.o cm_debug.o json_parser.o cmarketcap.c
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

mysql_api.o: mysql_api.c mysql_api.h

signal_handler.o: signal_handler.c signal_handler.h

timer.o: timer.c timer.h
Expand All @@ -16,9 +18,7 @@ httpd.o: httpd.c httpd.h

cm_debug.o: cm_debug.c cm_debug.h

sql_api.o: sql_api.c sql_api.h

json_parser.o: json_parser.c json_parser.h

clean:
rm -rf *.o $(TARGETS)
rm -rf *.o $(TARGETS)
Binary file removed cmarket.db
Binary file not shown.
17 changes: 10 additions & 7 deletions cmarketcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "cmarketcap.h"
#include "json_parser.h"
#include "sql_api.h"
#include "mysql_api.h"
#include "cm_debug.h"
#include "httpd.h"
#include "timer.h"
Expand Down Expand Up @@ -40,7 +40,7 @@ void *__cb_update_database(void *db_)
int hour_col = 0;
time_t time_diff;

sqlite3 *db = (sqlite3 *)db_;
MYSQL *db = (MYSQL *)db_;

pthread_detach(pthread_self());

Expand Down Expand Up @@ -80,7 +80,7 @@ void *__cb_update_database(void *db_)
min_col = 10;

coin_base = new_coin_entry_base();
fill_column(db, coin_base);
cm_update_table(db, coin_base);
min_col++; /* already filled a column */
min_hour_trig++;
DEBUG_MSG("min_col = %d, hour_col = %d, min_hour_trig = %d\n", min_col, hour_col, min_hour_trig);
Expand All @@ -104,10 +104,10 @@ void *__cb_update_database(void *db_)

int main(int argc, char *argv[])
{
sqlite3 *db;
MYSQL *db;
pthread_t update_database_id;

pthread_mutex_init(&sql_db_access, NULL);
pthread_mutex_init(&mysql_db_access, NULL);
pthread_mutex_init(&shift_column_locker, NULL);
global_data_handle.httpd_sockfd = 0;

Expand All @@ -118,7 +118,11 @@ int main(int argc, char *argv[])
CM_ERROR("Database error\n");
exit(1);
}

/*struct coin_entry_base *x = new_coin_entry_base();
init_coin_history_table(db, x);
free_entry_base(x);
exit(0);
*/
pthread_create(&update_database_id, NULL, __cb_update_database, (void *)db);
__cb_main_thread(db);

Expand All @@ -128,5 +132,4 @@ int main(int argc, char *argv[])
return 0;
} /* main */


#endif
103 changes: 0 additions & 103 deletions coin_reference.sql

This file was deleted.

33 changes: 14 additions & 19 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <pthread.h>

#include "cm_debug.h"
#include "sql_api.h"
#include "mysql_api.h"
#include "httpd.h"
#include "cmarketcap.h"

Expand All @@ -23,19 +23,19 @@
#define LOCK_SHIFT_COLUMN_LOCKER pthread_mutex_lock(&shift_column_locker)
#define UNLOCK_SHIFT_COLUMN_LOCKER pthread_mutex_unlock(&shift_column_locker)

void error_handle(char *err)
{
perror(err);
exit(EXIT_FAILURE);
}
static void parse_http_header(char *buff, struct myhttp_header *header);
static void *__cb_read_from_client(void *cb_arg);
static int check_http_header(struct myhttp_header *header);
static void send_header(int sockfd, char *code, char *type);
static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *db);

/** @brief Parse http headers into a data structure.
*
* This function is called by multiple threads. All modifications should be
* thread safe
*
*/
void parse_http_header(char *buff, struct myhttp_header *header)
static void parse_http_header(char *buff, struct myhttp_header *header)
{
char *token;
char *line = NULL;
Expand All @@ -53,7 +53,7 @@ void parse_http_header(char *buff, struct myhttp_header *header)
strcpy(header->protocol, token);
}

int check_http_header(struct myhttp_header *header)
static int check_http_header(struct myhttp_header *header)
{
return 0;
}
Expand All @@ -62,7 +62,7 @@ int check_http_header(struct myhttp_header *header)
*
* called by send_json_response
*/
void send_header(int sockfd, char *code, char *type)
static void send_header(int sockfd, char *code, char *type)
{
char buf[100];

Expand Down Expand Up @@ -253,7 +253,7 @@ void print_uri_base(struct uri_base *ub)
*
* We have to write a functio to tokenize url data.
*/
void send_json_response(int sockfd, struct myhttp_header *header, sqlite3 *db)
static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *db)
{
char code[4];
struct coin_status_base *sb;
Expand Down Expand Up @@ -340,11 +340,11 @@ void send_json_response(int sockfd, struct myhttp_header *header, sqlite3 *db)

struct __cb_args {
int socket;
sqlite3 *db;
MYSQL *db;
};

/* @brief Callback function for 'pthread_create' */
void *__cb_read_from_client(void *cb_arg)
static void *__cb_read_from_client(void *cb_arg)
{
char buffer[RD_BUFF_MAX];
int nbytes;
Expand All @@ -353,7 +353,7 @@ void *__cb_read_from_client(void *cb_arg)
struct __cb_args *_cb_arg = (struct __cb_args *)cb_arg;

sockfd = _cb_arg->socket;
sqlite3 *db = _cb_arg->db;
MYSQL *db = _cb_arg->db;

pthread_detach(pthread_self());

Expand Down Expand Up @@ -386,13 +386,8 @@ void *__cb_read_from_client(void *cb_arg)
pthread_exit(NULL);
}

int write_to_client(int sockfd)
{
return 0;
}

/* @brief needs openned database */
int __cb_main_thread(sqlite3 *db)
int __cb_main_thread(MYSQL *db)
{
int httpd_port;
int sockfd;
Expand Down
17 changes: 8 additions & 9 deletions httpd.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef HTTPD_H_
#define HTTPD_H_

#include <sqlite3.h>
#include "mysql_api.h"

struct myhttp_header {
char method[5];
Expand Down Expand Up @@ -30,16 +30,15 @@ struct uri_base {
};
/*! @} */ /* uri_tokenization */

extern int __cb_main_thread(sqlite3 *db);
extern void error_handle(char *err);
extern void *__cb_read_from_client(void *sock);
extern int write_to_client(int sockfd);
extern void parse_http_header(char *buff, struct myhttp_header *header);
extern int check_http_header(struct myhttp_header *header);
extern void send_header(int, char *, char *);
extern void send_json_response(int sockfd, struct myhttp_header *header, sqlite3 *db);
extern int __cb_main_thread(MYSQL *db);

/*! \addtogroup url_tokenization
* url tokenization mechanism
* @{
*/
extern struct uri_base *tokenize_uri(const char *uri);
extern void print_uri_base(struct uri_base *ub);
extern void free_uri_base(struct uri_base *ub);
/*! @} */ /* uri_tokenization */

#endif
13 changes: 9 additions & 4 deletions json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void print_entries(struct coin_entry_base *eb)
}
}

/* @brief freeup entire 'entry_base' structure */
/* @brief freeup entire 'coin_entry_base' C data structure */
void free_entry_base(struct coin_entry_base *eb)
{
struct coin_entry *entry, *entry2;
Expand All @@ -126,8 +126,13 @@ void free_entry_base(struct coin_entry_base *eb)
}
}

/* @brief Main json_parser function. This makes C data structures from
* JSON data
/**
* @brief Downloads cryptocurrency data from 'coinmarketcap.com'
* and return them as a C data structure.
*
* In this program we call this structure as 'coin_entry_base'
*
* @return 'coin_entry_base'
*/
struct coin_entry_base *new_coin_entry_base()
{
Expand Down Expand Up @@ -206,7 +211,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
/* @brief This will return a large text output which is needed
* by 'json_loads()'
*
* @param[in] url API url to be read.
* @param[in] url API url to be read from 'coinmarketcap.com'.
*/
static char *request_json_data(const char *url)
{
Expand Down
6 changes: 6 additions & 0 deletions json_parser.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/** @file json_parser.h
*
* @brief Fetch JSON data from 'coinmarketcap.com' and output them
* as linked list data structure.
*/

#ifndef JSON_PARSER_H
#define JSON_PARSER_H

Expand Down
Loading

0 comments on commit ee0d719

Please sign in to comment.