Basics - implementation of secured client-server architecture
NNSP consists of several functions (written in C language) which are very helpful to build-up basic structure of your secured client-server architecture. Following text is a simple tutorial on how to use those functions. It is divided into two parts - building-up server and building-up client. After compiling and executing examples you should have established communication of client and server and gained a symmetric key for encryption purposes.
Building-up a server
To implement your server properly you have to use at least 4 functions of NNSP framework and define two callback functions by yourself. To use NNSP functions you have to include header file "nnsp/nnsp.h" into your source code.
server_t* server_create(int port, int inputs, int neurons, int weights, int iterations, ServerCallbackControl callback_control, ServerCallbackProcess callback_process); typedef int (*ServerCallbackControl)(server_t* server); typedef int (*ServerCallbackProcess)(server_t* server, connection_t* connection, message_t* message); int server_run(server_t* server); int server_stop(server_t* server); int server_destroy(server_t* server);
Function for creation of server server_create takes few parameters to operates with:
- int port: defines a port which a server will be listening on
- int inputs: number of inputs of neurons
- int neurons: number of neurons in neural network
- int weights: range of neuron weights
- int iterations: number of iterations of synchronization
More inputs, neurons and large weight range means higher security but slower synchronization. Thus, if you set higher values of those parameters, you should set more iterations.
There are also two parameters in server_create function which are pointers to callback-functions. These callbacks have to be defined by yourself because of specific implementation of server's task. ServerCallbackControl is used to control the server by an administrator (e.g. start, stop, logging, etc.), ServerCallbackCProcess is used to process incoming messages from client. Usage of those callbacks will be shown in sample code.
Function server_create returns pointer to server context server_t which is then used in the rest of functions to run the server, stop it and destroy it (to free allocated memory).
Following code will show the usage of server functions.
#include "nnsp/nnsp.h" int main() { server_t* server = server_create(4242, 100, 3, 10, 5000, server_control, server_process); server_run(server); server_stop(server); server_destroy(server); return 0; }
At line 1 there is included header file needed for NNSP functions usage.
At line 5 server_create function is called. Server will be listening on port 4242, neural network will be constructed of 3 neurons with 100 inputs and values of their weights will be in range (-10;10). Synchronization will be done up to 5000 iterations. Two callbacks called server_control and server_process have to be defined. Function server_create returns a pointer to server_t called server. That context is used at lines 6, 7 and 8 to run the server, stop it and destroy its context.
Now we have to implement callback functions. In previous code we specified the names of callback functions as server_control and server_process.
#include <stdio.h> #include "nnsp/nnsp.h" // controlling of server int server_control(server_t* server) { fprintf(stderr, "type 'q' to stop server\r\n"); // infinite loop while(1) { if(getchar() == 'q') break; } return 0; } // process incoming message int server_process(server_t* server, connection_t* connection, message_t* message) { return 0; } int main() { server_t* server = server_create(4242, 100, 3, 10, 5000, server_control, server_process); server_run(server); server_stop(server); server_destroy(server); return 0; }
At line 1 header file for standard input/output functions is included.
Lines 4-17 define body of callback function server_control. Callback should always contain an infinite cycle because of controlling its run. If there is no cycle callback ends immediately and server is stopped and destroyed (at lines 30 and 31). In this case it contains an infinite cycle which is break when user write 'q' in the console. Callback also gets pointer to server_t context in order to manipulate with server and get info of its running (e.g. server's socket, logging etc.).
Lines 19-24 define body of callback function server_process. Parameters of that callback are two contexts. First context regards to server itself, second context regards to connection from which some message comes in and third context regards to that message. Usage of those contexts can be found in other samples.
Building-up a client
Implementation of a client is very similar to implementation of a server. To use NNSP functions you have to include header file "nnsp/nnsp.h" into your source code.
client_t* client_create(char* address, int port, ClientCallbackControl callback_control, ClientCallbackProcess callback_process); typedef int (*ClientCallbackControl)(client_t* client); typedef int (*ClientCallbackProcess)(client_t* client, connection_t* connection, message_t* message); int client_run(client_t* client); int client_stop(client_t* client); int client_destroy(client_t* client;
Function for creation of client client_create takes few parameters to operates with:
- char* address: domain name or IP address of server
- int port: defines a port which a client will be connecting to
There is no setting of client's neural network because this setting is transfered from server when connection is established.
There are also two parameters (pointers to callback-function) in client_create function. These callbacks have to be defined by yourself because of specific implementation of client's task. ClientCallbackControl is used to control the client by an administrator (e.g. start, stop, logging, etc.), ClientCallbackProcess is used to process incoming messages from server. Usage of those callbacks will be shown in sample code.
Function client_create returns pointer to client context client_t which is then used in the rest of functions to run the client, stop it and destroy it (free allocated memory). As you can see functions for client are very similar to those for server.
Following code shows the usage of client functions.
#include "nnsp/nnsp.h" int main() { client_t* client = client_create("localhost", 4242, client_control, client_process); client_run(client); client_stop(client); client_destroy(client); return 0; }
At line 1 there is included header file needed for NNSP functions usage.
At line 5 client_create function is called. Client tries to establish connection with server at 'localhost' at port 4242. Two callbacks called client_control and client_process have to be defined. Function client_create returns a pointer to client_t called client. That context is used at lines 6, 7 and 8 to run the client, to stop it and to destroy its context.
Now we have to implement callback functions. In previous code we specified the names of callback functions as client_control and client_process.
#include <stdio.h> #include "nnsp/nnsp.h" // controlling of client int client_control(client_t* client) { // infinite loop while(1) { } return 0; } // process incoming message int client_process(client_t* client, connection_t* connection, message_t* message) { return 0; } int main() { client_t* client = client_create("localhost", 4242, client_control, client_process); client_run(client); client_stop(client); client_destroy(client); return 0; }
At line 1 header file for standard input/output functions is included.
Lines 4-13 define body of callback function client_control. It contains infinite loop where some commands entered from console can be processed. Callback also gets pointer to client_t context in order to manipulate with client and get info of its running (e.g. client's socket, logging etc.).
Lines 15-20 define body of callback function client_process. Parameters of that callback are three contexts. First context regards to client itself, second context regards to connection the message comes from and third context regards to the message itself. Usage of those contexts can be found in other samples.