diff options
author | deva <deva> | 2010-06-01 12:58:32 +0000 |
---|---|---|
committer | deva <deva> | 2010-06-01 12:58:32 +0000 |
commit | 74a28aa7125be6a603128ad600c98c4882f3b5c2 (patch) | |
tree | 1a9e4ab74f29d5ff10f2701e4e112f4525c0dcec /server/src/queryhandlerpentominos.cc | |
parent | 9b9c1e2dd3e5807ff7714b378b03b9ba31f42df7 (diff) |
From new_protocol branch.
Diffstat (limited to 'server/src/queryhandlerpentominos.cc')
-rw-r--r-- | server/src/queryhandlerpentominos.cc | 262 |
1 files changed, 200 insertions, 62 deletions
diff --git a/server/src/queryhandlerpentominos.cc b/server/src/queryhandlerpentominos.cc index c5450b3..b67f731 100644 --- a/server/src/queryhandlerpentominos.cc +++ b/server/src/queryhandlerpentominos.cc @@ -28,103 +28,241 @@ #include "debug.h" -#include "configuration.h" +#include <config.h> -QueryHandlerPentominos::QueryHandlerPentominos(std::string cpr) - : QueryHandler() +// For time +#include <time.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// For getpid +#include <unistd.h> +#include <sys/types.h> + +// For time +#include <time.h> + +// For strerror and errno +#include <errno.h> + +// For socket and friends +#include <sys/socket.h> +#include <arpa/inet.h> +#include <net/if.h> +#include <netinet/in.h> + +// For ioctl +#include <sys/ioctl.h> + +#include "queryparser.h" + +typedef struct { + in_addr_t ip; + time_t time; + pid_t pid; + unsigned short int count; +} UID; + +#define SIOCGIFCONF 0x8912 // get iface list +/* +static in_addr_t getIP(const char *interface) { - this->cpr = cpr; + in_addr_t ret = 0; + int numreqs = 30, sd, n; + struct ifconf ifc; + struct ifreq *ifr; + struct in_addr *ia; - atfh = atf_init(); + sd = socket(AF_INET, SOCK_STREAM, 0); + if(sd == -1) { + // throw Pentominos::UIDCouldNotConnectException(strerror(errno)); + } + + ifc.ifc_buf = NULL; + ifc.ifc_len = sizeof(struct ifreq) * numreqs; + + ifc.ifc_buf = (char*)malloc(ifc.ifc_len); + if(ifc.ifc_buf == NULL) { + // throw Pentominos::UIDOutOfMemoryException(); + } + + if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) { + // throw Pentominos::UIDInterfaceListException(strerror(errno)); + } - bool use_https = false; - atfc = atf_connect(atfh, Conf::pentominos_addr.c_str(), - Conf::pentominos_port, use_https); + ifr = ifc.ifc_req; + for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { + ia = (struct in_addr *)((ifr->ifr_ifru.ifru_addr.sa_data)+2); + if(!strcmp(ifr->ifr_ifrn.ifrn_name, interface)) { + ret = *(in_addr_t*)ia; + } + ifr++; + } + if(!ret) { // Still no interface... We're fucked! + // throw Pentominos::UIDInterfaceException(interface); + } + + free(ifc.ifc_buf); + return ret; } -QueryHandlerPentominos::~QueryHandlerPentominos() + +static unsigned short counter = 0; +static unsigned short getCounter() { - atf_disconnect(atfc); - atf_close(atfh); + return counter++; } -static QueryResult node2result(atf_result_node_t *node, time_t timestamp) + +static UID uid = {0,0,0,0}; +static std::string getUID(const char *interface) { - QueryResult rnode; - rnode.timestamp = timestamp; - rnode.source = "pentominos"; - - if(!node) return rnode; - - struct _atf_result_node_t *child = node->child; - while(child) { - if(child->value == NULL) { - rnode.groups[child->name] = node2result(child, timestamp); - } else { - rnode.values[child->name] = child->value; - } - child = child->next; - } + if(!uid.ip) uid.ip = getIP(interface); + + time_t t = time(NULL); + if(uid.time != t) counter = 0; // If time differes, reset the counter + uid.time = t; // We need this value every time. + + if(!uid.pid) uid.pid = getpid(); - return rnode; + uid.count = getCounter(); + + char buf[32]; + sprintf(buf, "%08x%08x%04x%04x", uid.ip, (unsigned int)uid.time, uid.pid, uid.count); + return std::string(buf); +} +*/ + +QueryHandlerPentominos::QueryHandlerPentominos(Artefact &atf, + std::string patientid, + std::string user) + : QueryHandler(), artefact(atf) +{ + this->patientid = patientid; + this->user = user; } QueryResult QueryHandlerPentominos::exec(Query &query) { - atf_transaction_t* atft = NULL; - atf_reply_t *reply = NULL; - atf_result_t *result = NULL; - atf_result_node_t *root = NULL; - atf_status_t status; - time_t timestamp; - atf_id id; + return artefact.exec(query, patientid, user); + +#if 0 + time_t timestamp = time(NULL); + std::string uid = getUID("eth0"); - QueryResult rroot; - rroot.timestamp = timestamp; - rroot.source = "pentominos"; + char buf[512]; + char header[] = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<artefact xmlns=\"http://www.aasimon.org/pentominos\"\n" + " xmlns:pentominos=\"http://www.aasimon.org/pentominos\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http://www.aasimon.org/pentominos schema.xsd\">\n"; +#ifndef WITHOUT_PENTOMINOS + artefact.socket.write(header, strlen(header)); +#endif/*WITHOUT_PENTOMINOS*/ - atft = atf_new_transaction(atfc, cpr.c_str()); - if(!atft) goto aaarg; + PRACRO_DEBUG(queryhandler, "%s", header); - id = atf_add_query(atft, query.attributes["class"].c_str(), - FILTER_LATEST, USE_NONE, 0, 0); - if(!atft) goto aaarg; + sprintf(buf, " <pentominos:entry replyformat=\"xml\"\n" + " cpr=\"%s\"\n" + " operator=\"%s\"\n" + " src_addr=\"%s\"\n" + " dst_addr=\"%s\"\n" + " timestamp=\"%d\"\n" + " uid=\"%s\"/>\n", + cpr.c_str(), + "pracro", +#ifndef WITHOUT_PENTOMINOS + artefact.socket.srcaddr().c_str(), + artefact.socket.dstaddr().c_str(), +#else + "127.0.0.1", + "127.0.0.1", +#endif/*WITHOUT_PENTOMINOS*/ + (unsigned int)timestamp, + uid.c_str()); +#ifndef WITHOUT_PENTOMINOS + artefact.socket.write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ - reply = atf_commit(atft); - if(!reply) goto aaarg; + PRACRO_DEBUG(queryhandler, "%s", buf); - if(atf_get_num_results(reply, id) != 1) goto aaarg; + sprintf(buf, " <pentominos:query device_id=\"%s\"\n" + " device_type=\"%s\"\n" + " filter=\"latest\"\n" + " location=\"all\"/>\n", + query.attributes["class"].c_str(), + query.attributes["class"].c_str()); + +#ifndef WITHOUT_PENTOMINOS + artefact.socket.write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ - result = atf_get_result(reply, id, 0); - if(!result) goto aaarg; + PRACRO_DEBUG(queryhandler, "%s", buf); - status = atf_get_result_status(result, NULL, 0); - if(status != ATF_STATUS_OK) goto aaarg; - - timestamp = atf_get_result_timestamp(result); + sprintf(buf, "</artefact>"); - root = atf_get_result_node(result); - if(!root) goto aaarg; +#ifndef WITHOUT_PENTOMINOS + artefact.socket.write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ - { - QueryResult qresult = node2result(root, timestamp); - rroot.groups[query.attributes["class"]] = qresult; + PRACRO_DEBUG(queryhandler, "%s", buf); + + QueryResult result; + +#ifndef WITHOUT_PENTOMINOS + QueryParser parser; + + ssize_t size; + + // Read until we've got the entire result. + while((size = artefact.socket.read(buf, sizeof(buf))) > 0) { + // fwrite(buf, size, 1, stdout); fflush(stdout); + if(parser.parse(buf, size)) break; } - aaarg: - if(root) atf_free_result_node(root); - if(reply) atf_free_reply(reply); - if(atft) atf_free_transaction(atft); + result = parser.result; +#endif/*WITHOUT_PENTOMINOS*/ + + PRACRO_INFO(queryhandler, "Done handling query\n"); + + result.print(); - return rroot; + return result; +#endif + return QueryResult(); } #ifdef TEST_QUERYHANDLERPENTOMINOS int main() { +#ifdef WITHOUT_PENTOMINOS + printf("The project need to be configured for use of Pentominos in order to run this test.\n"); return 1; +#endif/*WITHOUT_PENTOMINOS*/ + + TCPSocket s; + try { + s.connect("localhost", 11108); + } catch(Exception &e) { + printf("ERROR: %s\n", e.what()); + printf("A running instance of the artefact server in needed on localhost, port 11108 in order for this test to run.\n"); + return 1; + } + + QueryHandlerPentominos qh(s, "2003791613"); + + Query q1; + q1.attributes["device_id"] = "lensmeter"; + q1.attributes["device_type"] = "lensmeter"; + QueryResult res = qh.exec(q1); + res.print(); + + return 0; } #endif/*TEST_QUERYHANDLERPENTOMINOS*/ |