src/SocketHandler.h
branchv_0
changeset 8 3e17086fffea
parent 5 e57e2a2798b2
equal deleted inserted replaced
7:e6f005f3edfe 8:3e17086fffea
    21 #include <vector>
    21 #include <vector>
    22 #include <iostream>
    22 #include <iostream>
    23 #include <sstream>
    23 #include <sstream>
    24 #include <locale>
    24 #include <locale>
    25 #include <codecvt>
    25 #include <codecvt>
       
    26 #include <regex>
    26 
    27 
    27 #include <relpipe/common/type/typedefs.h>
    28 #include <relpipe/common/type/typedefs.h>
    28 #include <relpipe/reader/TypeId.h>
    29 #include <relpipe/reader/TypeId.h>
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
    31 #include <relpipe/reader/handlers/AttributeMetadata.h>
    40 class SocketHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    41 class SocketHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    41 private:
    42 private:
    42 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    43 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    43 	Configuration configuration;
    44 	Configuration configuration;
    44 	std::shared_ptr<Socket> socket;
    45 	std::shared_ptr<Socket> socket;
       
    46 	std::wregex relationPattern;
    45 
    47 
    46 	struct CurrentRelation {
    48 	struct CurrentRelation {
    47 		relpipe::common::type::StringX name;
    49 		relpipe::common::type::StringX name;
    48 		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
    50 		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
       
    51 		bool valid;
    49 		relpipe::common::type::Integer attributeIndex = 0;
    52 		relpipe::common::type::Integer attributeIndex = 0;
    50 		std::string currentValue;
    53 		std::string currentValue;
    51 	} currentRelation;
    54 	} currentRelation;
    52 
    55 
    53 	void configureSocket() {
    56 	void configureSocket() {
    60 		}
    63 		}
    61 	}
    64 	}
    62 
    65 
    63 public:
    66 public:
    64 
    67 
    65 	SocketHandler(Configuration configuration) : configuration(configuration) {
    68 	SocketHandler(Configuration configuration) : configuration(configuration), relationPattern(std::wregex(configuration.relation)) {
    66 	}
    69 	}
    67 
    70 
    68 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    71 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    69 		configureSocket();
    72 		currentRelation = CurrentRelation{name, attributes, std::regex_match(name, relationPattern)};
    70 		currentRelation = CurrentRelation{name, attributes};
    73 		if (currentRelation.valid) configureSocket();
    71 	}
    74 	}
    72 
    75 
    73 	void attribute(const relpipe::common::type::StringX& value) override {
    76 	void attribute(const relpipe::common::type::StringX& value) override {
       
    77 		if (currentRelation.valid) {
       
    78 			auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
       
    79 			if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value);
       
    80 			else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str();
       
    81 			else if (attributeName == L"text"); // keep empty or value from 'data'
       
    82 			else if (attributeName == L"data"); // keep empty or value from 'text'
    74 
    83 
    75 		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
    84 			currentRelation.attributeIndex++;
    76 		if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value);
    85 			if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
    77 		else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str();
    86 				currentRelation.attributeIndex = 0;
    78 		else if (attributeName == L"text"); // keep empty or value from 'data'
    87 				socket->send(currentRelation.currentValue);
    79 		else if (attributeName == L"data"); // keep empty or value from 'text'
    88 			}
    80 
       
    81 		currentRelation.attributeIndex++;
       
    82 		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
       
    83 			currentRelation.attributeIndex = 0;
       
    84 			socket->send(currentRelation.currentValue);
       
    85 		}
    89 		}
    86 
       
    87 	}
    90 	}
    88 
    91 
    89 	void endOfPipe() {
    92 	void endOfPipe() {
    90 
    93 
    91 	}
    94 	}