40 private: |
40 private: |
41 std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
41 std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
42 Configuration configuration; |
42 Configuration configuration; |
43 shared_ptr<PosixMQ> mq; |
43 shared_ptr<PosixMQ> mq; |
44 |
44 |
|
45 struct CurrentRelation { |
|
46 relpipe::common::type::StringX name; |
|
47 std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
48 relpipe::common::type::Integer attributeIndex = 0; |
|
49 std::string currentValue; |
|
50 } currentRelation; |
|
51 |
|
52 std::string formatText(relpipe::common::type::StringX value) { |
|
53 // TODO: check valid UTF-8; if invalid, return only ASCII characters |
|
54 return convertor.to_bytes(value); |
|
55 } |
|
56 |
|
57 std::string formatData(relpipe::common::type::StringX value) { |
|
58 // TODO: decode HEX if type is string; return original octets if type is octet-string (not yet implemented) |
|
59 return convertor.to_bytes(value); |
|
60 } |
|
61 |
45 public: |
62 public: |
46 |
63 |
47 PosixMQHandler(Configuration configuration) : configuration(configuration) { |
64 PosixMQHandler(Configuration configuration) : configuration(configuration) { |
48 mq.reset(PosixMQ::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose)); |
65 mq.reset(PosixMQ::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose)); |
49 } |
66 } |
50 |
67 |
51 void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
68 void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
52 |
69 currentRelation = CurrentRelation{name, attributes}; |
53 } |
70 } |
54 |
71 |
55 void attribute(const relpipe::common::type::StringX& value) override { |
72 void attribute(const relpipe::common::type::StringX& value) override { |
56 // TODO: send only certain attributes |
73 |
57 mq->send(convertor.to_bytes(value)); |
74 auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
|
75 if (attributeName == L"text") currentRelation.currentValue = formatText(value); |
|
76 else if (attributeName == L"data") currentRelation.currentValue = formatData(value); |
|
77 |
|
78 currentRelation.attributeIndex++; |
|
79 if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
|
80 currentRelation.attributeIndex = 0; |
|
81 mq->send(currentRelation.currentValue); |
|
82 } |
|
83 |
58 } |
84 } |
59 |
85 |
60 void endOfPipe() { |
86 void endOfPipe() { |
61 |
87 |
62 } |
88 } |