Commit 4b2a659a authored by Matteo Quintiliani's avatar Matteo Quintiliani
Browse files

Added functions for Data Access Protocol


git-svn-id: file:///home/quintiliani/svncopy/nmxptool/trunk@24 3cd66e75-5955-46cb-a940-c26e5fc5497d
parent 2bfd9f4d
......@@ -56,6 +56,22 @@ typedef enum {
} NMXP_BUFFER_FLAG;
/*! \brief Body of ConnectRequest message*/
typedef struct {
char username[12];
uint32_t version;
uint32_t connection_time;
uint32_t crc32;
} NMXP_CONNECT_REQUEST;
/*! \brief Body of DataRequest message*/
typedef struct {
uint32_t chan_key;
uint32_t start_time;
uint32_t end_time;
} NMXP_DATA_REQUEST;
/*! \brief Sends the message "Connect" on a socket
*
* \param isock A descriptor referencing the socket.
......@@ -133,5 +149,56 @@ int nmxp_receiveCompressedData(int isock, NMXP_CHAN_LIST *channelList);
*/
int nmxp_receiveDecompressedData(int isock, NMXP_CHAN_LIST *channelList);
/*! \brief Sends the message "ConnectRequest" on a socket
*
* \param isock A descriptor referencing the socket.
* \param naqs_username User name (maximum 11 characters), zero terminated.
* \param naqs_password Password.
* \param connection_time Time that the connection was opened.
*
* \retval SOCKET_OK on success
* \retval SOCKET_ERROR on error
*
*/
int nmxp_sendConnectRequest(int isock, char *naqs_username, char *naqs_password, uint32_t connection_time);
/*! \brief Read connection time from a socket
*
* \param isock A descriptor referencing the socket.
* \param[out] connection_time Time in epoch.
*
* \retval SOCKET_OK on success
* \retval SOCKET_ERROR on error
*
*/
int nmxp_readConnectionTime(int isock, uint32_t *connection_time);
/*! \brief Wait the message "Ready" from a socket
*
* \param isock A descriptor referencing the socket.
*
* \retval SOCKET_OK on success
* \retval SOCKET_ERROR on error
*
*/
int nmxp_waitReady(int isock);
/*! \brief Sends the message "DataRequest" on a socket
*
* \param isock A descriptor referencing the socket.
* \param key Channel key for which data are requested.
* \param start_time Start time of the interval for which data are requested. Epoch time.
* \param end_time End time of the interval for which data are requested. Epoch time.
*
* \retval SOCKET_OK on success
* \retval SOCKET_ERROR on error
*
*/
int nmxp_sendDataRequest(int isock, uint32_t key, uint32_t start_time, uint32_t end_time);
#endif
......@@ -135,3 +135,133 @@ int nmxp_receiveDecompressedData(int isock, NMXP_CHAN_LIST *channelList) {
return ret;
}
int nmxp_sendConnectRequest(int isock, char *naqs_username, char *naqs_password, uint32_t connection_time) {
int ret;
char crc32buf[100];
NMXP_CONNECT_REQUEST connectRequest;
strcpy(connectRequest.username, naqs_username);
connectRequest.version = htonl(0);
connectRequest.connection_time = htonl(connection_time);
if(strlen(naqs_username) == 0 && strlen(naqs_password) == 0 ) {
sprintf(crc32buf, "%d%d", connectRequest.version, connection_time);
} else if(strlen(naqs_username) != 0 && strlen(naqs_password) != 0 ) {
sprintf(crc32buf, "%s%d%d%s", naqs_username, connectRequest.version,
connection_time, naqs_password);
} else if(strlen(naqs_username) != 0 ) {
sprintf(crc32buf, "%s%d%d", naqs_username, connectRequest.version, connection_time);
} else if(strlen(naqs_password) != 0 ) {
sprintf(crc32buf, "%d%d%s", connectRequest.version, connection_time, naqs_password);
}
connectRequest.crc32 = htonl(crc32((unsigned char *) crc32buf, strlen(crc32buf)));
ret = nmxp_sendMessage(isock, NMXP_MSG_CONNECTREQUEST, &connectRequest, sizeof(NMXP_CONNECT_REQUEST));
if(ret == NMXP_SOCKET_OK) {
nmxp_log(0,0, "Send a ConnectRequest crc32buf = (%s), crc32 = %d\n", crc32buf, crc32);
} else {
nmxp_log(1,0, "Send a ConnectRequest.\n");
}
return ret;
}
int nmxp_readConnectionTime(int isock, uint32_t *connection_time) {
int ret;
ret = nmxp_recv_ctrl(isock, connection_time, sizeof(connection_time));
*connection_time = ntohl(*connection_time);
nmxp_log(0,0, "Read connection time from socket %d.\n", *connection_time);
if(ret != NMXP_SOCKET_OK) {
nmxp_log(1,0, "Read connection time from socket.\n");
}
return ret;
}
int nmpx_waitReady(int isock) {
int times = 0;
int rc = NMXP_SOCKET_OK;
unsigned long signature;
unsigned long type = 0;
unsigned long length;
while(rc == NMXP_SOCKET_OK && type != NMXP_MSG_READY) {
rc = nmxp_recv_ctrl(isock, &signature, sizeof(signature));
if(rc != NMXP_SOCKET_OK) return rc;
signature = ntohl(signature);
if(signature == 0) {
nmxp_log(0, 0, "signature is equal to zero. receive again.\n");
rc = nmxp_recv_ctrl(isock, &signature, sizeof(signature));
signature = ntohl(signature);
}
if(signature != NMX_SIGNATURE) {
nmxp_log(1, 0, "signature is not valid. signature = %d\n", signature);
return NMXP_SOCKET_ERROR;
}
rc = nmxp_recv_ctrl(isock, &type, sizeof(type));
if(rc != NMXP_SOCKET_OK) return rc;
type = ntohl(type);
if(type != NMXP_MSG_READY) {
nmxp_log(0, 0, "type is not READY. type = %d\n", type);
rc = nmxp_recv_ctrl(isock, &length, sizeof(length));
if(rc != NMXP_SOCKET_OK) return rc;
length = ntohl(length);
if(length > 0) {
if(length == 4) {
int32_t app;
rc = nmxp_recv_ctrl(isock, &app, length);
if(rc != NMXP_SOCKET_OK) return rc;
app = ntohl(app);
nmxp_log(0, 1, "value = %d\n", app);
} else {
char *buf_app = (char *) malloc(sizeof(char) * length);
rc = nmxp_recv_ctrl(isock, buf_app, length);
if(buf_app) {
free(buf_app);
}
}
}
} else {
rc = nmxp_recv_ctrl(isock, &length, sizeof(length));
if(rc != NMXP_SOCKET_OK) return rc;
length = ntohl(length);
if(length != 0) {
nmxp_log(1, 0, "length is not equal to zero. length = %d\n", length);
return NMXP_SOCKET_ERROR;
}
}
times++;
if(times > 10) {
nmxp_log(1, 0, "waiting_ready_message. times > 10\n");
rc = NMXP_SOCKET_ERROR;
}
}
return rc;
}
int nmxp_sendDataRequest(int isock, uint32_t key, uint32_t start_time, uint32_t end_time) {
int ret;
NMXP_DATA_REQUEST dataRequest;
dataRequest.chan_key = htonl(key);
dataRequest.start_time = htonl(start_time);
dataRequest.end_time = htonl(end_time);
ret = nmxp_sendMessage(isock, NMXP_MSG_DATAREQUEST, &dataRequest, sizeof(dataRequest));
if(ret != NMXP_SOCKET_OK) {
nmxp_log(1,0, "Send a Request message\n");
}
return ret;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment