How to wrap a C library so that it can be called from a web service
Asked Answered
C

6

6

We have a library with very complex logic implemented in C. It has a command line interface with not too complex string-based arguments. In order to access this, we would like to wrap the library so that it can be accessed with simple XML RPC or even straightforward HTTP POST calls.

Having some experience with Java, my first idea would be

  • Wrap the library in JNI/JNA
  • Use a thin WS stack and a servlet engine
  • Proxy requests through Apache to the servlet engine

I believe there should already be something simple that could be used, so I am posting this question here. A solution has the following requirements

  • It should be deployable to a current linux distribution, preferrably already available via package management
  • It should integrate with a standard web server (as in my example Apache)
  • Small changes to the library's interface should be manageable
  • End-to-end (HTTP-WS-library-WS-HTTP) the solution should not incur too much overhead, but reliability is very important

Alternatively to the JNI/JNA proposal, I think in the C# world it should not be too difficult to write a web service and call this unmanaged code module, but I hope someone can give me some pointers that are feasible in regards to the requirements.

Chancemedley answered 10/2, 2010 at 20:52 Comment(0)
H
1

Creating an apache module is quite easy and since your familiar with xmlrpc you should check out mod-xmlrpc2. You can easily add your C code to this apache module and have a running xmlrpc server in minutes

Hazelhazelnut answered 11/2, 2010 at 15:30 Comment(1)
I have voted up all good answers, but this is the route we actually took. Thank you, Bob.Chancemedley
T
2

If you're going with web services, perhaps Soaplab would be useful. It's basically a tool to wrap existing command line applications in SOAP web services. The web services it generates look a bit weird but it is quite a popular way to make something like this work.

Tape answered 11/2, 2010 at 15:21 Comment(0)
L
1

I think you may also publish it as a SOAP based web service. gSoap can be used to provide the service interface out of the library. Have you explored gSOAP? See http://www.cs.fsu.edu/~engelen/soap.html

Regards, Kangkan

Lei answered 11/2, 2010 at 8:51 Comment(0)
H
1

Creating an apache module is quite easy and since your familiar with xmlrpc you should check out mod-xmlrpc2. You can easily add your C code to this apache module and have a running xmlrpc server in minutes

Hazelhazelnut answered 11/2, 2010 at 15:30 Comment(1)
I have voted up all good answers, but this is the route we actually took. Thank you, Bob.Chancemedley
A
0

Depends what technology you're comfortable with, what you already have installed and working on your servers, and what your load requirements are.

How about raw CGI? Assuming the C code is stateless between requests, you can do this without modifying the library at all. Write a simple script which pulls the request parameters out of the CGI environment, perhaps sanitises the input, calls the library via the command-line interface, and packages the result into whatever HTTP response you want. Then configure Apache to dispatch the relevant URL(s) to this script. Python, for example, has library support for XML-RPC, and so does every other scripting language used on the web.

Servlets sound like overkill, but for instance if you want multiple requests per CGI process instantiation, and don't feel like getting involved in Apache configuration, then it might be easiest to stick with what you know.

Acidfast answered 10/2, 2010 at 21:9 Comment(1)
Thank you, this is a very straightforward and simple suggestion, which I (strangely enough) had not really considered. It should not be too complicated to do this. The CLI for the library is not yet complete, but I believe that direct API calls are (1) more stable and (2) much faster. However, I think this will be the first implementation we will be trying. Thank you.Chancemedley
B
0

I'm doing a similar thing with C++ at the moment. In my case, I'm writing a PHP module to allow PHP scripts to access the logic in my C++ library.

I can then use whatever format I want to allow the rest of the world to see it - initially it will just be through a PHP web application but I'll also be developing an XML-RPC interface.

Billington answered 11/2, 2010 at 15:25 Comment(0)
J
0

If you're going down the JNI route, check out SWIG. http://www.swig.org/Doc1.3/Java.html

Assuming you have headers to project bindings with, swig is pretty easy to work with.

Jackshaft answered 11/2, 2010 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.