Revision 54 logic/trunk/src/hci_comm/hci_comm_listener.c

View differences:

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

  
25 26
	// Buffer
26 27
	char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char));
......
73 74
		int connectionHolder = accept(socketHolder,NULL,NULL);
74 75
		if(connectionHolder < 0) {
75 76
			printf("HCI comm error: Error accepting request.\n");
76
			close(socketHolder);
77
			return HCI_COMM_ERR_ACCEPT_FAIL;
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;
78 83
		}
79 84

  
80 85
		printf("[hci_comm_listener] New connection accepted\n");
......
84 89
		printf("[hci_comm_listener] Recieved request: %s \n",requestBuffer);
85 90
		if(readResult < 0) {
86 91
			printf("HCI comm error: Error reading request.\n");
87
			close(socketHolder);
88
			return HCI_COMM_ERR_READ_FAIL;
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;
89 98
		}
90 99

  
91 100
        // Parse request
......
93 102
        tree = mxmlLoadString(NULL, requestBuffer, MXML_NO_CALLBACK);
94 103
        if(tree == NULL) {
95 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;
96 111
        }
97 112
        printf("[hci_comm_listener] Request parsed\n");
98 113

  
99 114
		// Create a response
100 115
		printf("[hci_comm_listener] Creating response\n");
101
		int responseLength = 0;
102 116
		mxml_node_t *node;
103 117
        node = mxmlFindElement(tree, tree, "hci-request", NULL, NULL, MXML_DESCEND);
104 118
        if(node == NULL) {
105
            printf("[hci_comm_listener] Error: Recieved node is NULL!\n");
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;
106 126
        }
107 127
        printf("[hci_comm_listener] Main request node found\n");
108 128
        printf("[hci_comm_listener] %d\n",node);
......
146 166
		printf("[hci_comm_listener] Written response to client\n");
147 167
		if(writeResult < 0) {
148 168
			printf("HCI comm error: Error writting response.\n");
149
			close(socketHolder);
150
			return HCI_COMM_ERR_WRITE_FAIL;
151 169
		}
152 170

  
153 171
		// Close connection

Also available in: Unified diff