root/vtcross/branches/wrodgers/src/service_management_layer/ServiceManagementLayer_Pre_Transact_Change.cpp @ 277

Revision 277, 54.7 KB (checked in by wrodgers, 15 years ago)

Updating SML functionality

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * LICENSE INFORMATION GOES HERE
5 */
6
7/* Inter-component communication handled by sockets and FD's. 
8 * Server support has been completely implemented and tested.
9 *
10 * Services are stored in a SQLite DB by the ID of the CE that registered them.  Service
11 * support has been completely implemented and tested.
12 *
13 * Missions are loaded from an XML file, connected with services provided by components,
14 * and run.  See the documentation for the "PerformActiveMission" below for important
15 * info.  Mission support has been completely implemented but mostly untested
16 * (except for the XML parsing, which has been fully tested).
17 *
18 * It is important that in the XML configuration file, inputs and outputs are listed
19 * in the order that they appear.  See the documentation for the "PerformActiveMission"
20 * method for more information.
21 *
22 *
23 */
24
25#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28#include <cstring>
29#include <stdint.h>
30
31/*#include "../../trunk/src/include/vtcross/common.h"
32
33#include "../../trunk/src/include/vtcross/components.h"
34#include "../../trunk/src/include/vtcross/containers.h"
35#include "../../trunk/src/include/vtcross/debug.h"
36#include "../../trunk/src/include/vtcross/error.h"
37#include "../../trunk/src/include/vtcross/socketcomm.h"*/
38
39#include "../../trunk/src/include/vtcross/common.h"
40
41#include "../../trunk/src/include/vtcross/components.h"
42#include "../../trunk/src/include/vtcross/containers.h"
43#include "../../trunk/src/include/vtcross/debug.h"
44#include "../../trunk/src/include/vtcross/error.h"
45#include "../../trunk/src/include/vtcross/socketcomm.h"
46
47
48#include <cstdlib>
49#include <cstring>
50#include <stdint.h>
51#include <math.h>
52
53#include <arpa/inet.h>
54#include <iostream>
55#include <netinet/in.h>
56#include <netdb.h>
57#include <fcntl.h>
58#include <sys/ioctl.h>
59#include <sys/mman.h>
60#include <sys/socket.h>
61#include <sys/types.h>
62#include <sys/wait.h>
63
64#include "../../trunk/src/include/tinyxml/tinyxml.h"
65#include "../../trunk/src/include/tinyxml/tinystr.h"
66
67//#include "tinyxml/tinyxml.h"
68//#include "tinyxml/tinystr.h"
69
70#include "sqlite3.h"
71//#include "sqlite3ext.h"
72
73typedef struct services_s * services_DB;
74
75struct services_s {
76    char filename[64];
77    char tablename[64];
78    char command[2048];
79    sqlite3 *db;
80    unsigned int num_columns;
81};
82
83services_DB _services_DB;
84const char *_SML_Config;
85
86int callback(void *notUsed, int argc, char **argv, char **azColName){
87    int i;
88    for(i=0; i<argc; i++){
89        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
90    }
91    printf("\n");
92    return 0;
93}
94
95
96
97ServiceManagementLayer::ServiceManagementLayer()
98{
99    LOG("Creating Service Management Layer.\n");
100    shellSocketFD = -1;
101    numberOfCognitiveEngines = 0;
102    CE_Present = false;
103    cogEngSrv = 1;
104}
105
106
107ServiceManagementLayer::~ServiceManagementLayer()
108{
109    char *errorMsg;
110    strcpy(_services_DB->command, "drop table ");
111    strcat(_services_DB->command, _services_DB->tablename);
112    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
113    if( rc!=SQLITE_OK && rc!=101 )
114        fprintf(stderr, "SQL error: %s\n", errorMsg);
115    strcpy(_services_DB->command, "vacuum");
116    rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
117    if( rc!=SQLITE_OK && rc!=101 )
118        fprintf(stderr, "SQL error: %s\n", errorMsg);
119    free(_services_DB);
120}
121
122
123ServiceManagementLayer::ServiceManagementLayer(const char* SML_Config, \
124        const char* serverName, const char* serverPort)
125{
126    LOG("Creating Service Management Layer.\n");
127    _SML_Config = SML_Config;
128
129    ConnectToShell(serverName, serverPort);
130    CE_List = (CE_Reg *) malloc(10*sizeof(struct CE_Reg));
131    CE_List = new CE_Reg[10];
132
133    miss = new Mission[10];
134    for(int i = 0; i < 10; i++)
135        miss[i].services = new Service[20];
136
137    Current_ID = 0;
138
139    LoadConfiguration(SML_Config, miss);
140    CreateServicesDB();
141}
142
143//VERIFIED May 26
144void
145ServiceManagementLayer::CreateServicesDB()
146{
147    _services_DB = (services_DB) malloc(sizeof(struct services_s));
148    char *errorMsg;
149
150    // create database
151
152    // copy filename
153    unsigned int i=0;
154    strcpy(_services_DB->filename, "Services_Table");
155
156    // execute create database command
157    // database handle
158    //_services_DB->db = NULL;
159    sqlite3_open(_services_DB->filename, &(_services_DB->db));
160    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
161
162    // create table
163
164    // copy tablename
165    strcpy(_services_DB->tablename, "Services");
166
167    // number of columns in the table
168    _services_DB->num_columns = 2;
169
170    // generate command
171    strcpy(_services_DB->command, "CREATE TABLE ");
172    strcat(_services_DB->command, _services_DB->tablename);
173    strcat(_services_DB->command, "(");
174    //strcat(_services_DB->command, "SRVID INTEGER PRIMARY KEY, ");
175    strcat(_services_DB->command, cols[0]);
176    strcat(_services_DB->command, " INT, ");
177    strcat(_services_DB->command, cols[1]);
178    strcat(_services_DB->command, " TEXT");
179    strcat(_services_DB->command, ");");
180
181    // execute create table command
182  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
183  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
184
185    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail);
186    if( rc!=SQLITE_OK && rc!=101 )
187        printf("SQL error(1): %d\n", rc);
188    rc = sqlite3_step(ppStmt);
189    if( rc!=SQLITE_OK && rc!=101 )
190        printf("SQL error(2): %d\n", rc);
191}
192
193
194void
195ServiceManagementLayer::SendComponentType()
196{
197    SendMessage(shellSocketFD, "response_sml");
198    LOG("SML responded to GetRemoteComponentType query.\n");
199}
200
201
202void
203ServiceManagementLayer::ConnectToShell(const char* serverName, \
204        const char* serverPort)
205{
206    shellSocketFD = ClientSocket(serverName, serverPort);
207   
208    //Set the shell socket to non-block mode
209    /*int oldflags = fcntl (shellSocketFD, F_GETFL, 0);
210    oldflags |= O_NONBLOCK;
211    fcntl (shellSocketFD, F_SETFL, oldflags);*/
212
213    RegisterComponent();
214}
215
216void
217ServiceManagementLayer::MessageHandler(int32_t ID)
218{
219    char buffer[256];   
220    memset(buffer, 0, 256); 
221    int32_t _FD;
222    //printf("ID= %d\n", ID);
223   
224    if(ID != -1)
225        _FD = CE_List[ID].FD;
226    else
227        _FD = shellSocketFD;
228
229    //printf("FD=%d, shellFD=%d", _FD, shellSocketFD);
230    ssize_t msgLength = recv(_FD, buffer, 256, MSG_PEEK);
231    if(msgLength == 0){
232        //printf("Error reading from socket.\n");
233        //sleep(5);     
234        return;
235    }
236
237
238    size_t i;
239    for(i = 0; i < 256; i++) {
240        if(strcmp(&buffer[i], "\0") == 0)
241            break;
242    }
243    // Read the message into msgBuffer
244    recv(_FD, buffer, i + 1, 0);
245    //printf("********* %s **********\n", buffer);
246    // TODO
247    // If we send integer op codes rather than strings, this process will be
248    // MUCH faster since instead of donig string compares we can simply
249    // switch on the integer value...
250    /*if(strcmp(buffer, "register_service") == 0) {
251        if(strcmp(buffer, "policy_geo") == 0) {
252        }
253        else if(strcmp(buffer, "policy_time") == 0) {
254        }
255        else if(strcmp(buffer, "policy_spectrum") == 0) {
256        }
257        else if(strcmp(buffer, "policy_spacial") == 0) {
258        }
259    }
260    else if(strcmp(buffer, "deregister_service") == 0) {
261        if(strcmp(buffer, "policy_geo") == 0) {
262        }
263        else if(strcmp(buffer, "policy_time") == 0) {
264        }
265        else if(strcmp(buffer, "policy_spectrum") == 0) {
266        }
267        else if(strcmp(buffer, "policy_spacial") == 0) {
268        }
269    }*/
270    if(strcmp(buffer, "query_component_type") == 0) {
271        SendComponentType();
272    }
273    else if(strcmp(buffer, "reset_sml") == 0) {
274        Reset();
275    }
276    else if(strcmp(buffer, "shutdown_sml") == 0) {
277        Shutdown();
278    }
279    else if(strcmp(buffer, "register_engine_cognitive") == 0) {
280        RegisterCognitiveEngine(ID);
281    }
282    else if(strcmp(buffer, "register_service") == 0) {
283        ReceiveServices(ID);
284    }
285    else if(strcmp(buffer, "send_component_type") == 0) {
286        SendComponentType();
287    }
288    else if(strcmp(buffer, "list_services") == 0) {
289        ListServices();
290    }
291    else if(strcmp(buffer, "set_active_mission") == 0) {
292        SetActiveMission();
293    }
294    else if(strcmp(buffer, "request_optimization") == 0) {
295        PerformActiveMission();
296    }
297}
298
299
300
301void
302ServiceManagementLayer::Shutdown()
303{
304    DeregisterComponent();
305}
306
307
308void
309ServiceManagementLayer::Reset()
310{
311    DeregisterComponent();
312    LoadConfiguration(_SML_Config, miss);
313}
314
315
316void
317ServiceManagementLayer::RegisterComponent()
318{
319    SendMessage(shellSocketFD, "register_sml");
320    LOG("ServiceManagementLayer:: Registration message sent.\n");
321}
322
323
324void
325ServiceManagementLayer::DeregisterComponent()
326{
327    SendMessage(shellSocketFD, "deregister_sml");
328    LOG("ServiceManagementLayer:: Deregistration message sent.\n");
329
330    shutdown(shellSocketFD, 2);
331    close(shellSocketFD);
332    shellSocketFD = -1;
333    LOG("ServiceManagementLayer:: Shell socket closed.\n");
334}
335
336/* Streams config data directly from the shell to the CE, and checks
337 * for an "ack" message from the CE after every sent message
338 * to know when to stop communication.
339 */
340void
341ServiceManagementLayer::TransferRadioConfiguration(int32_t ID)
342{
343    struct timeval selTimeout;
344    fd_set sockSet;
345    int32_t rc = 0;
346    char buffer[256];
347    //Send data until the CE sends an ACK message back
348    while(rc==0){
349        memset(buffer, 0, 256);
350        //Receive data from Shell
351        ReadMessage(shellSocketFD, buffer);
352        //Send data to CE
353        SendMessage(CE_List[ID].FD, buffer);
354        FD_ZERO(&sockSet);
355        FD_SET(CE_List[ID].FD, &sockSet);
356        selTimeout.tv_sec = 0;
357        selTimeout.tv_usec = 0;
358        //Check if there is a message on the CE socket ready to be processed
359        rc=select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout);
360    }
361    memset(buffer, 0, 256);
362    ReadMessage(CE_List[ID].FD, buffer);
363    SendMessage(shellSocketFD, buffer);
364}
365
366//Simmilar to TransferRadioConfig, just with Experience data
367void
368ServiceManagementLayer::TransferExperience(int32_t ID)
369{
370    struct timeval selTimeout;
371    fd_set sockSet;
372    int32_t rc = 0;
373    char buffer[256];
374    //Send data until the CE sends an ACK message back
375    while(rc==0){
376        memset(buffer, 0, 256);
377        //Receive data from Shell
378        ReadMessage(shellSocketFD, buffer);
379        //Send data to CE
380        SendMessage(CE_List[ID].FD, buffer);
381        FD_ZERO(&sockSet);
382        FD_SET(CE_List[ID].FD, &sockSet);
383        selTimeout.tv_sec = 0;
384        selTimeout.tv_usec = 0;
385        //Check if there is a message on the CE socket ready to be processed
386        rc = select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout);
387    }
388    memset(buffer, 0, 256);
389    ReadMessage(CE_List[ID].FD, buffer);
390    SendMessage(shellSocketFD, buffer);
391}
392
393//VERIFIED May 26
394void
395ServiceManagementLayer::ReceiveServices(int32_t ID)
396{
397    char buffer[256];
398    memset(buffer, 0, 256);
399    ReadMessage(CE_List[ID].FD, buffer);
400    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
401   
402    // generate command
403    //printf("%s\n", _services_DB->command);
404    strcpy(_services_DB->command, "insert into ");
405    strcat(_services_DB->command, _services_DB->tablename);
406    strcat(_services_DB->command, " (");
407    strcat(_services_DB->command, cols[0]);
408    strcat(_services_DB->command, ", ");
409    strcat(_services_DB->command, cols[1]);
410    strcat(_services_DB->command, ") ");
411    strcat(_services_DB->command, " values(");
412    sprintf(_services_DB->command, "%s%d", _services_DB->command, ID);
413    strcat(_services_DB->command, ", '");
414    strcat(_services_DB->command, buffer);
415    strcat(_services_DB->command, "');");
416   
417    //printf("search command: %s\n", _services_DB->command);
418    // execute add command
419    char *errorMsg;
420    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
421    if( rc!=SQLITE_OK && rc!=101 )
422        fprintf(stderr, "SQL error: %s\n", errorMsg);
423    printf("SML: Registering service '%s' from component number '%d'\n", buffer, ID);
424
425}
426
427//This method associates the services that components provide with the services that are requested in the mission
428//Each service in the mission is given the ID and FD of a component that has registered to provide that service
429//Deregistration is okay until this method is called without a reload, but if deregistration occurs after this
430// method is called it needs to be called again even if other engines also provide the services
431
432//Verified May 29
433void
434ServiceManagementLayer::SetActiveMission()
435{
436    char buffer[256];
437    memset(buffer, 0, 256);
438    ReadMessage(shellSocketFD, buffer);
439    int32_t missID = atoi(buffer);
440    for(activeMission = 0; activeMission < 10; activeMission++)
441    {
442        if(miss[activeMission].missionID == missID)
443            break;
444    }
445    for(int i = 0; i < miss[activeMission].numServices; i++)
446    {   
447        if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while")){
448        strcpy(_services_DB->command, "select ");
449        strcat(_services_DB->command, _services_DB->tablename);
450        strcat(_services_DB->command, ".* from ");
451        strcat(_services_DB->command, _services_DB->tablename);
452        strcat(_services_DB->command, " where Service_Name==");
453        sprintf(_services_DB->command, "%s'%s';", _services_DB->command, miss[activeMission].services[i].name.c_str());
454       
455
456        sqlite3_stmt * pStatement;
457        int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, -1, &pStatement, NULL);
458        if (rc == SQLITE_OK){
459            if (sqlite3_step(pStatement) == SQLITE_ROW)
460                 miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0);
461            else {
462                    printf("services_DB:: Mission requires service not provided by any connected component.\n");
463                    rc=31337;
464            }
465        } else {
466            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command);
467        }
468
469        sqlite3_finalize(pStatement);
470        miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD;}
471    }
472    printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[2].componentID);
473}
474
475//This is a helper method for the "PerformActiveMission" function
476//It takes the ID's of a source and a destination and does a raw transfer between them
477//It knows when to stop when an "ack" signal returns from the destination component
478//An ID of "-1" coresponds to the shell
479void
480ServiceManagementLayer::TransactData(int32_t sourceID, int32_t destID)
481{
482    char buffer[256];
483    printf("transact occuring between %d and %d\n", sourceID, destID);
484    if(sourceID == destID)
485    {
486        memset(buffer, 0, 256);
487        ReadMessage(CE_List[sourceID].FD, buffer);
488        //Maybe change to strstr for more descriptive return values
489        while(strcmp(buffer, "ACK")!=0){
490            SendMessage(CE_List[sourceID].FD, buffer);
491            ReadMessage(CE_List[sourceID].FD, buffer);
492        }
493        return;
494       
495    }
496    int32_t FD_src, FD_dest;
497    struct timeval selTimeout;
498    if(sourceID==-1)
499        FD_src=shellSocketFD;
500    else
501        FD_src=CE_List[sourceID].FD;
502    if(destID==-1)
503        FD_dest=shellSocketFD;
504    else
505        FD_dest=CE_List[destID].FD;
506
507    int32_t rc = 0;
508    fd_set sockSet;
509    //Send data until the CE sends an ACK message back
510    while(rc==0){
511        memset(buffer, 0, 256);
512        //Receive data from Shell
513        printf("reading data from %d\n", FD_src);
514        ReadMessage(FD_src, buffer);
515        //Send data to CE
516        printf("sending data to %d\n", FD_dest);
517        SendMessage(FD_dest, buffer);
518        FD_ZERO(&sockSet);
519        FD_SET(FD_dest, &sockSet);
520        selTimeout.tv_sec = 1;
521        selTimeout.tv_usec = 50;
522        //Check if there is a message on the CE socket ready to be processed
523        rc=select(FD_dest + 1, &sockSet, NULL, NULL, &selTimeout);
524    }
525    memset(buffer, 0, 256);
526    ReadMessage(FD_dest, buffer);
527    printf("done transact data %s\n", buffer);
528}
529
530// Rules for active missions (currently)
531// -Three inputs/outputs per service and per mission
532// -Inputs simply define a path, so multiple variables can be transmitted over the same input
533// -Each component that takes input is responsible for sending an "ack" signal when transmission is complete
534//   -Transmission will continue until the "ack" signal is recieved
535// -Any input from shell to a CE must be the first input
536// -CE-CE inputs must be listed in the order the ouptuts are generated
537// -No ability to store data locally at this time, so all CE's must be ready to receive data as soon as it is sent
538// -Shell should be ready for output in order that it is generated
539// -If/While supported and nesting too (except nested while loops)
540// -Conditions must be boolean flags
541// -Flags are set by putting either the character string "true" or "false" on the buffer
542
543// This function works by first sending the inputs from the shell to the appropriate components
544// The first service should begin immeadiately, as should any others who have all of their input paramaters
545// When they complete, the output path is found and the data is transfered as it becomes available
546// Presumably at this point the second function has all of it's paramaters, so it begins to compute, and the cycle repeats
547// If the generated output is an overall output, it is sent on to the shell
548// "if" and "while" statements are handled by setting up a faux service that has a true input, a false input, and a boolean flag
549// If the true input is non-NULL and the flag is true, the statements execute
550// Likewise, if the false input is non-NULL and the flag is false, the statements execute
551// These flags are set during execution any time one of these faux services' inputs appear in an output statement 
552//
553void
554ServiceManagementLayer::PerformActiveMission()
555{
556    int i = 0;
557    char buffer[256];
558    if(!miss[activeMission].input[0].empty()){
559        printf("mission input 1 not empty\n");
560        for(int j = 0; j < miss[activeMission].numServices; j++)
561        {
562            if(miss[activeMission].services[j].input[0].compare(miss[activeMission].input[0]) == 0){
563                if(miss[activeMission].services[j].num_conds > 0){
564                    memset(buffer, 0, 256);
565                    ReadMessage(shellSocketFD, buffer);
566                    if(strcmp(buffer, "false")==0)
567                        miss[activeMission].services[j].cond_flag=false;
568                    else
569                        miss[activeMission].services[j].cond_flag=true;
570                    break;
571                }
572                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
573                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
574                TransactData(-1, miss[activeMission].services[j].componentID);
575            }   
576        }
577    }
578    if(!miss[activeMission].input[1].empty()){
579        for(int j = 0; j < miss[activeMission].numServices; j++)
580        {
581            if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[0]) == 0){
582                if(miss[activeMission].services[j].num_conds > 0){
583                    memset(buffer, 0, 256);
584                    ReadMessage(shellSocketFD, buffer);
585                    if(strcmp(buffer, "false")==0)
586                        miss[activeMission].services[j].cond_flag=false;
587                    else
588                        miss[activeMission].services[j].cond_flag=true;
589                    break;
590                }
591                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
592                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
593                TransactData(-1, miss[activeMission].services[j].componentID);
594            }   
595            if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[1]) == 0)
596                TransactData(-1, miss[activeMission].services[j].componentID); 
597        }
598    }
599    if(!miss[activeMission].input[2].empty()){
600        for(int j = 0; j < miss[activeMission].numServices; j++)
601        {
602            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[0]) == 0){
603                if(miss[activeMission].services[j].num_conds > 0){
604                    memset(buffer, 0, 256);
605                    ReadMessage(shellSocketFD, buffer);
606                    if(strcmp(buffer, "false")==0)
607                        miss[activeMission].services[j].cond_flag=false;
608                    else
609                        miss[activeMission].services[j].cond_flag=true;
610                    break;
611                }
612                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
613                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
614                TransactData(-1, miss[activeMission].services[j].componentID);
615            }   
616            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[1]) == 0)
617                TransactData(-1, miss[activeMission].services[j].componentID);
618            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[2]) == 0)
619                TransactData(-1, miss[activeMission].services[j].componentID); 
620        }
621    }
622    while(i != miss[activeMission].numServices)
623    {   
624        //TODO need to fix assignment of flags
625        if(miss[activeMission].services[i].name.compare("while")==0)
626        {
627            int32_t stmt_i = i;
628            i+=miss[activeMission].services[stmt_i].num_conds;
629            while((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\
630                  (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){
631                for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){
632                    if(strcmp(miss[activeMission].services[k].name.c_str(), "if")==0){
633                        int32_t stmt_k = k;
634                        if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\
635                           (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){
636                            for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){
637                                for(int m = 0; m < 3; m++){
638                                    for(int j = 0; j < 3; j++){
639                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \
640                                           !miss[activeMission].services[k].output[m].empty()){
641                                            TransactData(miss[activeMission].services[k].componentID, -1);
642                                            break;
643                                        }
644                                    }
645                                    for(int j = 0; j < miss[activeMission].numServices; j++){
646                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \
647                                           !miss[activeMission].services[k].output[m].empty()){
648                                            if(miss[activeMission].services[j].num_conds > 0){
649                                                memset(buffer, 0, 256);
650                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer);
651                                                if(strcmp(buffer, "false")==0)
652                                                    miss[activeMission].services[j].cond_flag=false;
653                                                else
654                                                    miss[activeMission].services[j].cond_flag=true;
655                                                break;
656                                            }
657                                            SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
658                                            SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
659                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
660                                            break;     
661                                        }       
662                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \
663                                           !miss[activeMission].services[k].output[m].empty()){
664                                            if(miss[activeMission].services[j].num_conds > 0){
665                                                memset(buffer, 0, 256);
666                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer);
667                                                if(strcmp(buffer, "false")==0)
668                                                    miss[activeMission].services[j].cond_flag=false;
669                                                else
670                                                    miss[activeMission].services[j].cond_flag=true;
671                                                break;
672                                            }
673                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
674                                            break;     
675                                        }       
676                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \
677                                           !miss[activeMission].services[k].output[m].empty()){
678                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
679                                            break;     
680                                        }
681                                    }   
682                                }
683                            }
684                        }
685                    }
686                    else{
687                        for(int m = 0; m < 3; m++)
688                        {
689                            for(int j = 0; j < 3; j++){
690                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \
691                                   !miss[activeMission].services[k].output[m].empty()){
692                                    TransactData(miss[activeMission].services[k].componentID, -1);
693                                    break;
694                                }
695                            }
696                            for(int j = 0; j < miss[activeMission].numServices; j++){
697                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \
698                                   !miss[activeMission].services[k].output[m].empty()){
699                                    if(miss[activeMission].services[j].num_conds > 0){
700                                        memset(buffer, 0, 256);
701                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer);
702                                        if(strcmp(buffer, "false")==0)
703                                            miss[activeMission].services[j].cond_flag=false;
704                                        else
705                                            miss[activeMission].services[j].cond_flag=true;
706                                        break;
707                                    }
708                                    SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
709                                    SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
710                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
711                                    break;     
712                                }       
713                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \
714                                   !miss[activeMission].services[k].output[m].empty()){
715                                    if(miss[activeMission].services[j].num_conds > 0){
716                                        memset(buffer, 0, 256);
717                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer);
718                                        if(strcmp(buffer, "false")==0)
719                                            miss[activeMission].services[j].cond_flag=false;
720                                        else
721                                            miss[activeMission].services[j].cond_flag=true;
722                                        break;
723                                    }
724                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
725                                    break;     
726                                }       
727                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \
728                                   !miss[activeMission].services[k].output[m].empty()){
729                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID);
730                                    break;     
731                                }
732                            }   
733                        }
734                    }
735                }
736            }
737        }
738        else if(miss[activeMission].services[i].name.compare("if")==0)
739        {
740            int32_t stmt_i = i;
741            i+=miss[activeMission].services[stmt_i].num_conds;
742            printf("if detected, i_old=%d, i_new=%d\n", stmt_i, i);
743            if((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\
744                  (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){
745                printf("cond_true!!!\n");
746                for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){
747                    if(strcmp(miss[activeMission].services[k].name.c_str(), "if")==0){
748                        int32_t stmt_k = k;
749                        if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\
750                           (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){
751                            for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){
752                                for(int m = 0; m < 3; m++)
753                                {
754                                    if(strstr(miss[activeMission].services[i].output[m].c_str(), "flag")){
755                                        for(int j = 0;  j < miss[activeMission].numServices; j++){
756                                            if((miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0])==0 || \
757                                                miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1])==0) &&
758                                                !miss[activeMission].services[i].output[m].empty()){
759                                                 memset(buffer, 0, 256);
760                                                 printf("conditional detected  %d, %d, %d\n", miss[activeMission].services[i].socketFD, i, j);
761                                                 ReadMessage(miss[activeMission].services[i].socketFD, buffer);
762                                                 printf("conditional detected  %s\n", buffer);
763                                                 if(strcmp(buffer, "false")==0)
764                                                     miss[activeMission].services[j].cond_flag=false;
765                                                 else
766                                                     miss[activeMission].services[j].cond_flag=true;
767                                                break;
768                                            }
769                                        }
770                                    }
771                                    else{
772                                        for(int j = 0; j < 3; j++){
773                                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].output[j])==0 && \
774                                               !miss[activeMission].services[i].output[m].empty()){
775                                        //printf("i=%d, m=%d, j=%d\n", i, m, j);
776                                                TransactData(miss[activeMission].services[i].componentID, -1);
777                                                break;
778                                            }
779                                        }
780                                        for(int j = 0; j < miss[activeMission].numServices; j++){
781                                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \
782                                               !miss[activeMission].services[i].output[m].empty()){
783                                                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
784                                                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
785                                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
786                                                break; 
787                                            }   
788                                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \
789                                               !miss[activeMission].services[i].output[m].empty()){
790                                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
791                                                break; 
792                                            }   
793                                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \
794                                               !miss[activeMission].services[i].output[m].empty()){
795                                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
796                                                break; 
797                                            }
798                                        }               
799                                    }   
800                                }
801                            }
802                        }
803                    }
804                    else{
805                        for(int m = 0; m < 3; m++)
806                        {
807                            if(strstr(miss[activeMission].services[i].output[m].c_str(), "flag")){
808                                for(int j = 0;  j < miss[activeMission].numServices; j++){
809                                    if((miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0])==0 || \
810                                        miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1])==0) &&
811                                        !miss[activeMission].services[i].output[m].empty()){
812                                         memset(buffer, 0, 256);
813                                         printf("conditional detected  %d, %d, %d\n", miss[activeMission].services[i].socketFD, i, j);
814                                         ReadMessage(miss[activeMission].services[i].socketFD, buffer);
815                                         printf("conditional detected  %s\n", buffer);
816                                         if(strcmp(buffer, "false")==0)
817                                             miss[activeMission].services[j].cond_flag=false;
818                                         else
819                                             miss[activeMission].services[j].cond_flag=true;
820                                        break;
821                                    }
822                                }
823                            }
824                            else{
825                                for(int j = 0; j < 3; j++){
826                                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].output[j])==0 && \
827                                       !miss[activeMission].services[i].output[m].empty()){
828                                //printf("i=%d, m=%d, j=%d\n", i, m, j);
829                                        TransactData(miss[activeMission].services[i].componentID, -1);
830                                        break;
831                                    }
832                                }
833                                for(int j = 0; j < miss[activeMission].numServices; j++){
834                                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \
835                                       !miss[activeMission].services[i].output[m].empty()){
836                                        SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
837                                        SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
838                                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
839                                        break; 
840                                    }   
841                                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \
842                                       !miss[activeMission].services[i].output[m].empty()){
843                                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
844                                        break; 
845                                    }   
846                                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \
847                                       !miss[activeMission].services[i].output[m].empty()){
848                                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
849                                        break; 
850                                    }
851                                }               
852                            }
853                        }
854                    }
855                }
856            }
857        }
858        else{
859            for(int m = 0; m < 3; m++)
860            {
861                //printf("\ninside SAM\n");
862                if(strstr(miss[activeMission].services[i].output[m].c_str(), "flag")){
863                    for(int j = 0;  j < miss[activeMission].numServices; j++){
864                        if((miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0])==0 || \
865                           miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1])==0) &&
866                           !miss[activeMission].services[i].output[m].empty()){
867                            memset(buffer, 0, 256);
868                            printf("conditional detected  %d, %d, %d\n", miss[activeMission].services[i].socketFD, i, j);
869                            ReadMessage(miss[activeMission].services[i].socketFD, buffer);
870                            printf("conditional detected  %s\n", buffer);
871                            if(strcmp(buffer, "false")==0)
872                                miss[activeMission].services[j].cond_flag=false;
873                            else
874                                miss[activeMission].services[j].cond_flag=true;
875                            break;
876                        }
877                    }
878                }
879                else{
880                        for(int j = 0; j < 3; j++){
881                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].output[j])==0 && \
882                               !miss[activeMission].services[i].output[m].empty()){
883                                //printf("i=%d, m=%d, j=%d\n", i, m, j);
884                                TransactData(miss[activeMission].services[i].componentID, -1);
885                                break;
886                            }
887                        }
888                        for(int j = 0; j < miss[activeMission].numServices; j++){
889                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \
890                               !miss[activeMission].services[i].output[m].empty()){
891                                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service");
892                                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str());
893                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
894                                break; 
895                            }   
896                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \
897                               !miss[activeMission].services[i].output[m].empty()){
898                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
899                                break; 
900                            }   
901                            if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \
902                               !miss[activeMission].services[i].output[m].empty()){
903                                TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID);
904                                break; 
905                            }
906                        }
907                }       
908            }
909        }
910        i++;
911    }
912}
913
914
915//Verified May 26
916void
917ServiceManagementLayer::ListServices()
918{
919    // generate commandi
920    strcpy(_services_DB->command, "select ");
921    strcat(_services_DB->command, _services_DB->tablename);
922    strcat(_services_DB->command, ".* from ");
923    strcat(_services_DB->command, _services_DB->tablename);
924    strcat(_services_DB->command, ";");
925
926    // execute print (select all)  command   
927    char *errorMsg;
928    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
929    if( rc!=SQLITE_OK && rc!=101 )
930        fprintf(stderr, "SQL error: %s\n", errorMsg);
931    printf("database %s, table %s:\n", _services_DB->filename, _services_DB->tablename);
932}
933
934
935void
936ServiceManagementLayer::ReloadConfiguration()
937{
938    LOG("ServiceManagementLayer:: Reloading Configuration.\n");
939    free(miss);
940    miss = new Mission[10];
941    for(int i = 0; i < 10; i++)
942        miss[i].services = new Service[20];
943    LoadConfiguration(_SML_Config, miss);
944}
945
946//IMPORTANT - See formatting instructions for correct parsing of data
947//Can currently handle 3 inputs and 3 outputs per service, but easily expandable
948//Also, can handle one layer of nested conditional statements, but could
949//be expanded to meet additional needs.  Only support now is for straight bool flags,
950//but support could be added for more complex conditionals later.
951
952//Components assigned to mission during "set active mission" stage so that
953//components can still continue to register after the configuration is loaded
954
955//Verified May 26
956void
957ServiceManagementLayer::LoadConfiguration(const char *SML_Config, Mission* &mList)
958{
959    TiXmlElement *pMission;
960    TiXmlElement *pService;
961    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3;
962    TiXmlHandle hRoot(0);
963    //printf("ServiceManagementLayer:: Loading Configuration.\n");
964    TiXmlDocument doc(".");
965    doc.LoadFile(SML_Config);
966    bool loadOkay = doc.LoadFile();
967    if(!loadOkay)
968        printf("Loading SML configuration failed: %s\n", SML_Config);
969
970    TiXmlHandle hDoc(&doc);
971   
972    pMission = hDoc.FirstChildElement().Element();
973
974    if(!pMission)
975        printf("No valid root!");
976
977    hRoot = TiXmlHandle(pMission);
978    pService = pMission->FirstChildElement();
979    int32_t mission_num = 0;
980    //Iterate through the missions
981    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \
982        pChild0 = pChild0->NextSiblingElement())
983    {
984        int32_t service_num = 0;
985        uint16_t cond_array[] = {0, 0};
986        //printf("mission_num = %d\n", mission_num);
987        memset(cond_array, 0, 2);
988       
989        for(pChild1  = pChild0->FirstChildElement(); pChild1; \
990            pChild1  = pChild1->NextSiblingElement())
991        {
992            int32_t conditional_0 = service_num;
993            for(pChild2 = pChild1->FirstChildElement(); \
994                pChild2; pChild2 = pChild2->NextSiblingElement())
995            {
996                service_num++;
997                int32_t conditional_1 = service_num;
998                for(pChild3 = pChild2->FirstChildElement(); \
999                    pChild3; pChild3 = pChild3 ->NextSiblingElement())
1000                {
1001                    service_num++;
1002                    mList[mission_num].services[service_num].name = pChild3->Attribute("name");
1003                    if(pChild3->Attribute("input1"))
1004                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1");
1005                    if(pChild3->Attribute("input2"))
1006                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2");
1007                    if(pChild3->Attribute("input3"))
1008                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3");
1009                    if(pChild3->Attribute("output1"))
1010                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1");
1011                    if(pChild3->Attribute("output2"))
1012                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2");
1013                    if(pChild3->Attribute("output3"))
1014                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3");
1015                    cond_array[1]++;
1016                }
1017
1018                if(conditional_1 != service_num){
1019                    mList[mission_num].services[conditional_1].name = pChild2->Value();
1020                    if(pChild2->Attribute("input_t"))
1021                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t");
1022                    if(pChild2->Attribute("input_f"))
1023                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f");
1024                }
1025                else{
1026                    mList[mission_num].services[conditional_1].name = pChild2->Attribute("name");
1027                    if(pChild2->Attribute("input1"))
1028                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1");
1029                    if(pChild2->Attribute("input2"))
1030                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2");
1031                    if(pChild2->Attribute("input3"))
1032                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3");
1033                    if(pChild2->Attribute("output1"))
1034                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1");
1035                    if(pChild2->Attribute("output2"))
1036                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2");
1037                    if(pChild2->Attribute("output3"))
1038                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3");
1039                }
1040
1041                mList[mission_num].services[conditional_1].num_conds = cond_array[1];
1042printf("hello\n");
1043                cond_array[1] = 0;
1044                cond_array[0]++;
1045            }
1046            if(conditional_0 != service_num){
1047                mList[mission_num].services[conditional_0].name = pChild1->Value();
1048                    if(pChild1->Attribute("input_t"))
1049                        mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t");
1050                    if(pChild1->Attribute("input_f"))
1051                        mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f");
1052                printf("---input_t= %s\n", mList[mission_num].services[service_num].input[0].c_str());
1053            }
1054            else{
1055                mList[mission_num].services[conditional_0].name = pChild1->Attribute("name");
1056                if(pChild1->Attribute("input1"))
1057                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1");
1058                if(pChild1->Attribute("input2"))
1059                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2");
1060                if(pChild1->Attribute("input3"))
1061                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3");
1062                if(pChild1->Attribute("output1"))
1063                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1");
1064                if(pChild1->Attribute("output2"))
1065                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2");
1066                if(pChild1->Attribute("output3"))
1067                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3");
1068            }
1069
1070            mList[mission_num].services[conditional_0].num_conds = cond_array[0];
1071            //printf("hello\n");
1072            cond_array[0] = 0;
1073            service_num++;
1074        }
1075        for(int i = 0; i < service_num; i++)
1076                printf("%d, input1=%s, output1=%s\n", i, mList[mission_num].services[i].input[0].c_str(), mList[mission_num].services[i].output[0].c_str());
1077
1078        mList[mission_num].numServices = service_num;
1079        mList[mission_num].name = pChild0->Attribute("name");
1080        mList[mission_num].missionID = atoi(pChild0->Attribute("id"));
1081        if(pChild0->Attribute("input1"))
1082            mList[mission_num].input[0] = pChild0->Attribute("input1");
1083        if(pChild0->Attribute("input2"))
1084            mList[mission_num].input[1] = pChild0->Attribute("input2");
1085        if(pChild0->Attribute("input3"))
1086            mList[mission_num].input[2] = pChild0->Attribute("input3");
1087        if(pChild0->Attribute("output1"))
1088            mList[mission_num].output[0] = pChild0->Attribute("output1");
1089        if(pChild0->Attribute("output2"))
1090            mList[mission_num].output[1] = pChild0->Attribute("output2");
1091        if(pChild0->Attribute("output3"))
1092            mList[mission_num].output[2] = pChild0->Attribute("output3");
1093        printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str());
1094        mission_num++;
1095    }
1096    /*TiXmlElement *pMission;
1097    TiXmlElement *pService;
1098    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3;
1099    TiXmlHandle hRoot(0);
1100    //printf("ServiceManagementLayer:: Loading Configuration.\n");
1101    TiXmlDocument doc(".");
1102    doc.LoadFile(SML_Config);
1103    bool loadOkay = doc.LoadFile();
1104    if(!loadOkay)
1105        printf("Loading SML configuration failed: %s\n", SML_Config);
1106
1107    TiXmlHandle hDoc(&doc);
1108   
1109    pMission = hDoc.FirstChildElement().Element();
1110
1111    if(!pMission)
1112        printf("No valid root!");
1113
1114    hRoot = TiXmlHandle(pMission);
1115    pService = pMission->FirstChildElement();
1116    int32_t mission_num = 0;
1117    //Iterate through the missions
1118    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \
1119        pChild0 = pChild0->NextSiblingElement())
1120    {
1121        int32_t service_num = 0;
1122        uint16_t cond_array[] = {0, 0};
1123        //printf("mission_num = %d\n", mission_num);
1124        memset(cond_array, 0, 2);
1125       
1126        for(pChild1  = pChild0->FirstChildElement(); pChild1; \
1127            pChild1  = pChild1->NextSiblingElement())
1128        {
1129            int32_t conditional_0 = service_num;
1130            for(pChild2 = pChild1->FirstChildElement(); \
1131                pChild2; pChild2 = pChild2->NextSiblingElement())
1132            {
1133                service_num++;
1134                int32_t conditional_1 = service_num;
1135                for(pChild3 = pChild2->FirstChildElement(); \
1136                    pChild3; pChild3 = pChild3 ->NextSiblingElement())
1137                {
1138                    service_num++;
1139                    mList[mission_num].services[service_num].name = pChild3->Attribute("name");
1140                    if(pChild3->Attribute("input1"))
1141                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1");
1142                    if(pChild3->Attribute("input2"))
1143                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2");
1144                    if(pChild3->Attribute("input3"))
1145                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3");
1146                    if(pChild3->Attribute("output1"))
1147                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1");
1148                    if(pChild3->Attribute("output2"))
1149                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2");
1150                    if(pChild3->Attribute("output3"))
1151                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3");
1152                    cond_array[1]++;
1153                }
1154
1155                if(conditional_1 != service_num){
1156                    mList[mission_num].services[conditional_1].name = pChild2->Value();
1157                    if(pChild2->Attribute("input_t"))
1158                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t");
1159                    if(pChild2->Attribute("input_f"))
1160                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f");
1161                }
1162                else{
1163                    mList[mission_num].services[service_num].name = pChild2->Attribute("name");
1164                    if(pChild2->Attribute("input1"))
1165                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1");
1166                    if(pChild2->Attribute("input2"))
1167                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2");
1168                    if(pChild2->Attribute("input3"))
1169                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3");
1170                    if(pChild2->Attribute("output1"))
1171                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1");
1172                    if(pChild2->Attribute("output2"))
1173                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2");
1174                    if(pChild2->Attribute("output3"))
1175                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3");
1176                }
1177
1178                mList[mission_num].services[conditional_1].num_conds = cond_array[1];
1179                cond_array[1] = 0;
1180                cond_array[0]++;
1181            }
1182            if(conditional_0 != service_num){
1183                mList[mission_num].services[conditional_0].name = pChild1->Value();
1184                if(pChild1->Attribute("input_t"))
1185                    mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t");
1186                if(pChild1->Attribute("input_f"))
1187                    mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f");
1188                printf("if detected %s, %s\n", pChild1->Attribute("input_f"), pChild1->Attribute("input_t"));
1189            }
1190            else{
1191                mList[mission_num].services[service_num].name = pChild1->Attribute("name");
1192                if(pChild1->Attribute("input1"))
1193                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1");
1194                if(pChild1->Attribute("input2"))
1195                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2");
1196                if(pChild1->Attribute("input3"))
1197                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3");
1198                if(pChild1->Attribute("output1"))
1199                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1");
1200                if(pChild1->Attribute("output2"))
1201                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2");
1202                if(pChild1->Attribute("output3"))
1203                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3");
1204            }
1205
1206            mList[mission_num].services[conditional_0].num_conds = cond_array[0];
1207            //printf("hello\n");
1208           
1209            cond_array[0] = 0;
1210            service_num++;
1211        }
1212        for(int i = 0; i < service_num; i++)
1213                printf("input1=%s, output1=%s\n", mList[mission_num].services[i].input[0].c_str(), mList[mission_num].services[service_num].output[0].c_str());
1214
1215        mList[mission_num].numServices = service_num;
1216        mList[mission_num].name = pChild0->Attribute("name");
1217        mList[mission_num].missionID = atoi(pChild0->Attribute("id"));
1218        if(pChild0->Attribute("input1"))
1219            mList[mission_num].input[0] = pChild0->Attribute("input1");
1220        if(pChild0->Attribute("input2"))
1221            mList[mission_num].input[1] = pChild0->Attribute("input2");
1222        if(pChild0->Attribute("input3"))
1223            mList[mission_num].input[2] = pChild0->Attribute("input3");
1224        if(pChild0->Attribute("output1"))
1225            mList[mission_num].output[0] = pChild0->Attribute("output1");
1226        if(pChild0->Attribute("output2"))
1227            mList[mission_num].output[1] = pChild0->Attribute("output2");
1228        if(pChild0->Attribute("output3"))
1229            mList[mission_num].output[2] = pChild0->Attribute("output3");
1230        printf("input1=%s, outpu1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str());
1231        mission_num++;
1232    }*/
1233}
1234
1235//Verified w/o Transfers May 26
1236void
1237ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID)
1238{
1239    SendMessage(shellSocketFD, "register_engine_cognitive");
1240    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n");
1241
1242    //printf("ServiceManagementLayer:: CE registration message forwarded to shell.\n");
1243
1244    //TransferRadioConfiguration(ID);
1245    //TransferExperience(ID);
1246
1247    numberOfCognitiveEngines++;
1248    CE_Present = true;
1249}
1250
1251void
1252ServiceManagementLayer::DeregisterServices(int32_t ID)
1253{
1254    char str_buffer[64];
1255    strcpy(_services_DB->command, "delete ");
1256    strcat(_services_DB->command, _services_DB->tablename);
1257    strcat(_services_DB->command, ".* from ");
1258    strcat(_services_DB->command, _services_DB->tablename);
1259    strcat(_services_DB->command, " where ");
1260    strcat(_services_DB->command, "ID_Num");
1261    strcat(_services_DB->command, "==");
1262    sprintf(str_buffer, "%d;", ID);
1263    strcat(_services_DB->command, str_buffer);
1264    sqlite3_exec(_services_DB->db, _services_DB->command, NULL, 0, NULL);
1265}
1266
1267
1268void
1269ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID)
1270{
1271    LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n");
1272
1273    numberOfCognitiveEngines--;
1274    if(numberOfCognitiveEngines == 0)
1275        CE_Present = false;
1276
1277    SendMessage(shellSocketFD, "deregister_engine_cognitive");
1278    char buffer[256];
1279    memset(buffer, 0, 256);
1280    ReadMessage(shellSocketFD, buffer);
1281    SendMessage(CE_List[ID].FD, buffer);
1282    if(strcmp("deregister_ack", buffer) != 0) {
1283        ERROR(1, "SML:: Failed to close CE socket\n");
1284    }
1285    CE_List[ID].FD = -1;
1286    CE_List[ID].ID_num = -1;
1287
1288    LOG("Cognitive Radio Shell:: Socket closed.\n");
1289}
1290
1291
1292//VERIFIED May 21
1293void
1294ServiceManagementLayer::StartSMLServer()
1295{
1296    struct timeval selTimeout;
1297    int32_t running = 1;
1298    int32_t port, rc, new_sd = 1;
1299    int32_t desc_ready = 1;
1300    int32_t timeout = 10;
1301                //If there is, call the MessageHandler with the Shell_Msg code of -1
1302    fd_set sockSet, shellSet;
1303
1304    cogEngSrv = CreateTCPServerSocket(2041);
1305    int32_t maxDescriptor = cogEngSrv;
1306
1307    if(InitializeTCPServerPort(cogEngSrv) == -1)
1308        ERROR(1,"Error initializing primary port\n");
1309
1310
1311    /*int FD = ClientSocket("127.0.0.1", "2013");
1312    SendMessage(FD, "register_engine_cognitive");   
1313    SendMessage(FD, "register_service");
1314    SendMessage(FD, "test_srv");
1315    SendMessage(FD, "list_services");
1316    int FD2 = ClientSocket("127.0.0.1", "2013");
1317    SendMessage(FD2, "register_engine_cognitive");   
1318    SendMessage(FD2, "register_service");
1319    SendMessage(FD2, "test_srv1");
1320    SendMessage(FD2, "list_services");*/
1321     //SendMessage(shellSocketFD, "list_services");*/
1322    //printf("num = %d, %d, %d\n", one, two, three);
1323   
1324
1325    int i = 10000000;
1326    while (i>0) {
1327        i--;
1328        /* Zero socket descriptor vector and set for server sockets */
1329        /* This must be reset every time select() is called */
1330        FD_ZERO(&sockSet);
1331        FD_SET(cogEngSrv, &sockSet);
1332        for(int k = 0; k < Current_ID; k++)
1333            FD_SET(CE_List[k].FD, &sockSet);
1334            //printf("k=%d, CID=%d\n", k, CE_List[k].FD);
1335
1336        /* Timeout specification */
1337        /* This must be reset every time select() is called */
1338        selTimeout.tv_sec = 0;       /* timeout (secs.) */
1339        selTimeout.tv_usec = 0;            /* 0 microseconds */
1340        //Changed both to zero so that select will check messages from the shell instead of blocking
1341        //when there is no command from the CE's to be processed
1342
1343        //Check if there is a message on the socket waiting to be read
1344        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
1345        //printf("rc=%d\n", rc);
1346        if(rc == 0){
1347            //LOG("No echo requests for %i secs...Server still alive\n", timeout);
1348       
1349            FD_ZERO(&shellSet);
1350            FD_SET(shellSocketFD, &shellSet);
1351            selTimeout.tv_sec = 0;
1352            selTimeout.tv_usec = 0;
1353            //Check if there is a message on the shell socket ready to be processed
1354            int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout);
1355            //printf("rc2=%d\n", rc2);
1356                //If there is, call the MessageHandler with the Shell_Msg code of -1
1357            if(FD_ISSET(shellSocketFD, &shellSet)){
1358                //printf("shell_msg, %d\n", rc2);
1359                MessageHandler(-1);}
1360        }
1361        else {
1362            desc_ready = rc;
1363            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
1364                if(FD_ISSET(port, &sockSet)) {
1365                    desc_ready -= 1;
1366
1367                    //Check if request is new or on an existing open descriptor
1368                    if(port == cogEngSrv) {
1369        //printf("here1\n");
1370                       // do {
1371                            new_sd = AcceptTCPConnection(port);
1372                         
1373                            //printf("sd = %d\n", new_sd);
1374                            if(new_sd < 0)
1375                                break;
1376                            //Set not to block
1377                            /*int oldflags = fcntl (new_sd, F_GETFL, 0);
1378                            oldflags |= O_NONBLOCK;
1379                            fcntl (new_sd, F_SETFL, oldflags);*/
1380
1381                           
1382                            CE_List[Current_ID].FD = new_sd;
1383                            CE_List[Current_ID].ID_num = Current_ID;
1384                            MessageHandler(Current_ID);
1385                            Current_ID++;
1386       
1387                            FD_SET(new_sd,&sockSet);
1388                            if(new_sd > maxDescriptor)
1389                                maxDescriptor = new_sd;
1390                            //LOG("New incoming connection - %i\n\n",new_sd);
1391                       // } while(new_sd != -1);
1392                    }
1393                    else {
1394                        //printf("here2\n");
1395                        //LOG("Request on already open descriptor.\n\n");
1396                        for(int16_t z = 0; z < Current_ID; z++)
1397                        {
1398                                if(CE_List[z].FD == port){
1399                                        //printf("z=%d\n", z);
1400                                        MessageHandler(z);}
1401                        }
1402                    }
1403                }
1404            }
1405        }
1406    }       
1407
1408    /* Close sockets */
1409    close(cogEngSrv);
1410
1411    //delete &cogEngSrv;
1412    return;
1413}
Note: See TracBrowser for help on using the browser.