Statistics
| Revision:

root / logic / trunk / src / hci_comm / hci_comm_listener.c @ 54

History | View | Annotate | Download (6.44 KB)

1
/*
2
 * hci_comm_listener.c
3
 *
4
 * 2011, Aleksander Besir (alex.besir@gmail.com)
5
 *
6
 */
7

    
8
#include "hci_comm.h"
9
#include <sys/types.h>
10
#include <sys/socket.h>
11
#include <netinet/in.h>
12
#include <arpa/inet.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include <unistd.h>
17
#include "../mxml/mxml.h"
18

    
19
int listenTo(unsigned short int portNum) {
20

    
21
    // Holders
22
    char *attrVal;
23
    char *name;
24
    int responseLength;
25

    
26
        // Buffer
27
        char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char));
28
        if(requestBuffer == NULL) {
29
                printf("HCI comm error: Cannot allocate buffer.\n");
30
                return HCI_COMM_ERR_BUF_ALOC_FAIL;
31
        }
32
        char *responseBuffer = malloc(HCI_COMM_RESPONSE_BUFFER_SIZE * sizeof(char));
33
        if(responseBuffer == NULL) {
34
                printf("HCI comm error: Cannot allocate buffer.\n");
35
                return HCI_COMM_ERR_BUF_ALOC_FAIL;
36
        }
37

    
38
        // Socket holder
39
        int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
40
        if(socketHolder < 0) {
41
                fprintf(stderr,"HCI comm error: Cannot create a new socket.\n");
42
                return HCI_COMM_ERR_NEW_SOCKET_FAIL;
43
        }
44

    
45
        // Socket address holder
46
        struct sockaddr_in socketAddressHolder;
47
        memset(&socketAddressHolder, 0, sizeof(socketAddressHolder));
48
        socketAddressHolder.sin_family      = AF_INET;
49
            socketAddressHolder.sin_port        = htons(portNum);
50
            socketAddressHolder.sin_addr.s_addr = INADDR_ANY;
51

    
52
        // Bind socket to port
53
        int bindingResult = bind(socketHolder,(struct sockaddr *)&socketAddressHolder, sizeof(socketAddressHolder));
54
        if(bindingResult == -1) {
55
                fprintf(stderr,"HCI comm error: Cannot bind socket to port %d.\n",portNum);
56
                close(socketHolder);
57
                return HCI_COMM_ERR_BIND_FAIL;
58
        }
59

    
60
        // Listen to port
61
        int listenResult = listen(socketHolder, 10);
62
        if(listenResult == -1) {
63
                printf("HCI comm error: Cannot listen to port %d.\n",portNum);
64
                close(socketHolder);
65
                return HCI_COMM_ERR_LISTEN_FAIL;
66
        }
67

    
68
        // Infinite listening
69
        while(1) {
70

    
71
            printf("[hci_comm_listener] Waiting for new connection...\n");
72

    
73
                // Accept request
74
                int connectionHolder = accept(socketHolder,NULL,NULL);
75
                if(connectionHolder < 0) {
76
                        printf("HCI comm error: Error accepting request.\n");
77
            strcpy(responseBuffer,"<logic-response type=\"error\">Could not accept request!</logic-response>");
78
            responseLength = strlen(responseBuffer);
79
            write(connectionHolder,responseBuffer,responseLength);
80
            shutdown(connectionHolder,SHUT_RDWR);
81
            close(connectionHolder);
82
            continue;
83
                }
84

    
85
                printf("[hci_comm_listener] New connection accepted\n");
86

    
87
                // Read request
88
                int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE);
89
                printf("[hci_comm_listener] Recieved request: %s \n",requestBuffer);
90
                if(readResult < 0) {
91
                        printf("HCI comm error: Error reading request.\n");
92
            strcpy(responseBuffer,"<logic-response type=\"error\">Could not read request!</logic-response>");
93
            responseLength = strlen(responseBuffer);
94
            write(connectionHolder,responseBuffer,responseLength);
95
            shutdown(connectionHolder,SHUT_RDWR);
96
            close(connectionHolder);
97
            continue;
98
                }
99

    
100
        // Parse request
101
        mxml_node_t *tree;
102
        tree = mxmlLoadString(NULL, requestBuffer, MXML_NO_CALLBACK);
103
        if(tree == NULL) {
104
            printf("[hci_comm_listener] Error parsing request!\n");
105
            strcpy(responseBuffer,"<logic-response type=\"error\">Could not parse request!</logic-response>");
106
            responseLength = strlen(responseBuffer);
107
            write(connectionHolder,responseBuffer,responseLength);
108
            shutdown(connectionHolder,SHUT_RDWR);
109
            close(connectionHolder);
110
            continue;
111
        }
112
        printf("[hci_comm_listener] Request parsed\n");
113

    
114
                // Create a response
115
                printf("[hci_comm_listener] Creating response\n");
116
                mxml_node_t *node;
117
        node = mxmlFindElement(tree, tree, "hci-request", NULL, NULL, MXML_DESCEND);
118
        if(node == NULL) {
119
            printf("[hci_comm_listener] Request invalid! Sending error to client\n");
120
            strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>");
121
            responseLength = strlen(responseBuffer);
122
            write(connectionHolder,responseBuffer,responseLength);
123
            shutdown(connectionHolder,SHUT_RDWR);
124
            close(connectionHolder);
125
            continue;
126
        }
127
        printf("[hci_comm_listener] Main request node found\n");
128
        printf("[hci_comm_listener] %d\n",node);
129

    
130
        attrVal = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "type")));
131
        attrVal = mxmlElementGetAttr(node,"type");
132
        printf("[hci_comm_listener] Got type\n");
133
        if(strcmp(attrVal,"checkConnection") == 0) {
134
            printf("[hci_comm_listener] checkConnection request\n");
135
            strcpy(responseBuffer,"");
136
            strcat(responseBuffer,"<logic-response type=\"checkConnection\"><data>");
137
            mxml_node_t *child = node->child;
138
            child = child->child;
139
            strcat(responseBuffer,child->value.text.string);
140
            strcat(responseBuffer,"</data></logic-response>");
141
            printf("[hci_comm_listener] Response formed: %s\n",responseBuffer);
142
            responseLength = strlen(responseBuffer);
143
            printf("[hci_comm_listener] Response length formed: %d\n",responseLength);
144
        } else if(strcmp(attrVal,"getConfig") == 0) {
145
            // TODO
146
        } else if(strcmp(attrVal,"setConfig") == 0) {
147
            // TODO
148
        } else if(strcmp(attrVal,"getScenarios") == 0) {
149
            // TODO
150
        } else if(strcmp(attrVal,"setScenarios") == 0) {
151
            // TODO
152
        } else if(strcmp(attrVal,"executeAction") == 0) {
153
            // TODO
154
        } else {
155
            strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>");
156
            responseLength = 62;
157
        }
158

    
159
                // Check if logic wants to terminate the infinite loop
160
                if(responseLength == HCI_COMM_TERMINATE) {
161
                        break;
162
                }
163

    
164
                // Send response
165
                int writeResult = write(connectionHolder,responseBuffer,responseLength);
166
                printf("[hci_comm_listener] Written response to client\n");
167
                if(writeResult < 0) {
168
                        printf("HCI comm error: Error writting response.\n");
169
                }
170

    
171
                // Close connection
172
                free(attrVal);
173
                shutdown(connectionHolder,SHUT_RDWR);
174
                close(connectionHolder);
175

    
176
                printf("[hci_comm_listener] Connection closed\n");
177

    
178
        }
179

    
180
        // Terminate
181
        close(socketHolder);
182
        free(requestBuffer);
183
        requestBuffer = NULL;
184
        return HCI_COMM_TERMINATED_BY_LOGIC;
185

    
186
}