SlideShare uma empresa Scribd logo
1 de 48
Baixar para ler offline
POCO C++ Libraries
uilian.ries@datacom.ind.br
Junho / 2016
ETHERNET SWITCHES SOLUTIONS
AGENDA
• O que é a POCO?
• Onde é utilizada
• POCO vs boost
• Como usar no DmOS
• Cache
• Process
• Filesystem
• Application
• Net
• Notification
• DirectoryWatcher
• DynamicAny
• String
ETHERNET SWITCHES SOLUTIONS
O que é a POCO?
• POrtable COmponents;
• Coleção de bibliotecas C++;
• Focada em redes e aplicações;
• Altamente portável;
• Lançada em 2005, Áustria;
• Atualmente com 120 colaboradores (github);
• Compacta, ~8MB;
• Documentação de fácil leitura;
• Links:
– http://pocoproject.org
– http://pocoproject.org/documentation
ETHERNET SWITCHES SOLUTIONS
O que é a POCO?
ETHERNET SWITCHES SOLUTIONS
Onde é utilizada?
• Sim, é utilizada em sistemas embarcados:
– ACTIA Automotive
●
Sistema de diagnóstico automotivo.
– Schneider Electric
●
Plataforma de automação embarcada.
– StreamUnlimited
●
Aplicação embarcada para TV.
– Starticket
●
Dispositivo para emissão de passagem.
ETHERNET SWITCHES SOLUTIONS
POCO vs boost
• Poco é mais compreensível;
• Mais fácil de aprender;
• Possui suporte em pontos que a boost não dá:
– SQLite; Process; Application; Directory Watch
• Desvantagens:
– Comunidade não tão ampla como a boost;
– Documentação não extensa;
– C++11 superou alguns pontos;
– Boost cobre igualmente alguns pontos.
ETHERNET SWITCHES SOLUTIONS
Como usar no DmOS
• Versão Debian: 1.3.6 (2009)
• Versão atual: 1.7.3 (maio/2016)
• Versão no DmOS: 1.6.1 (agosto/2015)
ETHERNET SWITCHES SOLUTIONS
Como usar no DmOS
• dmos-dmtdd/dmptinfo/externals/
dmos-template/
dmos-template-test.json
{
“package”: “dmos-template-test”,
“template”: “test”,
“builddeps”: [
“poco”
]
}
ETHERNET SWITCHES SOLUTIONS
Como usar no DmOS
• CMake
– find_package(Poco) → Ops!
– FindPoco.cmake:
●
https://goo.gl/kaveoK
– PocoConfig.cmake (oficial):
●
https://goo.gl/pbMJZD
ETHERNET SWITCHES SOLUTIONS
Como usar no DmOS
• CMake
– find_library(Poco_FOUNDATION PocoFoundation)
– find_library(Poco_UTIL PocoUtil)
– find_library(Poco_Net PocoNet)
– find_library(Poco_DATA PocoData)
– set(Poco_LIBRARIES ${Poco_FOUNDATION} ${Poco_UTIL}
${Poco_Net} ${Poco_DATA})
– target_link_libraries(${PROJECT_NAME} ${Poco_LIBRARIES})
– Bibliotecas com sufixo d são de DEBUG
ETHERNET SWITCHES SOLUTIONS
Como usar no DmOS
• No Buildroot:
Simbolo: BR2_PACKAGE_POCO
Localização:
→ Target packages
→ Libraries
→ Other
Filhas:
BR2_PACKAGE_POCO_CRYPTO
BR2_PACKAGE_POCO_DATA
BR2_PACKAGE_POCO_UTIL
BR2_PACKAGE_POCO_NET
ETHERNET SWITCHES SOLUTIONS
POCO Cache
• Boost possui circular buffer e multi-index;
• STL possui apenas seus containers;
• Atualmente manipulamos LRU no braço;
• Vantagem de utilizar Poco Cache:
– Container com tamanho limitado;
– Container com tempo limitado;
– Notificação de alteração na cache;
– Acesso ao item mais recente.
ETHERNET SWITCHES SOLUTIONS
POCO Cache – Least Recently Used
#include "Poco/LRUCache.h"
Poco::LRUCache<int, std::string> cache(3);
cache.add(1, "Foo"); // |-1-| -> primeiro elemento MRU
Poco::SharedPtr<std::string> elem = cache.get(1); // |-1-|
cache.add(2, "Bar"); // |-2-1-|
cache.add(3, "Qux"); // |-3-2-1-|
// Insere novo elemento mais impopular : "Couse"
cache.add(4, "Couse"); // |-4-3-2-|
assert (*elem == "Foo"); // Conteúdo ainda válido
elem = cache.get(2); // |-2-4-3-|
// Substitui Bar por Baz
cache.add(2, "Baz"); // 2 Eventos: Remove e Add
ETHERNET SWITCHES SOLUTIONS
POCO Cache – Time Based Expiration
#include "Poco/ExpireCache.h"
// Por padrão, expira TODOS OS VALORES após 10 min
Poco::ExpireCache<int, std::string> cache;
// Customização para 1 sec
Poco::ExpireCache<int, std::string> cache(1000);
• Uso de Poco::SharedPtr garante o tempo de vida da variável;
• Cuidado ao combinar as funções has() e get()
– Timeout entre as duas chamadas resulta em null pointer.
ETHERNET SWITCHES SOLUTIONS
POCO Cache – Time Based Expiration
#include "Poco/UniqueExpireCache.h"
#include “Poco/ExpirationDecorator.h”
// Expira o tempo de vida POR ELEMENTO
Poco::UniqueExpireCache<int, std::string> cache;
// Utiliza pattern Decorator para inserção de elementos
using ExprDecStr = Poco::ExpirationDecorator<std::string>;
cache.add(1, ExprDecStr(“Foo”, 1000)); // 1 sec
cache.add(2, ExprDecStr(“Bar”, 15000)); // 15 sec
poco_assert (cache.size() == 2);
std::this_thread::sleep_for(1s);
assert (cache.size() == 1);
Poco::SharedPtr<ExprDecStr> elem = cache.get(1);
assert (elem.isNull());
ETHERNET SWITCHES SOLUTIONS
POCO Cache – Time Based Expiration
#include "Poco/AccessExpireCache.h"
#include “Poco/UniqueAccessExpireCache.h”
// Expira o tempo de vida quando NÃO ACESSADO
Poco::AccessExpireCache<int, std::string> cache;
Poco::UniqueAccessExpireCache<int, std::string> cache;
• Dicas:
– Evitar usar has() com ExpireCache;
– O arcabouço de Cache é extensível:
Poco::AbstractStrategy;
– Poco Cache utiliza std::map, para std::set, utilize
Poco::Void como placeholder.
ETHERNET SWITCHES SOLUTIONS
POCO Process
• Boost Process não é oficial;
• STL não possui invocação de processos;
• Atualmente usamos system, popen ou fork;
• Vantagem de utilizar Poco Process:
– Manipular o tempo de vida do processo de forma simples.
ETHERNET SWITCHES SOLUTIONS
POCO Process
#include <Poco/Process.h>
Poco::Process::Args args = {“--foo=true”};
Poco::ProcessHandle handle =
Poco::Process::launch("dmos-example", args, "/bin");
// Do something ....
Poco::Process::requestTermination(handle.id());
Poco::Process::wait(handle);
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem
• Boost filesystem é oficial;
• STL receberá filesystem no C++17;
• Atualmente usamos string para manipular path;
• Vantagem de utilizar Poco File:
– Manipulação de path (prefix, sufix, absolute, extension);
– Iterar sobre diretórios;
– Validar tipo de arquivo (is [directory | regular file]);
– Buscar arquivo no sistema;
– Validar acessos (can [write | read | exec]);
Boost
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem - Path
#include <Poco/Path.h>
Poco::Path mac("/sys/class/net/eth0/address");
assert(mac.isFile());
// file_name = "address"
Poco::Path file_name = mac.getBaseName();
// dir_name = Poco::Path("/sys/class/net/eth0/");
Poco::Path dir_name = mac.parent();
std::string ss = file_name.toString();
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem - Find
#include <Poco/Path.h>
#include <Poco/Environment.h>
std::string process_name("dmos-example");
std::string path(Poco::Environment::get("PATH"));
path += Poco::Path::pathSeparator();
path += "/dmpt/target/imported/bin";
Poco::Path process_path;
assert(Poco::Path::find(
path, process_name, process_path));
assert(process_path.isFile());
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem - File
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/Timestamp.h>
Poco::Path mac("/sys/class/net/eth0/address");
Poco::File mac_fd(mac);
assert(mac_fd.isFile());
assert(mac_fd.canRead());
assert(!mac_fd.canWrite());
mac_fd.copyTo("/tmp");
Poco::Timestamp mac_date = mac_fd.created();
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem – Directory Iterator
#include <Poco/Path.h>
#include <Poco/DirectoryIterator.h>
Poco::Path cwd(Path::current());
Poco::DirectoryIterator it(cwd);
Poco::DirectoryIterator end;
while (it != end) {
if (it->isFile()) {
assert(it->canRead());
}
++it;
}
ETHERNET SWITCHES SOLUTIONS
POCO Filesystem – Glob
#include <Poco/Glob.h>
#include <Poco/File.h>
std::set<std::string> files;
Poco::Glob::glob("/usr/local/include/*/*.h", files);
for (const auto& it : files) {
Poco::File file(it);
assert(file.isFile());
assert(file.canRead());
}
ETHERNET SWITCHES SOLUTIONS
POCO Application
• Boost Application não é oficial;
• STL não possui pattern para aplicação;
• Atualmente usamos main e nada mais;
• Vantagem de utilizar Poco Application:
– Manter um padrão para construção da main;
– Manipulação de parâmetros de graça;
– Manipulação de arquivo de configuração de graça;
– Logger de graça;
– Estender sub-sistemas como filhas;
ETHERNET SWITCHES SOLUTIONS
POCO Application – Subsystem
// Retorna o nome do aplicativo.
const char* name() const
// Inicializa o sub-sistema
void initialize(Poco::Util::Application& app);
// Prepara o sub-sistema para morrer
void uninitialize(Poco::Util::Application& app);
// Reconfigura o sub-sistema
void reinitialize(Poco::Util::Application& app);
ETHERNET SWITCHES SOLUTIONS
POCO Application – Main
class FooApp : public Poco::ServerApplication {
int main(const Poco::ArgVec& args) {
Poco::TaskManager task_manager;
task_manager.start(new FooTask);
WaitForTerminationRequest(); // Aguarda SIGTERM
task_manager.cancelAll();
task_manager.joinAll();
return Poco::Util::Application::EXIT_OK;
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Application – Handle Options
class FooApp : public Poco::ServerApplication {
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "display help information")
.required(false)
.repeatable(false)
.callback(OptionCallback<FooApp>(this,
&FooApp::handleHelp)));
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Application – Configuration File
class FooApp : public Poco::ServerApplication {
void initialize(Poco::Application& self )
{
loadConfiguration();
if (config().hasOption(“serveruri”)) {
std::string ss = config().getString(“serveruri”);
}
Poco::Util::Application::initialize(self );
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Application – Logger
class FooApp : public Poco::ServerApplication {
void uninitialize(Poco::Application& self)
{
logger().debug(“Finish Foo Application”);
dmmw_finish(&dmmw);
Poco::Util::Application::uninitialize(self);
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Net
• Boost ASIO é oficial porém “complexa”;
• STL networking possui proposta para C++17;
• Atualmente usamos nativo;
• Vantagem de utilizar Poco Net:
– Manipular interface de rede (ip, mac);
– Socket (client/server);
– Conexão (Design Pattern Reactor);
– HTTP (client/server);
– TCP (client/server);
– FTP (client/server).
ETHERNET SWITCHES SOLUTIONS
POCO Net – Socket TCP
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/StreamCopier.h>
Poco::Net::SocketAddress sa("datacom.ind.br", 80);
Poco::Net::StreamSocket socket(sa);
Poco::Net::SocketStream str(socket);
str << "GET / HTTP/1.1rn”
"Host: datacom.ind.brrn"
"rn";
str.flush();
Poco::StreamCopier::copyStream(str, std::cout);
ETHERNET SWITCHES SOLUTIONS
POCO Net – Network Interface
#include <Poco/Net/NetworkInterface.h>
using Poco::Net::NetworkInterface;
NetworkInterface::NetworkInterfaceList interface_list
= Poco::Net::NetworkInterface::list();
for (const auto& it : interface_list) {
printf(“%s - %sn”, it.name(), it.address().toString());
printf("%02X:%02X:%02X:%02X:%02X:02Xn",
it.macAddress()[0], it.macAddress()[1],
it.macAddress()[2], it.macAddress()[3],
it.macAddress()[4], it.macAddress()[5]);
}
ETHERNET SWITCHES SOLUTIONS
POCO Notification & Events
• Boost possui signals2;
• STL possui function object;
• Atualmente usamos std::function, functor ou ponteiro;
• Vantagem de utilizar Poco Delegate:
– Pattern Observer;
ETHERNET SWITCHES SOLUTIONS
POCO Notification & Events - Delegate
#include <Poco/BasicEvent.h>
class Subject {
public:
Poco::BasicEvent<int> event;
void Notify(int n){
event.notify(this, n);
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Notification & Events - Delegate
class Observer {
public:
void OnEvent(const void* pSender, int& arg) {
assert(arg != -1);
}
};
ETHERNET SWITCHES SOLUTIONS
POCO Notification & Events - Delegate
#include <Poco/Delegate.h>
Subject subject;
std::array<Observer, 3> observers;
for (auto& observer : observers) { // Cadastra
subject.event += Poco::delegate(&observer,
&Observer::OnEvent);
}
subject.Notify(42); // Notifica todos os observadores
for (auto& observer : observers) { // Remove
subject.event -= Poco::delegate(&observer,
&Observer::OnEvent);
}
ETHERNET SWITCHES SOLUTIONS
POCO DirectoryWatcher
• Boost dir_monitor não é oficial;
• STL não possui suporte a eventos por arquivo;
• Atualmente usamos inotify;
• Vantagem de utilizar Poco DirectoryWatcher:
– Assistir mudanças em diretórios (não bloqueante);
– Manipulação mais fácil do que inotify.
ETHERNET SWITCHES SOLUTIONS
POCO DirectoryWatcher
#include <Poco/DirectoryWatcher.h>
DirectoryWatcher watcher(“/var/log”,
DW_FILTER_ENABLE_ALL,
DW_DEFAULT_SCAN_INTERVAL);
watcher.itemModified += Poco::Delegate(&target,
Target::OnEvent);
Target::OnEvent(const DirectoryEvent& event) {
assert(event.f == “/var/log/syslog”);
}
ETHERNET SWITCHES SOLUTIONS
POCO DynamicAny
• Boost possui any;
• STL integrará fundamentals TS no C++17;
• Atualmente não usamos este conceito;
• Vantagem de utilizar Poco DynamicAny:
– Utilizar container dinâmico para tipos;
– Converter sem precisar saber o tipo original.
ETHERNET SWITCHES SOLUTIONS
POCO DynamicAny – boost::any
#include <boost/any.hpp>
using any_vector = std::vector<boost::any>;
any_vector foo = { 1, "foobar", true, 42.3 };
for (const auto& it : foo) {
std::cout << boost::any_cast<std::string>(it);
}
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::er
ror_info_injector<boost::bad_any_cast> >'
what(): boost::bad_any_cast: failed conversion using
boost::any_cast
ETHERNET SWITCHES SOLUTIONS
POCO DynamicAny
#include <Poco/DynamicAny.h>
using dynamic_vector = std::vector<Poco::DynamicAny>;
dynamic_vector dyn_vec = { "foo", 42, 3.14 };
for (const auto& it : dyn_vec) {
std::cout << it.convert<std::string>() << std::endl;
}
ETHERNET SWITCHES SOLUTIONS
POCO String
• Boost possui String Algorithms;
• STL possui apenas algorithms;
• Atualmente usamos boost e STL;
• Vantagem de utilizar Poco String:
– Atalho para algoritmos para string;
– Sobre boost, não tem vantagens;
– Economia de fosfato.
ETHERNET SWITCHES SOLUTIONS
POCO String – Compare ignore case
• STL
#include <algorithm>
std::equal(left.begin(), left.end(), right.begin(),
[](std::string::value_type a, std::string::value_type b) {
return std::tolower(a) == std::tolower(b);
});
• Poco
#include <Poco/String.h>
Poco::icompare(str1, str2);
• Boost
#include <boost/algorithm/string.hpp>
boost::iequals(str1, str2);
ETHERNET SWITCHES SOLUTIONS
POCO String – To lower case
• STL
#include <algorithm>
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
• Poco
#include <Poco/String.h>
Poco::toLower(str);
• Boost
#include <boost/algorithm/string.hpp>
boost::algorithm::to_lower(str);
boost::algorithm::to_lower_copy(str);
ETHERNET SWITCHES SOLUTIONS
POCO String – Trim
• STL
#include <algorithm>
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
• Poco
#include <Poco/String.h>
Poco::trim(str);
• Boost
#include <boost/algorithm/string.hpp>
boost::erase_all::(str, “ ”);
ETHERNET SWITCHES SOLUTIONS
POCO String – Split
• STL
#include <algorithm>
std::vector<std::string> tokens;
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(), std::back_inserter(tokens));
• Poco
#include <Poco/StringTokenizer.h>
Poco::StringTokenizer(“foo bar qux baz couse” ,“ ”);
• Boost
#include <boost/algorithm/string.hpp>
std::vector<std::string> tokens;
boost::split(tokens, "string to split", boost::is_any_of(" "));
ETHERNET SWITCHES SOLUTIONS
● http://pocoproject.org/
● http://pocoproject.org/documentation
● https://github.com/pocoproject/poco

Mais conteúdo relacionado

Mais procurados

Primeiros passos com o Docker
Primeiros passos com o DockerPrimeiros passos com o Docker
Primeiros passos com o DockerWebSix
 
Orquestrando Docker Containers: Machine, Swarm e Compose
Orquestrando Docker Containers: Machine, Swarm e ComposeOrquestrando Docker Containers: Machine, Swarm e Compose
Orquestrando Docker Containers: Machine, Swarm e ComposeRoberto Hashioka
 
Docker na vida real
Docker na vida realDocker na vida real
Docker na vida realFernando Ike
 
Testadores 2015 - O Fantástico Mundo de Docker
Testadores 2015 - O Fantástico Mundo de DockerTestadores 2015 - O Fantástico Mundo de Docker
Testadores 2015 - O Fantástico Mundo de DockerStefan Teixeira
 
Deploying Docker Containers
Deploying Docker ContainersDeploying Docker Containers
Deploying Docker ContainersHugo Henley
 
Como migrar seu ambiente de desenvolvimento para Linux
Como migrar seu ambiente de desenvolvimento para LinuxComo migrar seu ambiente de desenvolvimento para Linux
Como migrar seu ambiente de desenvolvimento para LinuxMarcelo Sabadini
 
TDC 2014 SP - E o DeltaSpike ?
TDC 2014 SP - E o DeltaSpike ?TDC 2014 SP - E o DeltaSpike ?
TDC 2014 SP - E o DeltaSpike ?Rafael Benevides
 
TDC 2015 POA - O Fantástico Mundo de Docker
TDC 2015 POA - O Fantástico Mundo de DockerTDC 2015 POA - O Fantástico Mundo de Docker
TDC 2015 POA - O Fantástico Mundo de DockerStefan Teixeira
 
Yaf - Um framework que roda sob uma extensão
Yaf - Um framework que roda sob uma extensãoYaf - Um framework que roda sob uma extensão
Yaf - Um framework que roda sob uma extensãoThiago Paes
 
CDI Extensions e DeltaSpike
CDI Extensions e DeltaSpikeCDI Extensions e DeltaSpike
CDI Extensions e DeltaSpikeRafael Benevides
 
Alta Performance de Aplicações PHP com Nginx
Alta Performance de Aplicações PHP com NginxAlta Performance de Aplicações PHP com Nginx
Alta Performance de Aplicações PHP com NginxThiago Paes
 
Docker, facilitando a vida do desenvolvimento
Docker, facilitando a vida do desenvolvimentoDocker, facilitando a vida do desenvolvimento
Docker, facilitando a vida do desenvolvimentoSérgio Lima
 
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...Bruno Rodrigues
 

Mais procurados (20)

Iniciando com docker
Iniciando com dockerIniciando com docker
Iniciando com docker
 
Primeiros passos com o Docker
Primeiros passos com o DockerPrimeiros passos com o Docker
Primeiros passos com o Docker
 
Orquestrando Docker Containers: Machine, Swarm e Compose
Orquestrando Docker Containers: Machine, Swarm e ComposeOrquestrando Docker Containers: Machine, Swarm e Compose
Orquestrando Docker Containers: Machine, Swarm e Compose
 
Debug de cabo a rabo
Debug de cabo a raboDebug de cabo a rabo
Debug de cabo a rabo
 
Docker na vida real
Docker na vida realDocker na vida real
Docker na vida real
 
Testadores 2015 - O Fantástico Mundo de Docker
Testadores 2015 - O Fantástico Mundo de DockerTestadores 2015 - O Fantástico Mundo de Docker
Testadores 2015 - O Fantástico Mundo de Docker
 
Docker + Django
Docker + DjangoDocker + Django
Docker + Django
 
5. rodando containers docker na aws
5. rodando containers docker na aws5. rodando containers docker na aws
5. rodando containers docker na aws
 
Deploying Docker Containers
Deploying Docker ContainersDeploying Docker Containers
Deploying Docker Containers
 
Como migrar seu ambiente de desenvolvimento para Linux
Como migrar seu ambiente de desenvolvimento para LinuxComo migrar seu ambiente de desenvolvimento para Linux
Como migrar seu ambiente de desenvolvimento para Linux
 
TDC 2014 SP - E o DeltaSpike ?
TDC 2014 SP - E o DeltaSpike ?TDC 2014 SP - E o DeltaSpike ?
TDC 2014 SP - E o DeltaSpike ?
 
Conceitos Basicos e Docker - Java Noroeste
Conceitos Basicos e Docker - Java NoroesteConceitos Basicos e Docker - Java Noroeste
Conceitos Basicos e Docker - Java Noroeste
 
TDC 2015 POA - O Fantástico Mundo de Docker
TDC 2015 POA - O Fantástico Mundo de DockerTDC 2015 POA - O Fantástico Mundo de Docker
TDC 2015 POA - O Fantástico Mundo de Docker
 
Yaf - Um framework que roda sob uma extensão
Yaf - Um framework que roda sob uma extensãoYaf - Um framework que roda sob uma extensão
Yaf - Um framework que roda sob uma extensão
 
CDI Extensions e DeltaSpike
CDI Extensions e DeltaSpikeCDI Extensions e DeltaSpike
CDI Extensions e DeltaSpike
 
Estudo de caso - Uso Docker no Desenvolvimento
Estudo de caso - Uso Docker no DesenvolvimentoEstudo de caso - Uso Docker no Desenvolvimento
Estudo de caso - Uso Docker no Desenvolvimento
 
Alta Performance de Aplicações PHP com Nginx
Alta Performance de Aplicações PHP com NginxAlta Performance de Aplicações PHP com Nginx
Alta Performance de Aplicações PHP com Nginx
 
Docker, facilitando a vida do desenvolvimento
Docker, facilitando a vida do desenvolvimentoDocker, facilitando a vida do desenvolvimento
Docker, facilitando a vida do desenvolvimento
 
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...
Trabalhando com a infraestrutura como software na AWS com Elastic Beanstalk e...
 
Alagoas Dev Day
Alagoas Dev DayAlagoas Dev Day
Alagoas Dev Day
 

Semelhante a POCO C++ Libraries Overview

Semana Acadêmica ICET - Feevale - 12/04/2014
Semana Acadêmica ICET - Feevale - 12/04/2014Semana Acadêmica ICET - Feevale - 12/04/2014
Semana Acadêmica ICET - Feevale - 12/04/2014Getup Cloud
 
Apresentação do Aplicativo de Controle de Combustíveis
Apresentação do Aplicativo de Controle de CombustíveisApresentação do Aplicativo de Controle de Combustíveis
Apresentação do Aplicativo de Controle de CombustíveisMauricio Fernandes de Castro
 
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker Compose
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker ComposeScrum Gathering Rio 2016 - Conteinerizando Testes com Docker Compose
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker ComposeStefan Teixeira
 
Colaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPColaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPThiago Colares
 
Cake php selecaodeprojetos-apres-em-modelo
Cake php selecaodeprojetos-apres-em-modeloCake php selecaodeprojetos-apres-em-modelo
Cake php selecaodeprojetos-apres-em-modeloCBA2012
 
Otimizacao de websites em PHP
Otimizacao de websites em PHPOtimizacao de websites em PHP
Otimizacao de websites em PHPFelipe Ribeiro
 
Colaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPColaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPCBA2012
 
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...Jeronimo Zucco
 
Faça seu portal voar usando o plone.app.caching
Faça seu portal voar usando o plone.app.cachingFaça seu portal voar usando o plone.app.caching
Faça seu portal voar usando o plone.app.cachingFabiano Weimar
 
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...Jeronimo Zucco
 
Introdução à Programação Python e Tk
Introdução à Programação Python e TkIntrodução à Programação Python e Tk
Introdução à Programação Python e TkCarlos Campani
 
Cache em aplicações web
Cache em aplicações webCache em aplicações web
Cache em aplicações webJean Carlo Emer
 
Tutorial Django + Python
Tutorial Django + PythonTutorial Django + Python
Tutorial Django + PythonMateus Padua
 
Python - Programando em alto nível
Python - Programando em alto nívelPython - Programando em alto nível
Python - Programando em alto nívelIgor Sobreira
 
PHP, Gearman e Memcache
PHP, Gearman e MemcachePHP, Gearman e Memcache
PHP, Gearman e MemcacheAndre Golvea
 
LabMM3 - Aula teórica 04
LabMM3 - Aula teórica 04LabMM3 - Aula teórica 04
LabMM3 - Aula teórica 04Carlos Santos
 

Semelhante a POCO C++ Libraries Overview (20)

Semana Acadêmica ICET - Feevale - 12/04/2014
Semana Acadêmica ICET - Feevale - 12/04/2014Semana Acadêmica ICET - Feevale - 12/04/2014
Semana Acadêmica ICET - Feevale - 12/04/2014
 
Canivete python
Canivete pythonCanivete python
Canivete python
 
Apresentação do Aplicativo de Controle de Combustíveis
Apresentação do Aplicativo de Controle de CombustíveisApresentação do Aplicativo de Controle de Combustíveis
Apresentação do Aplicativo de Controle de Combustíveis
 
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker Compose
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker ComposeScrum Gathering Rio 2016 - Conteinerizando Testes com Docker Compose
Scrum Gathering Rio 2016 - Conteinerizando Testes com Docker Compose
 
Colaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPColaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHP
 
Mean Stack
Mean StackMean Stack
Mean Stack
 
Cake php selecaodeprojetos-apres-em-modelo
Cake php selecaodeprojetos-apres-em-modeloCake php selecaodeprojetos-apres-em-modelo
Cake php selecaodeprojetos-apres-em-modelo
 
Otimizacao de websites em PHP
Otimizacao de websites em PHPOtimizacao de websites em PHP
Otimizacao de websites em PHP
 
Colaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHPColaboração em Projetos FLOSS: CakePHP
Colaboração em Projetos FLOSS: CakePHP
 
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...
Tchelinux live 2020 - Detectando e Respondendo Incidentes de Segurança em Fro...
 
Faça seu portal voar usando o plone.app.caching
Faça seu portal voar usando o plone.app.cachingFaça seu portal voar usando o plone.app.caching
Faça seu portal voar usando o plone.app.caching
 
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...
Detectando e Respondendo Incidentes de Segurança em Frontends Nginx utilizand...
 
Introdução à Programação Python e Tk
Introdução à Programação Python e TkIntrodução à Programação Python e Tk
Introdução à Programação Python e Tk
 
Cache em aplicações web
Cache em aplicações webCache em aplicações web
Cache em aplicações web
 
Tutorial Django + Python
Tutorial Django + PythonTutorial Django + Python
Tutorial Django + Python
 
Python, CPython, Pythonico, Cython
Python, CPython, Pythonico, CythonPython, CPython, Pythonico, Cython
Python, CPython, Pythonico, Cython
 
Python - Programando em alto nível
Python - Programando em alto nívelPython - Programando em alto nível
Python - Programando em alto nível
 
Bt4 H2HC6th
Bt4 H2HC6thBt4 H2HC6th
Bt4 H2HC6th
 
PHP, Gearman e Memcache
PHP, Gearman e MemcachePHP, Gearman e Memcache
PHP, Gearman e Memcache
 
LabMM3 - Aula teórica 04
LabMM3 - Aula teórica 04LabMM3 - Aula teórica 04
LabMM3 - Aula teórica 04
 

Mais de Uilian Ries

Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIUilian Ries
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersUilian Ries
 
Meetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioMeetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioUilian Ries
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package ManagerUilian Ries
 
Software Development Tools for C/C++
Software Development Tools for C/C++Software Development Tools for C/C++
Software Development Tools for C/C++Uilian Ries
 
Testes Unitários com GTest e Catch
Testes Unitários com GTest e CatchTestes Unitários com GTest e Catch
Testes Unitários com GTest e CatchUilian Ries
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++Uilian Ries
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Uilian Ries
 

Mais de Uilian Ries (11)

Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CI
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for Developers
 
Git Workflow
Git WorkflowGit Workflow
Git Workflow
 
BDD em Ação
BDD em AçãoBDD em Ação
BDD em Ação
 
Meetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioMeetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.io
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package Manager
 
Software Development Tools for C/C++
Software Development Tools for C/C++Software Development Tools for C/C++
Software Development Tools for C/C++
 
Testes Unitários com GTest e Catch
Testes Unitários com GTest e CatchTestes Unitários com GTest e Catch
Testes Unitários com GTest e Catch
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015
 

POCO C++ Libraries Overview

  • 2. ETHERNET SWITCHES SOLUTIONS AGENDA • O que é a POCO? • Onde é utilizada • POCO vs boost • Como usar no DmOS • Cache • Process • Filesystem • Application • Net • Notification • DirectoryWatcher • DynamicAny • String
  • 3. ETHERNET SWITCHES SOLUTIONS O que é a POCO? • POrtable COmponents; • Coleção de bibliotecas C++; • Focada em redes e aplicações; • Altamente portável; • Lançada em 2005, Áustria; • Atualmente com 120 colaboradores (github); • Compacta, ~8MB; • Documentação de fácil leitura; • Links: – http://pocoproject.org – http://pocoproject.org/documentation
  • 5. ETHERNET SWITCHES SOLUTIONS Onde é utilizada? • Sim, é utilizada em sistemas embarcados: – ACTIA Automotive ● Sistema de diagnóstico automotivo. – Schneider Electric ● Plataforma de automação embarcada. – StreamUnlimited ● Aplicação embarcada para TV. – Starticket ● Dispositivo para emissão de passagem.
  • 6. ETHERNET SWITCHES SOLUTIONS POCO vs boost • Poco é mais compreensível; • Mais fácil de aprender; • Possui suporte em pontos que a boost não dá: – SQLite; Process; Application; Directory Watch • Desvantagens: – Comunidade não tão ampla como a boost; – Documentação não extensa; – C++11 superou alguns pontos; – Boost cobre igualmente alguns pontos.
  • 7. ETHERNET SWITCHES SOLUTIONS Como usar no DmOS • Versão Debian: 1.3.6 (2009) • Versão atual: 1.7.3 (maio/2016) • Versão no DmOS: 1.6.1 (agosto/2015)
  • 8. ETHERNET SWITCHES SOLUTIONS Como usar no DmOS • dmos-dmtdd/dmptinfo/externals/ dmos-template/ dmos-template-test.json { “package”: “dmos-template-test”, “template”: “test”, “builddeps”: [ “poco” ] }
  • 9. ETHERNET SWITCHES SOLUTIONS Como usar no DmOS • CMake – find_package(Poco) → Ops! – FindPoco.cmake: ● https://goo.gl/kaveoK – PocoConfig.cmake (oficial): ● https://goo.gl/pbMJZD
  • 10. ETHERNET SWITCHES SOLUTIONS Como usar no DmOS • CMake – find_library(Poco_FOUNDATION PocoFoundation) – find_library(Poco_UTIL PocoUtil) – find_library(Poco_Net PocoNet) – find_library(Poco_DATA PocoData) – set(Poco_LIBRARIES ${Poco_FOUNDATION} ${Poco_UTIL} ${Poco_Net} ${Poco_DATA}) – target_link_libraries(${PROJECT_NAME} ${Poco_LIBRARIES}) – Bibliotecas com sufixo d são de DEBUG
  • 11. ETHERNET SWITCHES SOLUTIONS Como usar no DmOS • No Buildroot: Simbolo: BR2_PACKAGE_POCO Localização: → Target packages → Libraries → Other Filhas: BR2_PACKAGE_POCO_CRYPTO BR2_PACKAGE_POCO_DATA BR2_PACKAGE_POCO_UTIL BR2_PACKAGE_POCO_NET
  • 12. ETHERNET SWITCHES SOLUTIONS POCO Cache • Boost possui circular buffer e multi-index; • STL possui apenas seus containers; • Atualmente manipulamos LRU no braço; • Vantagem de utilizar Poco Cache: – Container com tamanho limitado; – Container com tempo limitado; – Notificação de alteração na cache; – Acesso ao item mais recente.
  • 13. ETHERNET SWITCHES SOLUTIONS POCO Cache – Least Recently Used #include "Poco/LRUCache.h" Poco::LRUCache<int, std::string> cache(3); cache.add(1, "Foo"); // |-1-| -> primeiro elemento MRU Poco::SharedPtr<std::string> elem = cache.get(1); // |-1-| cache.add(2, "Bar"); // |-2-1-| cache.add(3, "Qux"); // |-3-2-1-| // Insere novo elemento mais impopular : "Couse" cache.add(4, "Couse"); // |-4-3-2-| assert (*elem == "Foo"); // Conteúdo ainda válido elem = cache.get(2); // |-2-4-3-| // Substitui Bar por Baz cache.add(2, "Baz"); // 2 Eventos: Remove e Add
  • 14. ETHERNET SWITCHES SOLUTIONS POCO Cache – Time Based Expiration #include "Poco/ExpireCache.h" // Por padrão, expira TODOS OS VALORES após 10 min Poco::ExpireCache<int, std::string> cache; // Customização para 1 sec Poco::ExpireCache<int, std::string> cache(1000); • Uso de Poco::SharedPtr garante o tempo de vida da variável; • Cuidado ao combinar as funções has() e get() – Timeout entre as duas chamadas resulta em null pointer.
  • 15. ETHERNET SWITCHES SOLUTIONS POCO Cache – Time Based Expiration #include "Poco/UniqueExpireCache.h" #include “Poco/ExpirationDecorator.h” // Expira o tempo de vida POR ELEMENTO Poco::UniqueExpireCache<int, std::string> cache; // Utiliza pattern Decorator para inserção de elementos using ExprDecStr = Poco::ExpirationDecorator<std::string>; cache.add(1, ExprDecStr(“Foo”, 1000)); // 1 sec cache.add(2, ExprDecStr(“Bar”, 15000)); // 15 sec poco_assert (cache.size() == 2); std::this_thread::sleep_for(1s); assert (cache.size() == 1); Poco::SharedPtr<ExprDecStr> elem = cache.get(1); assert (elem.isNull());
  • 16. ETHERNET SWITCHES SOLUTIONS POCO Cache – Time Based Expiration #include "Poco/AccessExpireCache.h" #include “Poco/UniqueAccessExpireCache.h” // Expira o tempo de vida quando NÃO ACESSADO Poco::AccessExpireCache<int, std::string> cache; Poco::UniqueAccessExpireCache<int, std::string> cache; • Dicas: – Evitar usar has() com ExpireCache; – O arcabouço de Cache é extensível: Poco::AbstractStrategy; – Poco Cache utiliza std::map, para std::set, utilize Poco::Void como placeholder.
  • 17. ETHERNET SWITCHES SOLUTIONS POCO Process • Boost Process não é oficial; • STL não possui invocação de processos; • Atualmente usamos system, popen ou fork; • Vantagem de utilizar Poco Process: – Manipular o tempo de vida do processo de forma simples.
  • 18. ETHERNET SWITCHES SOLUTIONS POCO Process #include <Poco/Process.h> Poco::Process::Args args = {“--foo=true”}; Poco::ProcessHandle handle = Poco::Process::launch("dmos-example", args, "/bin"); // Do something .... Poco::Process::requestTermination(handle.id()); Poco::Process::wait(handle);
  • 19. ETHERNET SWITCHES SOLUTIONS POCO Filesystem • Boost filesystem é oficial; • STL receberá filesystem no C++17; • Atualmente usamos string para manipular path; • Vantagem de utilizar Poco File: – Manipulação de path (prefix, sufix, absolute, extension); – Iterar sobre diretórios; – Validar tipo de arquivo (is [directory | regular file]); – Buscar arquivo no sistema; – Validar acessos (can [write | read | exec]); Boost
  • 20. ETHERNET SWITCHES SOLUTIONS POCO Filesystem - Path #include <Poco/Path.h> Poco::Path mac("/sys/class/net/eth0/address"); assert(mac.isFile()); // file_name = "address" Poco::Path file_name = mac.getBaseName(); // dir_name = Poco::Path("/sys/class/net/eth0/"); Poco::Path dir_name = mac.parent(); std::string ss = file_name.toString();
  • 21. ETHERNET SWITCHES SOLUTIONS POCO Filesystem - Find #include <Poco/Path.h> #include <Poco/Environment.h> std::string process_name("dmos-example"); std::string path(Poco::Environment::get("PATH")); path += Poco::Path::pathSeparator(); path += "/dmpt/target/imported/bin"; Poco::Path process_path; assert(Poco::Path::find( path, process_name, process_path)); assert(process_path.isFile());
  • 22. ETHERNET SWITCHES SOLUTIONS POCO Filesystem - File #include <Poco/Path.h> #include <Poco/File.h> #include <Poco/Timestamp.h> Poco::Path mac("/sys/class/net/eth0/address"); Poco::File mac_fd(mac); assert(mac_fd.isFile()); assert(mac_fd.canRead()); assert(!mac_fd.canWrite()); mac_fd.copyTo("/tmp"); Poco::Timestamp mac_date = mac_fd.created();
  • 23. ETHERNET SWITCHES SOLUTIONS POCO Filesystem – Directory Iterator #include <Poco/Path.h> #include <Poco/DirectoryIterator.h> Poco::Path cwd(Path::current()); Poco::DirectoryIterator it(cwd); Poco::DirectoryIterator end; while (it != end) { if (it->isFile()) { assert(it->canRead()); } ++it; }
  • 24. ETHERNET SWITCHES SOLUTIONS POCO Filesystem – Glob #include <Poco/Glob.h> #include <Poco/File.h> std::set<std::string> files; Poco::Glob::glob("/usr/local/include/*/*.h", files); for (const auto& it : files) { Poco::File file(it); assert(file.isFile()); assert(file.canRead()); }
  • 25. ETHERNET SWITCHES SOLUTIONS POCO Application • Boost Application não é oficial; • STL não possui pattern para aplicação; • Atualmente usamos main e nada mais; • Vantagem de utilizar Poco Application: – Manter um padrão para construção da main; – Manipulação de parâmetros de graça; – Manipulação de arquivo de configuração de graça; – Logger de graça; – Estender sub-sistemas como filhas;
  • 26. ETHERNET SWITCHES SOLUTIONS POCO Application – Subsystem // Retorna o nome do aplicativo. const char* name() const // Inicializa o sub-sistema void initialize(Poco::Util::Application& app); // Prepara o sub-sistema para morrer void uninitialize(Poco::Util::Application& app); // Reconfigura o sub-sistema void reinitialize(Poco::Util::Application& app);
  • 27. ETHERNET SWITCHES SOLUTIONS POCO Application – Main class FooApp : public Poco::ServerApplication { int main(const Poco::ArgVec& args) { Poco::TaskManager task_manager; task_manager.start(new FooTask); WaitForTerminationRequest(); // Aguarda SIGTERM task_manager.cancelAll(); task_manager.joinAll(); return Poco::Util::Application::EXIT_OK; } };
  • 28. ETHERNET SWITCHES SOLUTIONS POCO Application – Handle Options class FooApp : public Poco::ServerApplication { void defineOptions(OptionSet& options) { Application::defineOptions(options); options.addOption( Option("help", "h", "display help information") .required(false) .repeatable(false) .callback(OptionCallback<FooApp>(this, &FooApp::handleHelp))); } };
  • 29. ETHERNET SWITCHES SOLUTIONS POCO Application – Configuration File class FooApp : public Poco::ServerApplication { void initialize(Poco::Application& self ) { loadConfiguration(); if (config().hasOption(“serveruri”)) { std::string ss = config().getString(“serveruri”); } Poco::Util::Application::initialize(self ); } };
  • 30. ETHERNET SWITCHES SOLUTIONS POCO Application – Logger class FooApp : public Poco::ServerApplication { void uninitialize(Poco::Application& self) { logger().debug(“Finish Foo Application”); dmmw_finish(&dmmw); Poco::Util::Application::uninitialize(self); } };
  • 31. ETHERNET SWITCHES SOLUTIONS POCO Net • Boost ASIO é oficial porém “complexa”; • STL networking possui proposta para C++17; • Atualmente usamos nativo; • Vantagem de utilizar Poco Net: – Manipular interface de rede (ip, mac); – Socket (client/server); – Conexão (Design Pattern Reactor); – HTTP (client/server); – TCP (client/server); – FTP (client/server).
  • 32. ETHERNET SWITCHES SOLUTIONS POCO Net – Socket TCP #include <Poco/Net/SocketAddress.h> #include <Poco/Net/StreamSocket.h> #include <Poco/Net/SocketStream.h> #include <Poco/StreamCopier.h> Poco::Net::SocketAddress sa("datacom.ind.br", 80); Poco::Net::StreamSocket socket(sa); Poco::Net::SocketStream str(socket); str << "GET / HTTP/1.1rn” "Host: datacom.ind.brrn" "rn"; str.flush(); Poco::StreamCopier::copyStream(str, std::cout);
  • 33. ETHERNET SWITCHES SOLUTIONS POCO Net – Network Interface #include <Poco/Net/NetworkInterface.h> using Poco::Net::NetworkInterface; NetworkInterface::NetworkInterfaceList interface_list = Poco::Net::NetworkInterface::list(); for (const auto& it : interface_list) { printf(“%s - %sn”, it.name(), it.address().toString()); printf("%02X:%02X:%02X:%02X:%02X:02Xn", it.macAddress()[0], it.macAddress()[1], it.macAddress()[2], it.macAddress()[3], it.macAddress()[4], it.macAddress()[5]); }
  • 34. ETHERNET SWITCHES SOLUTIONS POCO Notification & Events • Boost possui signals2; • STL possui function object; • Atualmente usamos std::function, functor ou ponteiro; • Vantagem de utilizar Poco Delegate: – Pattern Observer;
  • 35. ETHERNET SWITCHES SOLUTIONS POCO Notification & Events - Delegate #include <Poco/BasicEvent.h> class Subject { public: Poco::BasicEvent<int> event; void Notify(int n){ event.notify(this, n); } };
  • 36. ETHERNET SWITCHES SOLUTIONS POCO Notification & Events - Delegate class Observer { public: void OnEvent(const void* pSender, int& arg) { assert(arg != -1); } };
  • 37. ETHERNET SWITCHES SOLUTIONS POCO Notification & Events - Delegate #include <Poco/Delegate.h> Subject subject; std::array<Observer, 3> observers; for (auto& observer : observers) { // Cadastra subject.event += Poco::delegate(&observer, &Observer::OnEvent); } subject.Notify(42); // Notifica todos os observadores for (auto& observer : observers) { // Remove subject.event -= Poco::delegate(&observer, &Observer::OnEvent); }
  • 38. ETHERNET SWITCHES SOLUTIONS POCO DirectoryWatcher • Boost dir_monitor não é oficial; • STL não possui suporte a eventos por arquivo; • Atualmente usamos inotify; • Vantagem de utilizar Poco DirectoryWatcher: – Assistir mudanças em diretórios (não bloqueante); – Manipulação mais fácil do que inotify.
  • 39. ETHERNET SWITCHES SOLUTIONS POCO DirectoryWatcher #include <Poco/DirectoryWatcher.h> DirectoryWatcher watcher(“/var/log”, DW_FILTER_ENABLE_ALL, DW_DEFAULT_SCAN_INTERVAL); watcher.itemModified += Poco::Delegate(&target, Target::OnEvent); Target::OnEvent(const DirectoryEvent& event) { assert(event.f == “/var/log/syslog”); }
  • 40. ETHERNET SWITCHES SOLUTIONS POCO DynamicAny • Boost possui any; • STL integrará fundamentals TS no C++17; • Atualmente não usamos este conceito; • Vantagem de utilizar Poco DynamicAny: – Utilizar container dinâmico para tipos; – Converter sem precisar saber o tipo original.
  • 41. ETHERNET SWITCHES SOLUTIONS POCO DynamicAny – boost::any #include <boost/any.hpp> using any_vector = std::vector<boost::any>; any_vector foo = { 1, "foobar", true, 42.3 }; for (const auto& it : foo) { std::cout << boost::any_cast<std::string>(it); } terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::er ror_info_injector<boost::bad_any_cast> >' what(): boost::bad_any_cast: failed conversion using boost::any_cast
  • 42. ETHERNET SWITCHES SOLUTIONS POCO DynamicAny #include <Poco/DynamicAny.h> using dynamic_vector = std::vector<Poco::DynamicAny>; dynamic_vector dyn_vec = { "foo", 42, 3.14 }; for (const auto& it : dyn_vec) { std::cout << it.convert<std::string>() << std::endl; }
  • 43. ETHERNET SWITCHES SOLUTIONS POCO String • Boost possui String Algorithms; • STL possui apenas algorithms; • Atualmente usamos boost e STL; • Vantagem de utilizar Poco String: – Atalho para algoritmos para string; – Sobre boost, não tem vantagens; – Economia de fosfato.
  • 44. ETHERNET SWITCHES SOLUTIONS POCO String – Compare ignore case • STL #include <algorithm> std::equal(left.begin(), left.end(), right.begin(), [](std::string::value_type a, std::string::value_type b) { return std::tolower(a) == std::tolower(b); }); • Poco #include <Poco/String.h> Poco::icompare(str1, str2); • Boost #include <boost/algorithm/string.hpp> boost::iequals(str1, str2);
  • 45. ETHERNET SWITCHES SOLUTIONS POCO String – To lower case • STL #include <algorithm> std::transform(str.begin(), str.end(), str.begin(), ::tolower); • Poco #include <Poco/String.h> Poco::toLower(str); • Boost #include <boost/algorithm/string.hpp> boost::algorithm::to_lower(str); boost::algorithm::to_lower_copy(str);
  • 46. ETHERNET SWITCHES SOLUTIONS POCO String – Trim • STL #include <algorithm> str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); • Poco #include <Poco/String.h> Poco::trim(str); • Boost #include <boost/algorithm/string.hpp> boost::erase_all::(str, “ ”);
  • 47. ETHERNET SWITCHES SOLUTIONS POCO String – Split • STL #include <algorithm> std::vector<std::string> tokens; std::istringstream iss(str); std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter(tokens)); • Poco #include <Poco/StringTokenizer.h> Poco::StringTokenizer(“foo bar qux baz couse” ,“ ”); • Boost #include <boost/algorithm/string.hpp> std::vector<std::string> tokens; boost::split(tokens, "string to split", boost::is_any_of(" "));
  • 48. ETHERNET SWITCHES SOLUTIONS ● http://pocoproject.org/ ● http://pocoproject.org/documentation ● https://github.com/pocoproject/poco