Tuesday, May 7, 2013

Creating a REST web service using webtoolkit (aka witty)

I previously introduced witty as a powerful web framework for C++ developers, mostly those one that don't want to re-invent the wheel and become productive.  Witty uses a resource-based approach to register services that listen on specific paths (resource paths), these resources are registered on the witty server first and the server is started later.

So the steps of an standalone witty w/ custom resource server:

  1. create a server
  2. register a custom resource on a desired path
  3. start the server, hence its resources
  4. wait for server shutdown signal
  5. stop the server

Here is the snippet of the main.cpp,  ignore all log-related plumbing:


#include <Wt/WServer>
#include <iostream>
#include "MyResource.h"
#include "log.h"

using namespace std;
using namespace Wt;

int main(int argc, char **argv) {
 WLogger logger;
 configLogger(logger);
 try {
  WServer server(argv[0], "");
  try {
   server.setServerConfiguration(argc, argv);
   MyResource dr;
   server.addResource(&dr, "/resource");
   info(logger, "Starting resource server.");
   if (server.start()) {
    WServer::waitForShutdown();
    server.stop();
   } else {
    fatal(logger, "Fatal error starting resource server.");
    return 1;
   }
   return 0;
  } catch (std::exception& e) {
   fatal(logger, "Fatal error starting resource server.", e.what());
   return 1;
  }
 } catch (WServer::Exception& e) {
  fatal(logger, "Fatal error creating WServer.", e.what());
  return 1;
 } catch (exception& e) {
  fatal(logger, "Fatal error occurred.", e.what());
  return 1;
 }
}

You may notice the creation and registration of MyResource, this is place where you provide the service implementation, specifically on the handleRequest() method, take a look at MyResource.h:

#ifndef MYRESOURCE_H_
#define MYRESOURCE_H_

#include <Wt/WResource>

using namespace Wt;
using namespace Wt::Http;

class MyResource: public WResource {
 public:
  MyResource();
  virtual ~MyResource();
 protected:
  virtual void handleRequest(const Request &request, Response &response);
};

#endif /* MYPRESOURCE_H_ */

The implemantation is very trivial, it's just an echo of the request content also including some extra data like content type, content length and request method: GET, POST, etc. Enjoy MyResource.cpp:
#include <iostream>
#include <Wt/Http/Response>
#include "MyResource.h"

using namespace std;
using namespace Wt::Http;

MyResource::MyResource() {
}

MyResource::~MyResource() {
}

void MyResource::handleRequest(const Request& request, Response& response) {
 string method = request.method();
 string contentType = request.contentType();
 int contentLength = request.contentLength();
 char* buffer = new char[contentLength + 1];
 request.in().read(buffer, contentLength);
 buffer[contentLength] = 0;
 response.setMimeType("application/xml");
 ostream& out = response.out();
 out << "<?xml version='1.0' encoding='utf-8' ?>" << endl;
 out << "<reply>" << endl;
 out << "<method>" << method << "</method>" << endl; 
 out << "<contentType>" << contentType << "</contentType>" << endl;
 out << "<contentLenght>" << contentLength << "</contentLenght>" << endl;
 out << "<body>" << buffer << "</body>" << endl;
 out << "</reply>";
 delete[] buffer;
}


The reamining stuff is just plumbing as I said before, here you got log.h:
#ifndef LOG_H_
#define LOG_H_

#include <Wt/WLogger>

using namespace std;
using namespace Wt;

void info(WLogger& logger, const string& message);
void fatal(WLogger& logger, const string& message, const char* what);
void fatal(WLogger& logger, const string& message);
void configLogger(WLogger& logger);

#endif /* LOG_H_ */

And log.cpp:
#include "log.h"
#include <iostream>

void info(WLogger& logger, const string& message) {
 WLogEntry entry = logger.entry("info");
 entry << WLogger::timestamp << WLogger::sep << WLogger::sep << '[' << "info"
   << ']' << WLogger::sep << message;
}
void fatal(WLogger& logger, const string& message, const char* what) {
 WLogEntry entry = logger.entry("fatal");
 entry << WLogger::timestamp << WLogger::sep << WLogger::sep << '['
  << "fatal" << ']' << WLogger::sep << message << what;
}

void fatal(WLogger& logger, const string& message) {
 fatal(logger, message, "");
}

void configLogger(WLogger& logger) {
 logger.addField("datetime", false);
 logger.addField("type", false);
 logger.addField("message", true);
 logger.setFile("/var/log/resource.log");
}

Now compile an run the server, here in this case using standalone server mode instead of fast cgi mode, but also works w/ fast cgi variant:
$ g++ log.cpp MyResource.cpp main.cpp -lwthttp -oresource

Run the server:
$ ./resource --http-address 0.0.0.0 --http-port 80 --docroot=.
INFO: Opened log file (/var/log/resource.log).
[2013-May-07 19:52:11.046985] 9658 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = './resource')"
[2013-May-07 19:52:11.047757] 9658 - [info] "WServer/wthttp: initializing built-in wthttpd"
[2013-May-07 19:52:11.048027] 9658 - [info] "wthttp: started server: http://0.0.0.0:80"

Finally test the service using a simple call:
$ curl -X POST -H "Content-Type: application/xml" -d"<payload>PAYLOAD GOES HERE!</payload>" http://localhost/resource
<?xml version='1.0' encoding='utf-8' ?>
<reply>
<method>POST</method>
<contentType>application/xml</contentType>
<contentLenght>37</contentLenght>
<body><payload>PAYLOAD GOES HERE!</payload></body>
</reply>
Very simple as you can see!

Thursday, May 2, 2013

ImageMagick convert to raw RGB format in C++

This is a code snippet to convert from any supported ImageMagick format to raw RGB format, it's just an example on how to use the conversion features of ImageMagick libraries. The example was tested on both: Windows and Linux.

#include <Magick++.h>
#include <iostream>
#include <boost/filesystem.hpp>

using namespace std;
using namespace Magick;
using namespace boost::filesystem;

int main(int argc, char **argv) {
 Image img;
 string src_path;
 string tgt_path;
 Blob blob;
 FILE* tgt_file;

 InitializeMagick(*argv);

 // read image
 src_path = argv[1];
 if(exists(src_path)) {
  img.read(src_path);
  cout << "Detected format: " << img.format() << endl;

  // set raw RGBS output format & convert it into a Blob
  img.magick("RGB");
  img.write(&blob);

  // dump blob to disk
  tgt_path = src_path + ".RGB.raw";
  tgt_file = fopen(tgt_path.c_str(), "wb");
  fwrite(blob.data(), blob.length(), 1, tgt_file);
  fclose(tgt_file);
  cout << "Converted to raw RGB: " << tgt_path << endl;
  exit(0);
 } else {
  cout << "Could not load image, file not found " << src_path << endl;
  exit(1);
 }
}

Recording dialog-based interactive user input using expect on Linux

This is a how to successfully combine the shell script dialog tool with expect in order to record all user interactions and repeat them later. You can apply expect to record almost anything on a terminal, I applied it on a very complex dialog-based installation wizard with 100% success. There's nothing special here, nor a magic spell. The solution is pretty straightforward: start recording, launch dialogs + interact, stop record and finally replay it on a hit.

Show the example dialogs below:

#!/bin/bash
#
# dlgs.sh : all dialogs in one script, 1- a radio list, 2- a Yes/No dialog, 3-An input box


  
radiolist_result=$(dialog --stdout --clear --backtitle "Combining dialog w/ expect" \
   --radiolist "Select distro:" 10 40 3 1 "CentOS" off 2 "Ubuntu" on 3 "Debian" off)

dialog --stdout --clear --title "Simple question" --backtitle "Combining dialog w/ expect" \
  --yesno "Are you having fun?" 6 25 && yesno_result=Yes || yesno_result=No

inputbox_result=$(dialog --stdout --clear  --backtitle "Combining dialog w/ expect" \
  --inputbox "Enter your name:" 8 40)


I use CentOS 6 for this demo, but it seems to work on other distros:

$ yum -y install expect

Assign execution permissions to dlgs.sh:

$ chmod +x dlgs.sh

Start recording using autoexpect:

$ autoexpect
autoexpect started, file is script.exp

Call dlgs.sh and interact:

$ ./dlgs.sh
...
3
No
Eduardo Lago Aguilar
By pressing Ctrl+D end the session watch. Then verify the recording by calling the generated script:

$ ./script.exp
...
3
No
Eduardo Lago Aguilar

Very simple isn't? See more.