diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp index aed8f1be09d634589d50c0cc1e538a2228785ad4..3998e696708441ecc57fe3de79a1172f86c42205 100755 --- a/sflphone-common/src/config/yamlemitter.cpp +++ b/sflphone-common/src/config/yamlemitter.cpp @@ -34,7 +34,7 @@ namespace Conf { -YamlEmitter::YamlEmitter(const char *file) : filename(file) +YamlEmitter::YamlEmitter(const char *file) : filename(file), isFirstAccount(true) { open(); } @@ -60,6 +60,9 @@ void YamlEmitter::open() yaml_emitter_set_output_file(&emitter, fd); yaml_document_initialize(&document, NULL, NULL, NULL, 0, 0); + + // Init the main configuration mapping + topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE); } void YamlEmitter::close() @@ -82,70 +85,70 @@ void YamlEmitter::read() {} void YamlEmitter::write() { - serializeData(); - - for(int i = 0; i < eventNumber; i++) { - if(!yaml_emitter_emit(&emitter, &(events[i]))) - throw YamlEmitterException("Falied to emit event"); - - yaml_emitter_flush(&emitter); - } } void YamlEmitter::serializeData() { + yaml_emitter_dump(&emitter, &document); +} - unsigned char sclr[20]; - snprintf((char *)sclr, 20, "%s", "value"); - yaml_char_t *value = (yaml_char_t *)sclr; - - - // yaml_document_add_scalar(&document, NULL, value, -1, YAML_PLAIN_SCALAR_STYLE); - // yaml_emitter_dump(&emitter, &document); - eventNumber = 0; - - yaml_event_t event; - - yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING); - events[eventNumber++] = event; - yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0); - events[eventNumber++] = event; +void YamlEmitter::writeAccount(MappingNode *map) +{ - - yaml_document_end_event_initialize(&event, 0); - events[eventNumber++] = event; + std::string accountstr("accounts"); + + if(isFirstAccount) { + // accountSequence need to be static outside this scope since reused each time an account is written + accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE); + int accountid = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE); + yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence); + isFirstAccount = false; + } - // yaml_scalar_event_initialize(event, yaml_char_t *anchor, yaml_char_t *tag, yaml_char_t *value, int length, int plain_implicit, int quoted_implicit, yaml_scalar_style_t style) - yaml_scalar_event_initialize(&event, NULL, NULL, value, 5, 0, 0, YAML_PLAIN_SCALAR_STYLE); - events[eventNumber++] = event; + int accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE); + yaml_document_append_sequence_item (&document, accountSequence, accountmapping); - // yaml_sequence_start_event_initialize + Mapping *internalmap = map->getMapping(); + Mapping::iterator iter = internalmap->begin(); - // yaml_sequence_end_event_initialize + while(iter != internalmap->end()) { + addMappingItem(accountmapping, iter->first, iter->second); + iter++; + } - // yaml_mapping_start_event_initialize +} - // yaml_mapping_end_event_initialize - // yaml_event_delete + void YamlEmitter::addMappingItem(int mappingid, Key key, YamlNode *node) +{ - yaml_stream_end_event_initialize(&event); - events[eventNumber++] = event; + if(node->getType() == SCALAR) { -} + ScalarNode *sclr = (ScalarNode *)node; + int temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE); + int temp2 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE); + yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2); -void YamlEmitter::writeDocument() -{ - unsigned char sclr[20]; - snprintf((char *)sclr, 20, "%s", "value"); - yaml_char_t *value = (yaml_char_t *)sclr; + } + else if(node->getType() == MAPPING){ - yaml_document_add_scalar(&document, NULL, value, -1, YAML_PLAIN_SCALAR_STYLE); - yaml_emitter_dump(&emitter, &document); + int temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE); + int temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE); + yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2); + MappingNode *map = (MappingNode *)node; + Mapping *internalmap = map->getMapping(); + Mapping::iterator iter = internalmap->begin(); + + while(iter != internalmap->end()) { + addMappingItem(temp2, iter->first, iter->second); + iter++; + } + } } + } diff --git a/sflphone-common/src/config/yamlemitter.h b/sflphone-common/src/config/yamlemitter.h index 2ad9dd8d2ed97cb84d8b6a946f972c93b31e42d0..2bd0c24d0c8b14a51db3ed56eb8da494f0456d54 100755 --- a/sflphone-common/src/config/yamlemitter.h +++ b/sflphone-common/src/config/yamlemitter.h @@ -34,6 +34,7 @@ #include <yaml.h> #include <exception> #include <string> +#include "yamlnode.h" namespace Conf { @@ -74,12 +75,24 @@ class YamlEmitter { void write(); - void writeDocument(); + void writeAccount(MappingNode *map); - private: + void writePreference(); + + void writeAddressbook(); + + void writeAudio(); + + void writeHooks(); + + void writeVoiplink(); void serializeData(); + private: + + void addMappingItem(int mappingid, Key key, YamlNode *node); + std::string filename; FILE *fd; @@ -99,9 +112,28 @@ class YamlEmitter { */ unsigned char buffer[EMITTER_BUFFERSIZE]; + + /** + * Main document for this serialization + */ yaml_document_t document; - int eventNumber; + /** + * Reference id to the top levell mapping when creating + */ + int topLevelMapping; + + /** + * We need to add the account sequence if this is the first account to be + */ + bool isFirstAccount; + + /** + * Reference to the account sequence + */ + int accountSequence; + + friend class ConfigurationTest; }; diff --git a/sflphone-common/test/configurationtest.cpp b/sflphone-common/test/configurationtest.cpp index 062a2faca5b944067d65d72e2ac25b71b0b6b9e2..62fa5743d95b74c7d2367048420686acaacd2aaa 100755 --- a/sflphone-common/test/configurationtest.cpp +++ b/sflphone-common/test/configurationtest.cpp @@ -180,11 +180,112 @@ void ConfigurationTest::testYamlEmitter() { Conf::YamlEmitter *emitter; + Conf::MappingNode accountmap(NULL); + Conf::MappingNode credentialmap(NULL); + Conf::MappingNode srtpmap(NULL); + Conf::MappingNode zrtpmap(NULL); + Conf::MappingNode tlsmap(NULL); + + + Conf::ScalarNode id("Account:1278432417"); + Conf::ScalarNode username("181"); + Conf::ScalarNode password("pass181"); + Conf::ScalarNode alias("sfl-181"); + Conf::ScalarNode hostname("192.168.50.3"); + Conf::ScalarNode enable("true"); + Conf::ScalarNode type("SIP"); + Conf::ScalarNode expire("3600"); + Conf::ScalarNode interface("default"); + Conf::ScalarNode port("5060"); + Conf::ScalarNode mailbox("97"); + Conf::ScalarNode publishAddr("192.168.50.182"); + Conf::ScalarNode publishPort("5060"); + Conf::ScalarNode sameasLocal("true"); + Conf::ScalarNode resolveOnce("false"); + Conf::ScalarNode codecs("0/9/110/111/112/"); + Conf::ScalarNode stunServer("stun.sflphone.org"); + Conf::ScalarNode stunEnabled("false"); + Conf::ScalarNode displayName("Alexandre Savard"); + Conf::ScalarNode dtmfType("sipinfo"); + + Conf::ScalarNode count("0"); + Conf::ScalarNode srtpenabled("false"); + Conf::ScalarNode keyExchange("sdes"); + Conf::ScalarNode rtpFallback("false"); + + Conf::ScalarNode displaySas("false"); + Conf::ScalarNode displaySasOnce("false"); + Conf::ScalarNode helloHashEnabled("false"); + Conf::ScalarNode notSuppWarning("false"); + + Conf::ScalarNode tlsport(""); + Conf::ScalarNode certificate(""); + Conf::ScalarNode calist(""); + Conf::ScalarNode ciphers(""); + Conf::ScalarNode tlsenabled("false"); + Conf::ScalarNode tlsmethod("TLSV1"); + Conf::ScalarNode timeout("0"); + Conf::ScalarNode tlspassword(""); + Conf::ScalarNode privatekey(""); + Conf::ScalarNode requirecertif("true"); + Conf::ScalarNode server(""); + Conf::ScalarNode verifyclient("true"); + Conf::ScalarNode verifyserver("true"); + + accountmap.setKeyValue(aliasKey, &alias); + accountmap.setKeyValue(typeKey, &type); + accountmap.setKeyValue(idKey, &id); + accountmap.setKeyValue(usernameKey, &username); + accountmap.setKeyValue(passwordKey, &password); + accountmap.setKeyValue(hostnameKey, &hostname); + accountmap.setKeyValue(accountEnableKey, &enable); + accountmap.setKeyValue(mailboxKey, &mailbox); + accountmap.setKeyValue(expireKey, &expire); + accountmap.setKeyValue(interfaceKey, &interface); + accountmap.setKeyValue(portKey, &port); + accountmap.setKeyValue(publishAddrKey, &publishAddr); + accountmap.setKeyValue(publishPortKey, &publishPort); + accountmap.setKeyValue(sameasLocalKey, &sameasLocal); + accountmap.setKeyValue(resolveOnceKey, &resolveOnce); + accountmap.setKeyValue(dtmfTypeKey, &dtmfType); + accountmap.setKeyValue(displayNameKey, &displayName); + + accountmap.setKeyValue(srtpKey, &srtpmap); + srtpmap.setKeyValue(srtpEnableKey, &srtpenabled); + srtpmap.setKeyValue(keyExchangeKey, &keyExchange); + srtpmap.setKeyValue(rtpFallbackKey, &rtpFallback); + + accountmap.setKeyValue(zrtpKey, &zrtpmap); + zrtpmap.setKeyValue(displaySasKey, &displaySas); + zrtpmap.setKeyValue(displaySasOnceKey, &displaySasOnce); + zrtpmap.setKeyValue(helloHashEnabledKey, &helloHashEnabled); + zrtpmap.setKeyValue(notSuppWarningKey, ¬SuppWarning); + + accountmap.setKeyValue(credKey, &credentialmap); + credentialmap.setKeyValue(credentialCountKey, &count); + + accountmap.setKeyValue(tlsKey, &tlsmap); + tlsmap.setKeyValue(tlsPortKey, &tlsport); + tlsmap.setKeyValue(certificateKey, &certificate); + tlsmap.setKeyValue(calistKey, &calist); + tlsmap.setKeyValue(ciphersKey, &ciphers); + tlsmap.setKeyValue(tlsEnableKey, &tlsenabled); + tlsmap.setKeyValue(methodKey, &tlsmethod); + tlsmap.setKeyValue(timeoutKey, &timeout); + tlsmap.setKeyValue(tlsPasswordKey, &tlspassword); + tlsmap.setKeyValue(privateKeyKey, &privatekey); + tlsmap.setKeyValue(requireCertifKey, &requirecertif); + tlsmap.setKeyValue(serverKey, &server); + tlsmap.setKeyValue(verifyClientKey, &verifyclient); + tlsmap.setKeyValue(verifyServerKey, &verifyserver); + try{ emitter = new Conf::YamlEmitter("/tmp/sequenceEmiter.txt"); - emitter->writeDocument(); + emitter->writeAccount(&accountmap); + emitter->writeAccount(&accountmap); + emitter->serializeData(); delete emitter; } diff --git a/sflphone-common/test/configurationtest.h b/sflphone-common/test/configurationtest.h index 092da3112a09bf423116446a20086909cf4108b0..84d840674fad87fcaf467925c05a9537a7983464 100755 --- a/sflphone-common/test/configurationtest.h +++ b/sflphone-common/test/configurationtest.h @@ -53,6 +53,8 @@ #include "config/yamlparser.h" #include "config/yamlemitter.h" #include "config/yamlnode.h" +#include "sip/sipaccount.h" +#include "account.h" class ConfigurationTest: public CppUnit::TestFixture {