Since Tufão 0.4, I’ve been using CMake as the Tufão build system, but occasionally I see some users reimplementing the qmake-based project files and I thought it’d be a good idea to explain/document why such rewrite is a bad idea. This is the post.
Simply and clear (1 reason)
It means *nothing* to your qmake-based project.
What your qmake-based project needs is a *.pri file to you include in your *.pro file. And such *.pri file *is* generated (and properly included in your Qt-installation) by Tufão. You’ll just write the usual “CONFIG += TUFAO” without *any* pain.
Why won’t I use qmake in Tufão ever again (long answer)
Two reasons why is it a bad idea:
- You define only one target per file. You need subdirs. It’s hard.
- The Tufão unit testing based on Qt Test requires the definition of separate executables per test and the “src/tests/CMakeLists.txt” CMake file beautifully defines 13 tests. And with the CMake-based system, all you need to do to add a new test is add a single line to the previously mentioned file. QMake is so hard that I’d define a dumb test system that only works after you install the lib, just to free myself from the qmake-pain.
- There is no easy way to preprocess files.
- And if you use external commands that you don’t provide yourself like grep, sed or whatever, then your build system will be less portable than autotools. Not every Windows developer likes autotools and your approach (external commands) won’t be any better.
- All in all, it becomes hard to write a build system that will install the files required by projects that use QMake, CMake or PKG-CONFIG (Tufão supports all three).
The reasons above are the important reasons, but there are others like the fact that documentation is almost as bad as the documentation for creation of QtCreator plugins.
The ever growing distance from QMake
When Tufão grows, sometimes the build system becomes more complicated/demanding, and when it happens, I bet the QMake-based approach will become even more difficult to maintain. The most recent case of libtufao.pro that I’m aware of had to include some underdocumented black magic like the following just to meet the Qt4/5 Tufão 0.x demand:
You like a CMake-based Tufão
The current CMake-based build system of Tufão provides features that you certainly enjoy. At least the greater stability of the new unit testing framework requires CMake and you certainly want a stable library.
In the beginning, QMake met Tufão requirements of a build system, but I wouldn’t use it again for demanding projects.
But I don’t hate QMake and I’d use it again in a Qt-based project if, and only if, I *know* it won’t have demanding needs.
Of course I’ll hate QMake if people start to overuse it (and creating me trouble).
And if you still wants to maintain a QMake-based Tufão project file, at least you’ve been warned about the *pain*
and inferior solution you’ll end up with.
Espero mostrar rapidamente o histórico do projeto e usar o tempo restante para a motivação, reservando 10 minutos ao final para perguntas. A motivação deve incluir uma pequena explicação de como funciona a web e seus principais pontos, comparações, becnhmarks (esse é um item mais difícil), limitações, exemplos e um “tutorial”.
Devo preparar uma nova aplicação de exemplo que explora características presentes no Tufão.
After a long time developing Tufão, it finally reached 1.0 version some hours ago. I’ve spent a lot of time cleaning up the API and exploring the features provided by C++11 and Qt5 to release this version.
This is the first Tufão release that:
- … breaks config files, so you’ll need to update your config files to use them with the new version
- … breaks ABI, so you’ll need to recompile your projects to use the new version
- … breaks API, so you’ll need to change your source code to be able to recompile your previous code and use the new version
- … breaks previous documented behaviour, so you’ll need to change the way you use features that were available before. But don’t worry, because the list of these changes are really small and are well documented below.
Porting to Tufão 1.0
From now on, you should link against tufao1 instead of tufao. The PKGCONFIG, qmake and CMake files were renamed also, so you can have different Tufão libraries in the same system if their major version differs.
The list of behavioural changes are:
- Headers are being stored using a Hash-table, so you can’t easily predict (and shouldn’t) the order of the headers anymore. I hope this change will improve the performance.
- HttpServerRequest::ready signal auto-disconnects all slots connected to the HttpServerRequest::data and HttpServerRequest::end slots before being emitted.
- HttpFileServer can automatically detect the mime type from the served files, so if you had your own code logic to handle the mimes, you should throw it away and enjoy a lesser code base to maintain.
The list of changes:
- The project finally have a logo (made by me in Inkscape)
- Deprecated API was removed
- Url and QueryString removed in favor of QUrl
- Headers refactored to inherit from QMultiHash instead of QMultiMap
- Constructor’s options argument is optional now
- setOptions method added
- Constructor takes a reference to a QIODevice instead a pointer
- Constructor takes a reference to a QAbstractSocket instead a pointer
- socket method returns a reference instead a pointer
- url returns a QUrl
- data signal was changed and you must use readBody method to access body’s content
- the upgrade’s head data is accessed from the request body from now on
- now the object auto-disconnects slots from data and end signals right before emit ready
- setCustomData and customData methods added
- Now HttpServerRequestRouter use these methods to pass the list of captured texts
- HttpServer uses reference instead of pointers in several places
- AbstractHttpServerRequestRouter refactored to explore lambdas features
- Tufão’s plugin system fully refactored
- It’s using JSON files as configuration
- It uses references instead pointers
- It receives 2 arguments instead of 3
- One more abstraction to sessions created to explore lambdas
- startServerHandshake is taking references instead pointers
- LESS POINTERS and MORE REFERENCES
- This change exposes a model more predictive and natural
- I’m caring less about Qt style and more about C++ style
- But don’t worry, I’ll maintain a balance
- Using scoped enums
- HttpFileServer uses/sends mime info
- Interfaces don’t inherit from QObject anymore, so you can use multiple inheritance to make the same class implement many interfaces
- HttpUpgradeRouter introduced
- HttpServer::setUpgradeHandler also
- Updated QtCreator plugin to work with QtCreator 2.7.0 and Qt 5
I want to improve the Tufão’s stability and performance, so now I’ll focus on a minor relase with unit testing and some minor changes.
After Tufão 1.0.1, I’ll focus on BlowThemAll.
You can see a visualization video based on the history of commits below.
This release deserves a wallpaper, so I made one. See it below:
You can download the source of the previous wallpaper here.
What are you still doing here? Go download the new version right now!
Sou uma pessoa que simpatiza com jogos e, ultimamente, estive trabalhando em um jogo orientado a disputas multiplayer pertencente ao gênero Bomberman. Para implementar a arquitetura de jogo sugerida pelo time de desenvolvimento, desenvolvi algumas bibliotecas auxiliares. E nesse post vou documentar como combinar as duas bibliotecas para permitir chamadas de métodos remotas (RPC).
A primeira biblioteca que desenvolvi para o projeto foi batizada de Phobos e sua única função é implementar o protocolo JSON-RPC 2.0. Meu objetivo principal durante seu desenvolvimento foi auxiliar o desenvolvimento do BlowThemAll e, assim sendo, alguns fatores, como arquitetura e documentação, foram deixados de lado. O projeto não tem página própria na internet e as “releases” do mesmo encontram-se na página de downloads do BlowThemAll.
Há um segundo projeto que iniciei, batizado de Tufão. É uma biblioteca desenvolvida em cima do Qt que possui uma API similar ao Node.js que pode ser usada para criar aplicações web. Desenvolvi esse projeto de forma bem disciplinada, me dedicando bastante, seja ao projetar sua arquitetura, seja ao escrever sua documentação, seja durante os testes, empacotamento para diferentes plataformas, preparação de uma página decente, criação de exemplos e documentação por fora… Caso queira saber mais sobre o mesmo, visite a página do projeto.
Esse texto assume que você está utilizando (e tem um pouco de familiaridade com) o QtCreator para gerenciar seus projetos e tem uma boa noção de orientação a objetos. Além disso, esse texto assume que você já instalou o Tufão no seu sistema.
Preparando um novo projeto
Então, usando o plugin do Tufão para o QtCreator, crie um novo projeto usando o template “application“.
Com o projeto criado, extraia o conteúdo dos arquivos baixados para a pasta do projeto e edite o arquivo *.pro do mesmo, de forma que as seguintes linhas sejam adicionadas:
Pronto, você concluiu as etapas iniciais para trabalhar com um projeto usando JSON-RPC 2.0 em C++. A etapa seguinte dependerá se o projeto criado é o servidor ou o cliente.
Preparando um novo projeto (servidor)
Grande parte da “aplicação web” já está pronta, utilizando o “esqueleto” do projeto genérico criado pelo plugin do Tufão. As alterações que você precisa fazer se resumem a criar uma classe para tratar conexões WebSocket e uma classe Handler para ser utilizada pelo RPC.
Comece criando a classe WebServer, que deve herdar de Tufao::HttpServer e deve implementar o método protegido upgrade, que deve ficar assim:
Implemente a classe Handler herdando de QObject e utilizando a macro Q_INVOKABLE na frente da declaração/assinatura dos métodos que você deseja exportar para o cliente remoto.
O último passo é editar o arquivo main.cpp, para que o mesmo utilize a classe WebServer criada. Para isso, só precisa mudar o tipo da variável server dentro da função main, de Tufao::HttpServer para WebServer.
Preparando um novo projeto (cliente)
O cliente tem uma natureza bem diferente do esqueleto genérico de aplicação criado pelo plugin do Tufão e isso requer que você faça mais mudanças que no caso do servidor.
Primeiro, apague as classes (desnecessários para o projeto) NotFound e PluginReloader. Apague também o arquivo de recursos static.qrc e seu recurso associado, notfound.html.
Segundo, apague quase todo o conteúdo da função main, para que se resuma ao seguinte:
Com isso terminaram as remoções necessários do projeto-base. Agora restam as adições, que se dividem em duas partes, relacionadas ao Tufão e relacionadas ao Phobos. Irei comentar o mínimo possível sobre o Tufão, pois o mesmo está bem documentado e torna-se desnecessário que eu faça comentários extras.
Começando pela parte do Tufão, você precisará criar um objeto da classe Tufao::WebSocket e chamar o método connectToHost. Quando o sinal connected for emitido, o objeto estará pronto para ser utilizado.
Do lado do Phobos, você precisará, assim como no servidor, de um objeto Handler, que herde da classe QObject e utilize a macro Q_INVOKABLE na frente da declaração/assinatura dos métodos que você deseja exportar para o servidor remoto (o protocolo JSON-RPC não distingue servidor de cliente e, uma vez iniciada a conexão, ambos podem realizar chamadas remotas, enquanto a camada de transporte permitir).
Invocando métodos remotos
Para chamar métodos remotos, utilize o método JsonRpc::callWith. Os dois primeiros argumentos que ele recebe lembram o método singleShot da classe QTimer, com a diferença que a macro SLOT não cerca o argumento member no método callWith. Os argumentos restantes representam, respectivamente, o nome do método remoto e seus argumentos. Exemplificando:
Você pode obter o código-fonte das aplicações desenvolvidas nesse texto aqui.
Tufão is a tool to work with web in C++. Why C++? I like C++ and this is enough to me, but there are other reasons to create a web framework for C++. First, computing is changing again. In the last years, the clock speed of the processors stalled and again, you need to care about performance. Not only that, but the nature of techniques to gain performance are changing too. Today, you find processor with more and more cores, you can see more general purpose enabled GPUs and there are more optimized instruction sets in processors. C++ is the right tool for the job, and with the language update in the last year, I believe that the language is becoming stronger. Second, I don’t see a de facto standard to create web applications using C++. Third, there are lots of good libraries and APIs in C++ that you can use to implement cool ideas and develop web apps in C++ would let you to use them.
So, instead of porting a not decent API to create a C++ web framework, I choose Node.js to create the API core. I believe that C++ programmers love controlness, so they will like the API’s power. Besides the core, I’m implementing other facilites to help in the use of application servers, file upload, forms parsing, WebSocket, WebRTC and other modern standards, web services, …
How does Tufão compare to other frameworks? In this post, I’ll compare it through some different points of view.
The programming language
My idea about programming language is that languages should be designed to express ideas, and if they don’t have a good expressive power, then they are useless. Today we have lots and lots of programming languages. Personally, I don’t like the direction the Java language is taking, but, on the other side, I like Python. I already see some languages created only to develop web applications. I don’t believe that this is a good move. I don’t believe that the web development’s problem is the lack of an appropriate language. There are lots of good languages available today and I don’t believe that create new languages is necessary.
I’m developing Tufão in C++. C++ has a good expressive power and the last year’s update improved it. The language isn’t popular among web development, but web development is very dynamic and change rapidly from time to time. How I said before, the problem, in my opinion, is not in the lack of an appropriate language, but in the lack of an appropriate framework.
Tufão can have extra modules as any other framework, but compared to Node.js, I see a problem with ease of the installation process. In Node.js, we have npm and all we need to do is npm install <module>, but in Tufão you need rely in the operating system installation process or compile the modules. This isn’t a problem in some GNU/Linux distributions like ArchLinux, but this might not be the case in other systems.
I don’t see an ease way to solve this problem, so I’ll try to evolve Tufão to be self-sufficient in most of the cases. And this isn’t really difficult as Tufão is built on top of Qt, that already provides lots and lots of common needed features to any application.
In Tufão, the rounting is built on top of two classes. The AbstractHttpServerRequestHandler interface and the HttpServerRequestRouter class. The AbstractHttpServerRequestHandler interface requires you to create a slot to handle a request and the HttpServerRequestRouter is itself is an AbstractHttpServerRequestHandler. This design let you to build things like nested routing and use the same router in several different combinations (serving to HTTP and HTTPS servers).
In Tufão, the basic building block for routing request is the same as several other frameworks. You just need to use regular expression to filter the path and pass a handler to that request, and, optionally, specify which methods the object will handle.
The plugin-based application server
Well, when you develop a web application, you want to do it available 24h per day, every day. Imagine how you’d feel if you had to stop the server, recompile the application and run it again just to add your company’s name at the bottom of the page.
We could just use a dynamic interpreted language and solve the problem, but the language isn’t the problem. In some web frameworks there is the notion of an application server, that usually is integrated in the routing mechanism. The role of the application server is to dynamically register new applications and remove old ones.
In C++, a dynamic handler is just a plugin stored in a shared library loaded at runtime by the application. It’s just the old *.dll files on Windows or *.so on Linux. Tufão provides an application server too, but, different from most of the frameworks I’ve seen, it’s not melted in the routing mechanism.
The Tufão application server is plugin-based, stores its configuration in a file (chosen by you) and provides an external program to update this file. It’s easy to use, but you’ll have to add code to define when the application server must reload the config file. If you don’t have any special requeriments, the default application template code is enough and you won’t need to write this piece of code at all.
Some frameworks just don’t separate logic and ui design, such as most of old PHP, embedding the logic code and the html (or some custom view language code) code in the same file.
There are frameworks that allow you separate the logic and the ui design, but do a wrong implementation of the idea, requiring the designer to have some knowledge about the framework internals, trapping you to use the tools from the same framework forever.
Using this technique, when the user access your site, the browser will request the pages server through the Tufão’s static file server, making most part of the page cacheable, saving precious network traffic from your application . Also, the view isn’t generated at runtime, so you will save server’s processing power too, increasing the maximum numbers of requests per second.
Another advantage is the freedom to choose technologies. The UI is just a bunch of static files, isn’t something chained to specific Tufão internals, so you can use any tool you want to create them. We don’t take away your freedom.
As an advice, you should implement each page handler as a plugin, so you can change the running code at runtime.
Web frameworks usually don’t develop custom IDEs and this isn’t an exception for Tufão. But, Tufão is built on top of Qt, and Qt programmers are used to QtCreator, so I created a QtCreator plugin to help the development of Tufão applications. The plugin just help to create new applications, providing application templates. I think this is enough, but if I change my mind, I’ll extend the plugin.
There are 3 application templates currently:
- The first is a full featured web application, using the Tufão’s static file server, plugin server and a custom handler to treat 404-pages.
- The second is a plugin that responds with a “Hello World” and is as small as possible to keep things simple.
- The last is just a Hello World application.
Well, It’s not an easy task to measure the development productivity, but I can talk about some points. If what you want is a creative web app, you problably need a powerfull low-limited web API to help you, and my aim is to provide such API, so I highly invite you to test Tufão and give me some feedback.
If you plan to create some common-case application, there are frameworks such as Django that already provide several integrated facilities (such as mvc with integrated mapping of database schemas, template systems, already implemented authentacion sytems, … …) and right now, Tufão can only compete if you use other libraries to help you.
Improve Tufão to compete with Django isn’t my priority and I don’t even know if I care about facilitate the creation process of boring common projects, but is an important area and if the demand or the help increase, I’ll create tools to help in this use case too.
Tufão is an open source project under the LGPL license. The documentation, examples and public headers are under the MIT license. This means that anyone can contribute to Tufão development and you can use it in your commercial projects.
I created Tufão all alone and tried to do I really good job in every piece of it, but maybe the documentation isn’t good enough, so if you have any question that cannot be solved reading the documentation, just ask me and in the next version the documentation might be better. The same rule applies to bugs (and I wrote lots of tests to prevent it, but I just didn’t realize how to automate all tests, like little endian and big endian tests…).
In the next part of this article, I’ll demonstrate some benchmarks and how to create a web application using as much Tufão’s features as possible. Thank you for reading about the project, I really appreciate, and if you like the project, spread it among your friends.
I ackowledge Ryan Lienhart Dahl for developing Node.js. Before its API, I thought web development was tedious.
Hello good fellows, I’m not used to write posts in english, but this is a habit that I must change. Well, just today I’m releasing a new version of my C++/Qt web framework, Tufão.
For the newcomers, Tufão was, initially, a port of the excellent Node.js API to C++ built on top of Qt. It was a good choice because the Node.js assynchronous API fits well in Qt’s object communication system (signals & slots).
The new 0.2 version brings the following changes:
- Code is more stable
- Documentation a bit improved
- HttpServerResponse class is easier to use (stream operator)
- WebSocket support
- Added TUFAO_VERSION_MAJOR and TUFAO_VERSION_MINOR macros
- A QtCreator plugin
- New examples
The most problematic areas of Tufão now is its lack of:
- An application server
- A file server
In future versions these gaps will be filled. For more details take a look at the project roadmap in the README file.
I welcome everyone to compile, test and use Tufão.
Como vários de vocês não sabem, eu não passo de um preguiçoso movido pelo tédio. Ultimamente, venho aperfeiçoando o projeto do qual falei no meu último post (o Tufão), que visa eliminar o vazio do desenvolvimento web que há para C++ utilizando a framework Qt como base. Após muito trabalho e dedicação o Tufão passou na bateria de testes e está implementando toda a API proposta, que se iguala a API oferecida pelo Node.js. Assim sendo, estou anunciando o lançamento de sua primeira versão estável, a versão 0.1.
Vocês podem baixar essa versão na página do Tufão no googlecode e eu vos convido a testarem um dos exemplos que acompanham o Tufão.
O código anterior poderá ser melhorado no Qt5, que fará um melhor uso de funcionalidades encontradas no novo padrão do C++, como o uso de lambdas. Para aqueles que ficaram curiosos, recomendo esse link.
De agora em diante, estarei trabalhando nas seguintes funcionalidades:
- Plugins para o QtCreator
- Servidor de aplicações baseado em plugins
- Suporte a WebSocket
- Suporte mais fácil a multithread (atualmente já é possível utilizar threads de forma eficiente no Tufão para tirar proveito de várias CPUs)
- Descoberta e descrição de serviços
- Implementação de alguns RPCs comuns
- Backend HTML5 para o Qt