views:

5284

answers:

7

Hi,

I have to develop a computing intensive Web Service (say we are making really big Matrix computations) so I've think that the way to go is coding it in C++. I can't do a wrapper using SWIG or JNI in Java to benefit from the WS existing libraries because the computations are state dependant and involve big matrices. Passing those values up and down doesn't seem very efficient. So the question is, how could I create such web service in C++ or how would you approach this problem?

Thanks in advance

+3  A: 

My self... I'd set up a webserver, say Apache, and have a Php script (or Python, or Ruby or whatever you like) that opens a TCP channel to my C++ application, this way my C++ code can focus on the problem (algorithms) and not waste time embedding an HTTP server in my app for no real reason. This is basically what designing in layers is all about.

Besides the real lesson here is that the C++ <=> Webserver latency is NOTHING compared to Webserver<= Internet =>Client.

So even a wrapper's overhead would likely be more than acceptable if you really want to use SWIG for example (I still stick with my TCP solution though)

Robert Gould
+2  A: 

You can write your C++ program following the CGI or FastCGI specs that most webservers support. Then, you just host it in the webserver of your choice (Apache, lighttpd, nginx, etc.).

The RESTful part is just about good design, that is, what will you do with the requests the webserver will pass on to your app.

Vinko Vrsalovic
+6  A: 

You need to partition your application into 2 pieces: 1) a web front end and 2) business logic. The business logic would be your C++ code. You really don't want to implement your web front end in C++ unless you have a lot of spare time and patience. Luckily there are a lot of web frameworks to choose from which are based on more modern languages. My favorite is the new ASP.NET MVC framework because I'm a Windows/.NET guy, but you can take your pick (ruby on rails, python, java, etc).

RESTful design requires that you organize your application's functionality as resources and CRUD operations on those resources. CRUD operations map to the HTTP POST, GET, PUT and DELETE methods. A great book on RESTful design is RESTful Web Services... note I haven't found a whole lot of books on this topic.

Once you design and implement your REST API in the web front end, there are various ways to connect it to your C++ app, some of which were mentioned by previous posters (e.g. FastCGI).

P.S. You mentioned something about your app being stateful. If you need to have state that that persists across multiple HTTP requests, there are some RESTful design principles you can apply. Check out the book or search the web (sorry I don't have my favorite links handy right now).

DSO
A: 

It sounds like you're planning to write a web service in C++ and have a website make calls to it. A RESTful web service is mainly concerned with "human readable URLs." The way to mix in a little REST is to use a decent HTTP library and follow the accepted REST ways of passing in parameters. The HTTP library will have interfaces for parsing the correct parameters from the URL and returning the result.

Max Lybbert
+6  A: 

I agree with the other answers here in that you really should be breaking your application into separate components for your web server and application layers. With that said, if you really insist on implementing a RESTful web service embedded into a C++ application, there are the following options:

Personally, I have only used libmicrohttpd. I've found it to be a fairly good library to work with. It is small, fast, and stable (don't let the 0.4.0 version scare you off). The developer participates in a mailing list in which he responds to questions and bug reports. Obviously libmicrohttpd is my recommendation for you should you decide to pursue this route.

Andaris
+4  A: 

I would add Pion Web Server to the list of potential C++ web servers to use. http://www.pion.org (built on top of boost)

I've been using it to implement a C++ REST interface and so far I have been very pleased (Although the documentation is a bit light).

Only bits I have had to program so far have been the transformations from HTTPRequest/Response to internal data structures.

Understand there are a few downsides to having the C++ web server integrated directly though:

  1. Memory leaks in the system will affect the web server
  2. Serialization libraries in C++ are less than spectacular
  3. Thread safety in C++ is extra important because of memory corruption
TK
+1  A: 

There is a related answer on another thread: Are there any web frameworks for compiled languages like C++? and especially: NanoGear

Comptrol