Skip to content
Snippets Groups Projects
Commit 12cfa3fe authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3649] Implement yaml account emiter object

parent cbaefa24
No related branches found
No related tags found
No related merge requests found
......@@ -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;
void YamlEmitter::writeAccount(MappingNode *map)
{
yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
events[eventNumber++] = event;
std::string accountstr("accounts");
yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
events[eventNumber++] = event;
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;
}
int accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE);
yaml_document_append_sequence_item (&document, accountSequence, accountmapping);
yaml_document_end_event_initialize(&event, 0);
events[eventNumber++] = event;
Mapping *internalmap = map->getMapping();
Mapping::iterator iter = internalmap->begin();
// 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;
while(iter != internalmap->end()) {
addMappingItem(accountmapping, iter->first, iter->second);
iter++;
}
// yaml_sequence_start_event_initialize
}
// yaml_sequence_end_event_initialize
// yaml_mapping_start_event_initialize
void YamlEmitter::addMappingItem(int mappingid, Key key, YamlNode *node)
{
// yaml_mapping_end_event_initialize
if(node->getType() == SCALAR) {
// yaml_event_delete
ScalarNode *sclr = (ScalarNode *)node;
yaml_stream_end_event_initialize(&event);
events[eventNumber++] = event;
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);
}
else if(node->getType() == MAPPING){
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);
void YamlEmitter::writeDocument()
{
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);
MappingNode *map = (MappingNode *)node;
Mapping *internalmap = map->getMapping();
Mapping::iterator iter = internalmap->begin();
while(iter != internalmap->end()) {
addMappingItem(temp2, iter->first, iter->second);
iter++;
}
}
}
}
......@@ -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;
};
......
......@@ -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, &notSuppWarning);
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;
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment