-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (78 loc) · 2.25 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <pthread.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include "thread.h"
#include "network.h"
#define MAXDATASIZE 100
using namespace std;
/*
* This is a starting port number.
* This will help to assign each neuron an uniq port number.
*/
static int seq = 3490;
/*
* A neuron defination.
* Neuron behaviour can be different based on implementation.
* Based on neuron behaviour Neuron can be different types.
*/
class Neuron {
private:
string key; /* Key */
string value; /* Value or Reply or Answer */
string port; /* Server communication port */
vector<string> client_ports; /* List of ports to connected neurons */
/* Wrapper of behaviour_handler to overcome fn_ptr to C function */
static void wrapper_bh(int fd, void *future) {
Neuron *obj = (Neuron *)future;
obj->behaviour_handler(fd);
}
/* Wrapper of service_server_network to overcome fn_ptr to C function */
static void *wrapper_ssn(void *dummy) {
Neuron *obj = (Neuron *)dummy;
obj->service_server_network();
}
/* Define neuron behaviour */
void behaviour_handler(int fd) {
char buf[MAXDATASIZE];
int num = rx(fd, buf, MAXDATASIZE);
printf("Server received from Client: ### %s :: %d\n", buf, num);
}
/* Create neuron port for incoming communication */
void service_server_network() {
create_server(port.c_str(), wrapper_bh, this);
pthread_exit(NULL);
}
public:
/* Neuron constructor with key and value */
Neuron(string key, string value) {
stringstream convert;
convert << seq;
port = convert.str();
create_thread(wrapper_ssn, this);
}
};
/*
* These are two functions to test the Neuron
*/
void tx_handeler(int fd, void *future) {
tx(fd, "Hello World!");
}
void *service_client_network(void *dummy) {
create_client("127.0.0.1", "3490", tx_handeler, NULL);
pthread_exit(NULL);
}
int main() {
Neuron n("name", "Steve");
/* Test purpose */
sleep(2);
create_thread(service_client_network, NULL);
sleep(2);
create_thread(service_client_network, NULL);
pthread_exit(NULL);
}