Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <sys/time.h>
- #include <arpa/inet.h>
- #include <fcntl.h> // for open
- #include <unistd.h> // for close
- #include <pthread.h>
- #include <sys/inotify.h>
- #include <poll.h>
- #include <sqlite3.h>
- #include <iostream>
- #include <string>
- #include "database.cpp"
- #define LISTENING_IP "10.3.141.1"
- #define LISTENING_PORT 6667
- using namespace std;
- pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
- void * socketThread(void *arg) {
- int newSocket = *((int *)arg);
- struct sockaddr_in addr;
- socklen_t addr_size = sizeof(struct sockaddr_in);
- int res = getpeername(newSocket, (struct sockaddr *)&addr, &addr_size);
- string clientIp = inet_ntoa(addr.sin_addr);
- cout << "Thread started for " << clientIp << "\n";
- //Setting non blocking mode
- int flags = fcntl(newSocket, F_GETFL);
- fcntl(newSocket, F_SETFL, flags | O_NONBLOCK);
- char buffer[128];
- char message[128];
- while(1){
- string stateStr = getStateFromIp(clientIp);
- if(stateStr.empty()){
- cout << "Error, cant get state from database";
- }
- memset(message, 0, sizeof message);
- strcpy(message, "<");
- strcat(message, stateStr.c_str());
- strcat(message, ">");
- send(newSocket, message , 128 , MSG_NOSIGNAL | MSG_DONTWAIT);
- printf("Sent : %s\n", message);
- struct pollfd fd;
- fd.fd = newSocket; // your socket handler
- fd.events = POLLIN;
- int ret = poll(&fd, 1, 5000); // 1 second for timeout
- ssize_t rc = recv(newSocket, buffer, sizeof(buffer), 0);
- if (rc == 0 || rc == -1 || ret == -1 || ret == 0) { //-1 is error, 0 is timeout
- if(ret == 0){
- printf("Closing socket because of timeout for %s\n", clientIp);
- }
- else{
- printf("Closing socket because of error for %s\n", clientIp);
- }
- close(newSocket);
- pthread_exit(NULL);
- pthread_detach(pthread_self());
- }
- else {
- string strResponse(buffer);
- int batLvl = stoi(strResponse);
- printf("Received: %s\n", buffer);
- updateClientBatLevel(clientIp, batLvl);
- }
- int infd = inotify_init();
- int wd = inotify_add_watch(infd, "trigger", IN_MODIFY);
- struct pollfd fd2;
- fd2.fd = infd;
- fd2.events = POLLIN;
- int rc2 = poll(&fd2, 1, 5000); //sleep 5 second if not change file
- if(rc2 > 0){
- printf("FILE MODIFIED");
- usleep(50 * 1000);
- }
- inotify_rm_watch(infd, wd);
- close(infd);
- }
- }
- int main(){
- // openDatabase();
- int serverSocket, newSocket;
- struct sockaddr_in serverAddr;
- struct sockaddr_storage serverStorage;
- socklen_t addr_size;
- serverSocket = socket(PF_INET, SOCK_STREAM, 0);
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(LISTENING_PORT);
- serverAddr.sin_addr.s_addr = inet_addr(LISTENING_IP);
- memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
- while(bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr) ) < 0){
- perror("ERROR on binding on port, retrying in 5 sec");
- sleep(5);
- }
- if(listen(serverSocket, 50) == 0){
- printf("Listening on %s:%d\n", LISTENING_IP, LISTENING_PORT);
- }
- else{
- printf("Error\n");
- }
- pthread_t tid[60];
- int i = 0;
- while(1) {
- addr_size = sizeof serverStorage;
- newSocket = accept(serverSocket, (struct sockaddr *)&serverStorage, &addr_ size);
- if (newSocket < 0) {
- perror("ERROR on accept, retrying in 5 sec");
- sleep(5);
- }
- if(pthread_create(&tid[i], NULL, socketThread, &newSocket) != 0 ){
- printf("Failed to create thread\n");
- }
- }
- // closeDatabase();
- printf("End\n");
- return 0;
- }
- #include <sqlite3.h>
- #include <iostream>
- #include <string>
- using namespace std;
- sqlite3 *db;
- void openDatabase(){
- sqlite3_open("database.db", &db);
- }
- void closeDatabase(){
- sqlite3_close(db);
- }
- void setDefaultState(const string &state){
- sqlite3_stmt *stmt;
- sqlite3_prepare_v2(db, "UPDATE state SET state = ? WHERE client_id IS NULL", -1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, state.c_str(), -1, 0);
- sqlite3_step(stmt);
- sqlite3_finalize(stmt);
- }
- bool hasClientSpecificState(const string &ip){
- sqlite3_stmt *stmt;
- int rc;
- sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM state JOIN client ON client.id = state.client_id AND client.ip = ?;", -1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, ip.c_str(), -1, 0);
- while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
- int nb = sqlite3_column_int(stmt, 0);
- sqlite3_finalize(stmt);
- return nb > 0;
- }
- return false;
- }
- void setStateForIp(const string &state, const string &ip){
- sqlite3_stmt *stmt;
- string sql;
- if(!hasClientSpecificState(ip)){
- sql = "INSERT INTO state(client_id, state) VALUES((SELECT id FROM client WHERE client.ip = ?), ?);";
- sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, ip.c_str(), -1, 0);
- sqlite3_bind_text(stmt, 2, state.c_str(), -1, 0);
- }
- else{
- string sql = "UPDATE state SET state = ? WHERE EXISTS(SELECT * FROM client WHERE client.ip = ? AND client.id = client_id);";
- sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, state.c_str(), -1, 0);
- sqlite3_bind_text(stmt, 2, ip.c_str(), -1, 0);
- }
- sqlite3_step(stmt);
- sqlite3_finalize(stmt);
- }
- void updateClientBatLevel(const string &ip, int batLvl){
- openDatabase();
- sqlite3_stmt *stmt;
- int rc;
- string retStr;
- sqlite3_prepare_v2(db, "UPDATE client SET bat_level = ? WHERE ip = ?", -1, &stmt, NULL);
- sqlite3_bind_int(stmt, 1, batLvl);
- sqlite3_bind_text(stmt, 2, ip.c_str(), -1, 0);
- sqlite3_step(stmt);
- sqlite3_finalize(stmt);
- closeDatabase();
- }
- string getStateFromIp(const string &ip){
- openDatabase();
- sqlite3_stmt *stmt;
- int rc;
- string retStr;
- sqlite3_prepare_v2(db, "SELECT state.state from state LEFT JOIN client ON client.id = state.client_id WHERE client.ip = ? OR state.client_id IS NULL ORDER BY client_id DESC LIMIT 1", -1, &stmt, NULL);
- sqlite3_bind_text(stmt, 1, ip.c_str(), -1, 0);
- while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
- retStr = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
- sqlite3_finalize(stmt);
- break;
- }
- closeDatabase();
- return retStr;
- }
- #!/bin/bash
- sqlite3 database.db <<EOF
- DROP TABLE IF EXISTS client;
- DROP TABLE IF EXISTS state;
- DROP TABLE IF EXISTS variable;
- CREATE TABLE client(id INTEGER NOT NULL PRIMARY KEY , mac VARCHAR(255), ip VARCHAR(25) NOT NULL, bat_level INTEGER, ttime INTEGER, signal INTEGER, inactive INTEGER);
- CREATE TABLE state(id INTEGER NOT NULL PRIMARY KEY , client_id INTEGER, state TEXT NOT NULL);
- CREATE TABLE variable(id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255) NOT NULL, value TEXT NOT NULL);
- INSERT INTO state(client_id, state) VALUES(NULL, '255,0,0;0.2;0.2;static;none');
- EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement