From e22bdc0f9572b6a1a2304799d481b25b3e962f87 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 11 Jan 2010 08:30:15 +0000 Subject: Improvements to autoconf build to make support for python, web service etc and relevant test cases optional and generate ac_defines used in ifdefs to check for debug and multithreading. Moved some optional code and test cases around to run them only when the tested features are built. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@897791 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/server/Makefile.am | 9 +- sca-cpp/trunk/modules/server/client-test.cpp | 368 +----------------- sca-cpp/trunk/modules/server/client-test.hpp | 349 +++++++++++++++++ sca-cpp/trunk/modules/server/client-test.py | 22 -- sca-cpp/trunk/modules/server/domain-test.composite | 10 +- sca-cpp/trunk/modules/server/httpd-test | 1 + sca-cpp/trunk/modules/server/impl-test.cpp | 1 - sca-cpp/trunk/modules/server/mod-cpp.hpp | 1 - sca-cpp/trunk/modules/server/mod-eval.cpp | 388 +------------------ sca-cpp/trunk/modules/server/mod-eval.hpp | 422 +++++++++++++++++++++ sca-cpp/trunk/modules/server/mod-python.hpp | 80 ---- sca-cpp/trunk/modules/server/mod-scheme.hpp | 1 - sca-cpp/trunk/modules/server/mod-wiring.cpp | 1 - sca-cpp/trunk/modules/server/scheme-conf | 26 ++ sca-cpp/trunk/modules/server/server-conf | 2 - sca-cpp/trunk/modules/server/server-test | 1 + sca-cpp/trunk/modules/server/server-test.py | 29 -- sca-cpp/trunk/modules/server/wiring-test | 1 + 18 files changed, 810 insertions(+), 902 deletions(-) create mode 100644 sca-cpp/trunk/modules/server/client-test.hpp delete mode 100644 sca-cpp/trunk/modules/server/client-test.py create mode 100644 sca-cpp/trunk/modules/server/mod-eval.hpp delete mode 100644 sca-cpp/trunk/modules/server/mod-python.hpp create mode 100755 sca-cpp/trunk/modules/server/scheme-conf delete mode 100644 sca-cpp/trunk/modules/server/server-test.py (limited to 'sca-cpp/trunk/modules/server') diff --git a/sca-cpp/trunk/modules/server/Makefile.am b/sca-cpp/trunk/modules/server/Makefile.am index 6b3e818090..f68fee9f4f 100644 --- a/sca-cpp/trunk/modules/server/Makefile.am +++ b/sca-cpp/trunk/modules/server/Makefile.am @@ -20,21 +20,20 @@ noinst_PROGRAMS = client-test libdir=$(prefix)/lib lib_LTLIBRARIES = libmod_tuscany_eval.la libmod_tuscany_wiring.la -INCLUDES = -I. -I$(top_builddir)/kernel -I${LIBXML2_INCLUDE} -I${HTTPD_INCLUDE} -I${APR_INCLUDE} -I${JS_INCLUDE} -I${CURL_INCLUDE} +INCLUDES = -I${HTTPD_INCLUDE} libmod_tuscany_eval_la_SOURCES = mod-eval.cpp -libmod_tuscany_eval_la_LIBADD = -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 -L${CURL_LIB} -lcurl -L${JS_LIB} -lmozjs -L${PYTHON_LIB} -lpython2.6 +libmod_tuscany_eval_la_LIBADD = -lxml2 -lcurl -lmozjs libmod_tuscany_wiring_la_SOURCES = mod-wiring.cpp -libmod_tuscany_wiring_la_LIBADD = -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 -L${CURL_LIB} -lcurl -L${JS_LIB} -lmozjs +libmod_tuscany_wiring_la_LIBADD = -lxml2 -lcurl -lmozjs testdir=$(prefix)/test test_LTLIBRARIES = libimpl-test.la libimpl_test_la_SOURCES = impl-test.cpp -libimpl_test_la_LIBADD = -L${APR_LIB} -lapr-1 -laprutil-1 client_test_SOURCES = client-test.cpp -client_test_LDADD = -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 -L${CURL_LIB} -lcurl -L${JS_LIB} -lmozjs +client_test_LDADD = -lxml2 -lcurl -lmozjs TESTS = httpd-test server-test wiring-test diff --git a/sca-cpp/trunk/modules/server/client-test.cpp b/sca-cpp/trunk/modules/server/client-test.cpp index 557dfaf59e..3bc8aa7d10 100644 --- a/sca-cpp/trunk/modules/server/client-test.cpp +++ b/sca-cpp/trunk/modules/server/client-test.cpp @@ -23,364 +23,14 @@ * Test HTTP client functions. */ -#include -#include -#include -#include #include "stream.hpp" #include "string.hpp" -#include "parallel.hpp" -#include "perf.hpp" -#include "../http/curl.hpp" +#include "client-test.hpp" namespace tuscany { namespace server { -ostream* curlWriter(const string& s, ostream* os) { - (*os) << s; - return os; -} - -const bool testGet() { - gc_scoped_pool pool; - http::CURLSession ch; - { - ostringstream os; - const failable > r = http::get(curlWriter, &os, "http://localhost:8090", ch); - assert(hasContent(r)); - assert(contains(str(os), "HTTP/1.1 200 OK")); - assert(contains(str(os), "It works")); - } - { - const failable r = http::getcontent("http://localhost:8090", ch); - assert(hasContent(r)); - assert(contains(car(reverse(list(content(r)))), "It works")); - } - return true; -} - -struct getLoop { - http::CURLSession ch; - getLoop(http::CURLSession& ch) : ch(ch) { - } - const bool operator()() const { - const failable r = http::getcontent("http://localhost:8090", ch); - assert(hasContent(r)); - assert(contains(car(reverse(list(content(r)))), "It works")); - return true; - } -}; - -const bool testGetPerf() { - gc_scoped_pool pool; - http::CURLSession ch; - const lambda gl = getLoop(ch); - cout << "Static GET test " << time(gl, 5, 200) << " ms" << endl; - return true; -} - -const bool testEval(const string& uri) { - gc_scoped_pool pool; - http::CURLSession ch; - const value val = content(http::evalExpr(mklist(string("echo"), string("Hello")), uri, ch)); - assert(val == string("Hello")); - return true; -} - -const bool testEval() { - testEval("http://localhost:8090/test"); - testEval("http://localhost:8090/cpp"); - testEval("http://localhost:8090/python"); - return true; -} - -struct evalLoop { - const string uri; - http::CURLSession ch; - evalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { - } - const bool operator()() const { - const value val = content(http::evalExpr(mklist(string("echo"), string("Hello")), uri, ch)); - assert(val == string("Hello")); - return true; - } -}; - -const value blob(string(3000, 'A')); -const list blobs = mklist(blob, blob, blob, blob, blob); - -struct blobEvalLoop { - const string uri; - http::CURLSession ch; - blobEvalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { - } - const bool operator()() const { - const value val = content(http::evalExpr(mklist(string("echo"), blobs), uri, ch)); - assert(val == blobs); - return true; - } -}; - -const bool testEvalPerf(const string& type, const string& uri) { - gc_scoped_pool pool; - http::CURLSession ch; - const lambda el = evalLoop(uri, ch); - cout << type << " JSON-RPC eval echo test " << time(el, 5, 200) << " ms" << endl; - const lambda bel = blobEvalLoop(uri, ch); - cout << type << " JSON-RPC eval blob test " << time(bel, 5, 200) << " ms" << endl; - return true; -} - -const bool testEvalPerf() { - testEvalPerf("Scheme", "http://localhost:8090/test"); - testEvalPerf("C++", "http://localhost:8090/cpp"); - testEvalPerf("Python", "http://localhost:8090/python"); - return true; -} - -bool testPost(const string& uri) { - gc_scoped_pool pool; - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99")); - const list a = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - http::CURLSession ch; - const failable id = http::post(a, uri, ch); - assert(hasContent(id)); - return true; -} - -const bool testPost() { - testPost("http://localhost:8090/test"); - testPost("http://localhost:8090/cpp"); - testPost("http://localhost:8090/python"); - return true; -} - -struct postLoop { - const string uri; - const value val; - http::CURLSession ch; - postLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { - } - const bool operator()() const { - const failable id = http::post(val, uri, ch); - assert(hasContent(id)); - return true; - } -}; - -struct postBlobLoop { - const string uri; - const value val; - http::CURLSession ch; - postBlobLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { - } - const bool operator()() const { - gc_scoped_pool pool; - const failable id = http::post(val, uri, ch); - assert(hasContent(id)); - return true; - } -}; - -const bool testPostPerf(const string& type, const string& uri) { - gc_scoped_pool pool; - http::CURLSession ch; - { - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99")); - const list val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - const lambda pl = postLoop(uri, val, ch); - cout << type << " ATOMPub POST small test " << time(pl, 5, 200) << " ms" << endl; - } - { - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "blob1" + blob) - + (list() + "blob2" + blob) - + (list() + "blob3" + blob) - + (list() + "blob4" + blob) - + (list() + "blob5" + blob) - + (list() + "price" + string("$2.99")); - const list val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - const lambda pl = postBlobLoop(uri, val, ch); - cout << type << " ATOMPub POST blob test " << time(pl, 5, 200) << " ms" << endl; - } - return true; -} - -const bool testPostPerf() { - testPostPerf("Scheme", "http://localhost:8090/test"); - testPostPerf("C++", "http://localhost:8090/cpp"); - testPostPerf("Python", "http://localhost:8090/python"); - return true; -} - -#ifdef WANT_THREADS - -const bool postThread(const string& uri, const int count, const value& val) { - gc_scoped_pool pool; - http::CURLSession ch; - const lambda pl = postLoop(uri, val, ch); - time(pl, 0, count); - return true; -} - -const list > startPost(worker& w, const int threads, const lambda& l) { - if (threads == 0) - return list >(); - return cons(submit(w, l), startPost(w, threads - 1, l)); -} - -const bool checkPost(const list >& r) { - if (isNil(r)) - return true; - assert(car(r) == true); - return checkPost(cdr(r)); -} - -struct postThreadLoop { - const lambda l; - const int threads; - const gc_ptr w; - postThreadLoop(const lambda& l, const int threads) : l(l), threads(threads), w(new (gc_new()) worker(threads)) { - } - const bool operator()() const { - list > r = startPost(*w, threads, l); - checkPost(r); - return true; - } -}; - -const bool testPostThreadPerf(const string& type, const string& uri) { - gc_scoped_pool pool; - const int count = 50; - const int threads = 10; - - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99")); - const value val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - - const lambda pl= curry(lambda(postThread), uri, count, val); - const lambda ptl = postThreadLoop(pl, threads); - double t = time(ptl, 0, 1) / (threads * count); - cout << type << " ATOMPub POST thread test " << t << " ms" << endl; - - return true; -} - -const bool testPostThreadPerf() { - testPostThreadPerf("Scheme", "http://localhost:8090/test"); - testPostThreadPerf("C++", "http://localhost:8090/cpp"); - testPostThreadPerf("Python", "http://localhost:8090/python"); - return true; -} - -#else - -const bool postProc(const string& uri, const int count, const value& val) { - gc_scoped_pool pool; - http::CURLSession ch; - const lambda pl = postLoop(uri, val, ch); - time(pl, 0, count); - return true; -} - -const list startPost(const int procs, const lambda& l) { - if (procs == 0) - return list(); - pid_t pid = fork(); - if (pid == 0) { - assert(l() == true); - exit(0); - } - return cons(pid, startPost(procs - 1, l)); -} - -const bool checkPost(const list& r) { - if (isNil(r)) - return true; - int status; - waitpid(car(r), &status, 0); - assert(status == 0); - return checkPost(cdr(r)); -} - -struct postForkLoop { - const lambda l; - const int procs; - postForkLoop(const lambda& l, const int procs) : l(l), procs(procs) { - } - const bool operator()() const { - list r = startPost(procs, l); - checkPost(r); - return true; - } -}; - -const bool testPostForkPerf(const string& type, const string& uri) { - gc_scoped_pool pool; - const int count = 50; - const int procs = 10; - - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99")); - const value val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - - const lambda pl= curry(lambda(postProc), uri, count, val); - const lambda ptl = postForkLoop(pl, procs); - double t = time(ptl, 0, 1) / (procs * count); - cout << type << "ATOMPub POST fork test " << t << " ms" << endl; - - return true; -} - -const bool testPostForkPerf() { - testPostForkPerf("Scheme", "http://localhost:8090/test"); - testPostForkPerf("C++", "http://localhost:8090/cpp"); - testPostForkPerf("Python", "http://localhost:8090/python"); - return true; -} - -#endif - -const bool testPut(const string& uri) { - gc_scoped_pool pool; - const list i = list() - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99")); - const list a = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - http::CURLSession ch; - value rc = content(http::put(a, uri, ch)); - assert(rc == value(true)); - return true; -} - -const bool testPut() { - testPut("http://localhost:8090/test/111"); - testPut("http://localhost:8090/cpp/111"); - testPut("http://localhost:8090/python/111"); - return true; -} - -const bool testDel(const string& uri) { - gc_scoped_pool pool; - http::CURLSession ch; - value rc = content(http::del(uri, ch)); - assert(rc == value(true)); - return true; -} - -const bool testDel() { - testDel("http://localhost:8090/test/123456789"); - testDel("http://localhost:8090/cpp/123456789"); - testDel("http://localhost:8090/python/123456789"); - return true; -} +string testURI = "http://localhost:8090/test"; } } @@ -388,19 +38,7 @@ const bool testDel() { int main() { tuscany::cout << "Testing..." << tuscany::endl; - tuscany::server::testGet(); - tuscany::server::testPost(); - tuscany::server::testPut(); - tuscany::server::testDel(); - tuscany::server::testEval(); - tuscany::server::testGetPerf(); - tuscany::server::testPostPerf(); -#ifdef WANT_THREADS - tuscany::server::testPostThreadPerf(); -#else - tuscany::server::testPostForkPerf(); -#endif - tuscany::server::testEvalPerf(); + tuscany::server::testServer(); tuscany::cout << "OK" << tuscany::endl; diff --git a/sca-cpp/trunk/modules/server/client-test.hpp b/sca-cpp/trunk/modules/server/client-test.hpp new file mode 100644 index 0000000000..d1c8d9ba15 --- /dev/null +++ b/sca-cpp/trunk/modules/server/client-test.hpp @@ -0,0 +1,349 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * Test HTTP client functions. + */ + +#include +#include +#include +#include +#include "stream.hpp" +#include "string.hpp" +#include "parallel.hpp" +#include "perf.hpp" +#include "../http/curl.hpp" + +namespace tuscany { +namespace server { + +extern string testURI; + +ostream* curlWriter(const string& s, ostream* os) { + (*os) << s; + return os; +} + +const bool testGet() { + gc_scoped_pool pool; + http::CURLSession ch; + { + ostringstream os; + const failable > r = http::get(curlWriter, &os, "http://localhost:8090", ch); + assert(hasContent(r)); + assert(contains(str(os), "HTTP/1.1 200 OK")); + assert(contains(str(os), "It works")); + } + { + const failable r = http::getcontent("http://localhost:8090", ch); + assert(hasContent(r)); + assert(contains(car(reverse(list(content(r)))), "It works")); + } + return true; +} + +struct getLoop { + http::CURLSession ch; + getLoop(http::CURLSession& ch) : ch(ch) { + } + const bool operator()() const { + const failable r = http::getcontent("http://localhost:8090", ch); + assert(hasContent(r)); + assert(contains(car(reverse(list(content(r)))), "It works")); + return true; + } +}; + +const bool testGetPerf() { + gc_scoped_pool pool; + http::CURLSession ch; + const lambda gl = getLoop(ch); + cout << "Static GET test " << time(gl, 5, 200) << " ms" << endl; + return true; +} + +const bool testEval() { + gc_scoped_pool pool; + http::CURLSession ch; + const value val = content(http::evalExpr(mklist(string("echo"), string("Hello")), testURI, ch)); + assert(val == string("Hello")); + return true; +} + +struct evalLoop { + const string uri; + http::CURLSession ch; + evalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { + } + const bool operator()() const { + const value val = content(http::evalExpr(mklist(string("echo"), string("Hello")), uri, ch)); + assert(val == string("Hello")); + return true; + } +}; + +const value blob(string(3000, 'A')); +const list blobs = mklist(blob, blob, blob, blob, blob); + +struct blobEvalLoop { + const string uri; + http::CURLSession ch; + blobEvalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { + } + const bool operator()() const { + const value val = content(http::evalExpr(mklist(string("echo"), blobs), uri, ch)); + assert(val == blobs); + return true; + } +}; + +const bool testEvalPerf() { + gc_scoped_pool pool; + http::CURLSession ch; + const lambda el = evalLoop(testURI, ch); + cout << "JSON-RPC eval echo test " << time(el, 5, 200) << " ms" << endl; + const lambda bel = blobEvalLoop(testURI, ch); + cout << "JSON-RPC eval blob test " << time(bel, 5, 200) << " ms" << endl; + return true; +} + +bool testPost() { + gc_scoped_pool pool; + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99")); + const list a = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + http::CURLSession ch; + const failable id = http::post(a, testURI, ch); + assert(hasContent(id)); + return true; +} + +struct postLoop { + const string uri; + const value val; + http::CURLSession ch; + postLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { + } + const bool operator()() const { + const failable id = http::post(val, uri, ch); + assert(hasContent(id)); + return true; + } +}; + +struct postBlobLoop { + const string uri; + const value val; + http::CURLSession ch; + postBlobLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { + } + const bool operator()() const { + gc_scoped_pool pool; + const failable id = http::post(val, uri, ch); + assert(hasContent(id)); + return true; + } +}; + +const bool testPostPerf() { + gc_scoped_pool pool; + http::CURLSession ch; + { + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99")); + const list val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + const lambda pl = postLoop(testURI, val, ch); + cout << "ATOMPub POST small test " << time(pl, 5, 200) << " ms" << endl; + } + { + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "blob1" + blob) + + (list() + "blob2" + blob) + + (list() + "blob3" + blob) + + (list() + "blob4" + blob) + + (list() + "blob5" + blob) + + (list() + "price" + string("$2.99")); + const list val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + const lambda pl = postBlobLoop(testURI, val, ch); + cout << "ATOMPub POST blob test " << time(pl, 5, 200) << " ms" << endl; + } + return true; +} + +#ifdef WANT_THREADS + +const bool postThread(const string& uri, const int count, const value& val) { + gc_scoped_pool pool; + http::CURLSession ch; + const lambda pl = postLoop(uri, val, ch); + time(pl, 0, count); + return true; +} + +const list > startPost(worker& w, const int threads, const lambda& l) { + if (threads == 0) + return list >(); + return cons(submit(w, l), startPost(w, threads - 1, l)); +} + +const bool checkPost(const list >& r) { + if (isNil(r)) + return true; + assert(car(r) == true); + return checkPost(cdr(r)); +} + +struct postThreadLoop { + const lambda l; + const int threads; + const gc_ptr w; + postThreadLoop(const lambda& l, const int threads) : l(l), threads(threads), w(new (gc_new()) worker(threads)) { + } + const bool operator()() const { + list > r = startPost(*w, threads, l); + checkPost(r); + return true; + } +}; + +const bool testPostThreadPerf() { + gc_scoped_pool pool; + const int count = 50; + const int threads = 10; + + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99")); + const value val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + const lambda pl= curry(lambda(postThread), testURI, count, val); + const lambda ptl = postThreadLoop(pl, threads); + double t = time(ptl, 0, 1) / (threads * count); + cout << "ATOMPub POST thread test " << t << " ms" << endl; + + return true; +} + +#else + +const bool postProc(const string& uri, const int count, const value& val) { + gc_scoped_pool pool; + http::CURLSession ch; + const lambda pl = postLoop(uri, val, ch); + time(pl, 0, count); + return true; +} + +const list startPost(const int procs, const lambda& l) { + if (procs == 0) + return list(); + pid_t pid = fork(); + if (pid == 0) { + assert(l() == true); + exit(0); + } + return cons(pid, startPost(procs - 1, l)); +} + +const bool checkPost(const list& r) { + if (isNil(r)) + return true; + int status; + waitpid(car(r), &status, 0); + assert(status == 0); + return checkPost(cdr(r)); +} + +struct postForkLoop { + const lambda l; + const int procs; + postForkLoop(const lambda& l, const int procs) : l(l), procs(procs) { + } + const bool operator()() const { + list r = startPost(procs, l); + checkPost(r); + return true; + } +}; + +const bool testPostForkPerf() { + gc_scoped_pool pool; + const int count = 50; + const int procs = 10; + + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99")); + const value val = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + const lambda pl= curry(lambda(postProc), testURI, count, val); + const lambda ptl = postForkLoop(pl, procs); + double t = time(ptl, 0, 1) / (procs * count); + cout << "ATOMPub POST fork test " << t << " ms" << endl; + + return true; +} + +#endif + +const bool testPut() { + gc_scoped_pool pool; + const list i = list() + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99")); + const list a = mklist(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + http::CURLSession ch; + value rc = content(http::put(a, testURI, ch)); + assert(rc == value(true)); + return true; +} + +const bool testDel() { + gc_scoped_pool pool; + http::CURLSession ch; + value rc = content(http::del(testURI, ch)); + assert(rc == value(true)); + return true; +} + +const bool testServer() { + tuscany::server::testGet(); + tuscany::server::testPost(); + tuscany::server::testPut(); + tuscany::server::testDel(); + tuscany::server::testEval(); + tuscany::server::testGetPerf(); + tuscany::server::testPostPerf(); +#ifdef WANT_THREADS + tuscany::server::testPostThreadPerf(); +#else + tuscany::server::testPostForkPerf(); +#endif + tuscany::server::testEvalPerf(); + return true; +} + +} +} diff --git a/sca-cpp/trunk/modules/server/client-test.py b/sca-cpp/trunk/modules/server/client-test.py deleted file mode 100644 index 24b78f83ca..0000000000 --- a/sca-cpp/trunk/modules/server/client-test.py +++ /dev/null @@ -1,22 +0,0 @@ -# JSON-RPC test case - -def echo(x, ref): - return ref("echo", x) - -# ATOMPub test case - -def getall(ref): - return ref("getall") - -def get(id, ref): - return ref("get", id) - -def post(entry, ref): - return ref("post", entry) - -def put(id, entry, ref): - return ref("put", id, entry) - -def delete(id, ref): - return ref("delete", id) - diff --git a/sca-cpp/trunk/modules/server/domain-test.composite b/sca-cpp/trunk/modules/server/domain-test.composite index c911e9cf07..d5b0dae511 100644 --- a/sca-cpp/trunk/modules/server/domain-test.composite +++ b/sca-cpp/trunk/modules/server/domain-test.composite @@ -36,16 +36,8 @@ - - - - - - - - - + diff --git a/sca-cpp/trunk/modules/server/httpd-test b/sca-cpp/trunk/modules/server/httpd-test index 7dccfbf112..79651c8351 100755 --- a/sca-cpp/trunk/modules/server/httpd-test +++ b/sca-cpp/trunk/modules/server/httpd-test @@ -22,6 +22,7 @@ echo "Testing..." # Setup ../http/httpd-conf tmp 8090 htdocs ./server-conf tmp +./scheme-conf tmp cat >>tmp/conf/httpd.conf < diff --git a/sca-cpp/trunk/modules/server/impl-test.cpp b/sca-cpp/trunk/modules/server/impl-test.cpp index 1fd432c7fb..ad8953bf12 100644 --- a/sca-cpp/trunk/modules/server/impl-test.cpp +++ b/sca-cpp/trunk/modules/server/impl-test.cpp @@ -29,7 +29,6 @@ #include "list.hpp" #include "value.hpp" #include "monad.hpp" -#include "debug.hpp" namespace tuscany { namespace server { diff --git a/sca-cpp/trunk/modules/server/mod-cpp.hpp b/sca-cpp/trunk/modules/server/mod-cpp.hpp index 93abd84590..416ccabf39 100644 --- a/sca-cpp/trunk/modules/server/mod-cpp.hpp +++ b/sca-cpp/trunk/modules/server/mod-cpp.hpp @@ -33,7 +33,6 @@ #include "function.hpp" #include "list.hpp" #include "value.hpp" -#include "debug.hpp" #include "monad.hpp" #include "dynlib.hpp" #include "../scheme/driver.hpp" diff --git a/sca-cpp/trunk/modules/server/mod-eval.cpp b/sca-cpp/trunk/modules/server/mod-eval.cpp index 23454e3ceb..936c68d70f 100644 --- a/sca-cpp/trunk/modules/server/mod-eval.cpp +++ b/sca-cpp/trunk/modules/server/mod-eval.cpp @@ -20,268 +20,22 @@ /* $Rev$ $Date$ */ /** - * HTTPD module used to eval component implementations. + * HTTPD module used to eval C++ and Scheme component implementations. */ #include "string.hpp" -#include "stream.hpp" #include "function.hpp" #include "list.hpp" -#include "tree.hpp" #include "value.hpp" -#include "element.hpp" #include "monad.hpp" -#include "../atom/atom.hpp" -#include "../json/json.hpp" -#include "../scdl/scdl.hpp" -#include "../http/curl.hpp" -#include "../http/httpd.hpp" +#include "mod-eval.hpp" #include "mod-scheme.hpp" #include "mod-cpp.hpp" -#include "mod-python.hpp" - -extern "C" { -extern module AP_MODULE_DECLARE_DATA mod_tuscany_eval; -} namespace tuscany { namespace server { namespace modeval { -/** - * Server configuration. - */ -class ServerConf { -public: - ServerConf(server_rec* s) : s(s), home(""), wiringServerName("") { - } - - const server_rec* s; - string home; - string wiringServerName; -}; - -/** - * Directory configuration. - */ -class DirConf { -public: - DirConf(char* dirspec) : dirspec(dirspec), contributionPath(""), compositeName("") { - } - const char* dirspec; - string contributionPath; - string compositeName; - list implementations; -}; - -/** - * Convert a result represented as a content + failure pair to a - * failable monad. - */ -const failable failableResult(const list& v) { - if (isNil(cdr(v))) - return car(v); - return mkfailure(string(cadr(v))); -} - -/** - * Handle an HTTP GET. - */ -const failable get(request_rec* r, const lambda&)>& impl) { - debug(r->uri, "modeval::get::uri"); - - // Inspect the query string - const list > args = httpd::queryArgs(r); - const list ia = assoc(value("id"), args); - const list ma = assoc(value("method"), args); - - // Evaluate a JSON-RPC request and return a JSON result - if (!isNil(ia) && !isNil(ma)) { - - // Extract the request id, method and params - const value id = cadr(ia); - const value func = c_str(json::funcName(string(cadr(ma)))); - - // Apply the requested function - const failable val = failableResult(impl(cons(func, httpd::queryParams(args)))); - if (!hasContent(val)) - return mkfailure(reason(val)); - - // Return JSON result - json::JSONContext cx; - return httpd::writeResult(json::jsonResult(id, content(val), cx), "application/json-rpc", r); - } - - // Evaluate an ATOM GET request and return an ATOM feed representing a collection of resources - const list path(httpd::pathValues(r->uri)); - if (isNil(cddr(path))) { - const failable val = failableResult(impl(cons("getall", list()))); - if (!hasContent(val)) - return mkfailure(reason(val)); - return httpd::writeResult(atom::writeATOMFeed(atom::feedValuesToElements(content(val))), "application/atom+xml;type=feed", r); - } - - // Evaluate an ATOM GET and return an ATOM entry representing a resource - const failable val = failableResult(impl(cons("get", mklist(caddr(path))))); - if (!hasContent(val)) - return mkfailure(reason(val)); - return httpd::writeResult(atom::writeATOMEntry(atom::entryValuesToElements(content(val))), "application/atom+xml;type=entry", r); -} - -/** - * Handle an HTTP POST. - */ -const failable post(request_rec* r, const lambda&)>& impl) { - const list ls = httpd::read(r); - debug(r->uri, "modeval::post::url"); - debug(ls, "modeval::post::input"); - - // Evaluate a JSON-RPC request and return a JSON result - const string ct = httpd::contentType(r); - if (contains(ct, "application/json-rpc") || contains(ct, "text/plain")) { - json::JSONContext cx; - const list json = elementsToValues(content(json::readJSON(ls, cx))); - const list > args = httpd::postArgs(json); - - // Extract the request id, method and params - const value id = cadr(assoc(value("id"), args)); - const value func = c_str(json::funcName(cadr(assoc(value("method"), args)))); - const list params = (list)cadr(assoc(value("params"), args)); - - // Evaluate the request expression - const failable val = failableResult(impl(cons(func, params))); - if (!hasContent(val)) - return mkfailure(reason(val)); - - // Return JSON result - return httpd::writeResult(json::jsonResult(id, content(val), cx), "application/json-rpc", r); - } - - // Evaluate an ATOM POST request and return the location of the corresponding created resource - if (contains(ct, "application/atom+xml")) { - - // Evaluate the request expression - const value entry = atom::entryValue(content(atom::readEntry(ls))); - const failable val = failableResult(impl(cons("post", mklist(entry)))); - if (!hasContent(val)) - return mkfailure(reason(val)); - - // Return the created resource location - apr_table_setn(r->headers_out, "Location", apr_pstrdup(r->pool, httpd::url(content(val), r))); - r->status = HTTP_CREATED; - return OK; - } - - return HTTP_NOT_IMPLEMENTED; -} - -/** - * Handle an HTTP PUT. - */ -const failable put(request_rec* r, const lambda&)>& impl) { - const list ls = httpd::read(r); - debug(r->uri, "modeval::put::url"); - debug(ls, "modeval::put::input"); - - // Evaluate an ATOM PUT request and update the corresponding resource - const list path(httpd::pathValues(r->uri)); - const value entry = atom::entryValue(content(atom::readEntry(ls))); - const failable val = failableResult(impl(cons("put", mklist(caddr(path), entry)))); - if (!hasContent(val)) - return mkfailure(reason(val)); - if (val == value(false)) - return HTTP_NOT_FOUND; - return OK; -} - -/** - * Handle an HTTP DELETE. - */ -const failable del(request_rec* r, const lambda&)>& impl) { - debug(r->uri, "modeval::delete::url"); - - // Evaluate an ATOM delete request - const list path(httpd::pathValues(r->uri)); - if (isNil(cddr(path))) { - - // Delete a collection of resources - const failable val = failableResult(impl(cons("deleteall", list()))); - if (!hasContent(val)) - return mkfailure(reason(val)); - if (val == value(false)) - return HTTP_NOT_FOUND; - return OK; - } - - // Delete a resource - const failable val = failableResult(impl(cons("delete", mklist(caddr(path))))); - if (!hasContent(val)) - return mkfailure(reason(val)); - if (val == value(false)) - return HTTP_NOT_FOUND; - return OK; -} - -/** - * Translate a component request. - */ -int translate(request_rec *r) { - gc_scoped_pool pool(r->pool); - if (strncmp(r->uri, "/components/", 12) != 0) - return DECLINED; - r->handler = "mod_tuscany_eval"; - return OK; -} - -/** - * HTTP request handler. - */ -int handler(request_rec *r) { - gc_scoped_pool pool(r->pool); - if(strcmp(r->handler, "mod_tuscany_eval")) - return DECLINED; - httpdDebugRequest(r, "modeval::handler::input"); - - // Set up the read policy - const int rc = httpd::setupReadPolicy(r); - if(rc != OK) - return rc; - - // Get the component implementation lambda - DirConf& dc = httpd::dirConf(r, &mod_tuscany_eval); - const list path(httpd::pathValues(r->uri)); - const list impl(assoctree(cadr(path), dc.implementations)); - if (isNil(impl)) - return HTTP_NOT_FOUND; - - // Handle HTTP method - const lambda&)> l(cadr(impl)); - if (r->header_only) - return OK; - if(r->method_number == M_GET) - return httpd::reportStatus(get(r, l)); - if(r->method_number == M_POST) - return httpd::reportStatus(post(r, l)); - if(r->method_number == M_PUT) - return httpd::reportStatus(put(r, l)); - if(r->method_number == M_DELETE) - return httpd::reportStatus(del(r, l)); - return HTTP_NOT_IMPLEMENTED; -} - -/** - * Convert a list of component references to a list of HTTP proxy lambdas. - */ -const value mkproxy(const value& ref, const string& base) { - return lambda&)>(http::proxy(base + string(scdl::name(ref)))); -} - -const list proxies(const list& refs, const string& base) { - if (isNil(refs)) - return refs; - return cons(mkproxy(car(refs), base), proxies(cdr(refs), base)); -} - /** * Return a configured component implementation. * For now only Scheme and C++ implementations are supported. @@ -289,149 +43,11 @@ const list proxies(const list& refs, const string& base) { const failable&)> > readImplementation(const string& itype, const string& path, const list& px) { if (contains(itype, ".scheme")) return modscheme::readImplementation(path, px); - if (contains(itype, ".python")) - return modpython::readImplementation(path, px); if (contains(itype, ".cpp")) return modcpp::readImplementation(path, px); return mkfailure&)> >(string("Unsupported implementation type: ") + itype); } -const value confImplementation(DirConf& dc, ServerConf& sc, server_rec& server, const value& comp) { - const value impl = scdl::implementation(comp); - const string path = dc.contributionPath + string(scdl::uri(impl)); - - // Convert component references to configured proxy lambdas - ostringstream base; - if (sc.wiringServerName == "") - base << (server.server_scheme == NULL? "http" : server.server_scheme) - << "://" << (server.server_hostname == NULL? "localhost" : server.server_hostname) - << ":" << (server.port == 0? 80 : server.port) - << "/references/" << string(scdl::name(comp)) << "/"; - else - base << sc.wiringServerName << "/references/" << string(scdl::name(comp)) << "/"; - const list px(proxies(scdl::references(comp), str(base))); - - // Read and configure the implementation - const failable&)> > cimpl(readImplementation(elementName(impl), path, px)); - if (!hasContent(cimpl)) - return reason(cimpl); - return content(cimpl); } - -/** - * Return a tree of component-name + configured-implementation pairs. - */ -const list componentToImplementationAssoc(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { - if (isNil(c)) - return c; - return cons(mklist(scdl::name(car(c)), confImplementation(dc, sc, server, car(c))), componentToImplementationAssoc(dc, sc, server, cdr(c))); -} - -const list componentToImplementationTree(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { - return mkbtree(sort(componentToImplementationAssoc(dc, sc, server, c))); } - -/** - * Read the components declared in a composite. - */ -const failable > readComponents(const string& path) { - ifstream is(path); - if (fail(is)) - return mkfailure >(string("Could not read composite: ") + path); - return scdl::components(readXML(streamList(is))); -} - -/** - * Configure the components declared in the deployed composite. - */ -const bool confComponents(DirConf& dc, ServerConf& sc, server_rec& server) { - if (dc.contributionPath == "" || dc.compositeName == "") - return true; - const failable > comps = readComponents(dc.contributionPath + dc.compositeName); - if (!hasContent(comps)) - return true; - dc.implementations = componentToImplementationTree(dc, sc, server, content(comps)); - debug(dc.implementations, "modeval::confComponents::implementations"); - return true; -} - -/** - * Configuration commands. - */ -const char *confHome(cmd_parms *cmd, unused void *c, const char *arg) { - gc_scoped_pool pool(cmd->pool); - ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - sc.home = arg; - return NULL; -} -const char *confWiringServerName(cmd_parms *cmd, unused void *c, const char *arg) { - gc_scoped_pool pool(cmd->pool); - ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - sc.wiringServerName = arg; - return NULL; -} -const char *confContribution(cmd_parms *cmd, void *c, const char *arg) { - gc_scoped_pool pool(cmd->pool); - ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - DirConf& dc = *(DirConf*)c; - dc.contributionPath = arg; - confComponents(dc, sc, *cmd->server); - return NULL; -} -const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { - gc_scoped_pool pool(cmd->pool); - ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); - DirConf& dc = *(DirConf*)c; - dc.compositeName = arg; - confComponents(dc, sc, *cmd->server); - return NULL; -} - -/** - * HTTP server module declaration. - */ -const command_rec commands[] = { - AP_INIT_TAKE1("TuscanyHome", (const char*(*)())confHome, NULL, RSRC_CONF, "Tuscany home directory"), - AP_INIT_TAKE1("SCAWiringServerName", (const char*(*)())confWiringServerName, NULL, RSRC_CONF, "SCA wiring server name"), - AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, ACCESS_CONF, "SCA contribution location"), - AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, ACCESS_CONF, "SCA composite location"), - {NULL, NULL, NULL, 0, NO_ARGS, NULL} -}; - -int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, unused server_rec *s) { - return OK; -} - -void childInit(apr_pool_t* p, server_rec* svr_rec) { - gc_scoped_pool pool(p); - ServerConf* c = (ServerConf*)ap_get_module_config(svr_rec->module_config, &mod_tuscany_eval); - if(c == NULL) { - cerr << "[Tuscany] Due to one or more errors mod_tuscany_eval loading failed. Causing apache to stop loading." << endl; - exit(APEXIT_CHILDFATAL); - } -} - -void registerHooks(unused apr_pool_t *p) { - ap_hook_post_config(postConfig, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_child_init(childInit, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_handler(handler, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_translate_name(translate, NULL, NULL, APR_HOOK_FIRST); -} - -} -} -} - -extern "C" { - -module AP_MODULE_DECLARE_DATA mod_tuscany_eval = { - STANDARD20_MODULE_STUFF, - // dir config and merger - tuscany::httpd::makeDirConf, NULL, - // server config and merger - tuscany::httpd::makeServerConf, NULL, - // commands and hooks - tuscany::server::modeval::commands, tuscany::server::modeval::registerHooks -}; - } diff --git a/sca-cpp/trunk/modules/server/mod-eval.hpp b/sca-cpp/trunk/modules/server/mod-eval.hpp new file mode 100644 index 0000000000..4f95977fea --- /dev/null +++ b/sca-cpp/trunk/modules/server/mod-eval.hpp @@ -0,0 +1,422 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * HTTPD module used to eval component implementations. + */ + +#include "string.hpp" +#include "stream.hpp" +#include "function.hpp" +#include "list.hpp" +#include "tree.hpp" +#include "value.hpp" +#include "element.hpp" +#include "monad.hpp" +#include "../atom/atom.hpp" +#include "../json/json.hpp" +#include "../scdl/scdl.hpp" +#include "../http/curl.hpp" +#include "../http/httpd.hpp" + +extern "C" { +extern module AP_MODULE_DECLARE_DATA mod_tuscany_eval; +} + +namespace tuscany { +namespace server { +namespace modeval { + +/** + * Server configuration. + */ +class ServerConf { +public: + ServerConf(server_rec* s) : s(s), home(""), wiringServerName("") { + } + + const server_rec* s; + string home; + string wiringServerName; +}; + +/** + * Directory configuration. + */ +class DirConf { +public: + DirConf(char* dirspec) : dirspec(dirspec), contributionPath(""), compositeName("") { + } + const char* dirspec; + string contributionPath; + string compositeName; + list implementations; +}; + +/** + * Convert a result represented as a content + failure pair to a + * failable monad. + */ +const failable failableResult(const list& v) { + if (isNil(cdr(v))) + return car(v); + return mkfailure(string(cadr(v))); +} + +/** + * Handle an HTTP GET. + */ +const failable get(request_rec* r, const lambda&)>& impl) { + debug(r->uri, "modeval::get::uri"); + + // Inspect the query string + const list > args = httpd::queryArgs(r); + const list ia = assoc(value("id"), args); + const list ma = assoc(value("method"), args); + + // Evaluate a JSON-RPC request and return a JSON result + if (!isNil(ia) && !isNil(ma)) { + + // Extract the request id, method and params + const value id = cadr(ia); + const value func = c_str(json::funcName(string(cadr(ma)))); + + // Apply the requested function + const failable val = failableResult(impl(cons(func, httpd::queryParams(args)))); + if (!hasContent(val)) + return mkfailure(reason(val)); + + // Return JSON result + json::JSONContext cx; + return httpd::writeResult(json::jsonResult(id, content(val), cx), "application/json-rpc", r); + } + + // Evaluate an ATOM GET request and return an ATOM feed representing a collection of resources + const list path(httpd::pathValues(r->uri)); + if (isNil(cddr(path))) { + const failable val = failableResult(impl(cons("getall", list()))); + if (!hasContent(val)) + return mkfailure(reason(val)); + return httpd::writeResult(atom::writeATOMFeed(atom::feedValuesToElements(content(val))), "application/atom+xml;type=feed", r); + } + + // Evaluate an ATOM GET and return an ATOM entry representing a resource + const failable val = failableResult(impl(cons("get", mklist(caddr(path))))); + if (!hasContent(val)) + return mkfailure(reason(val)); + return httpd::writeResult(atom::writeATOMEntry(atom::entryValuesToElements(content(val))), "application/atom+xml;type=entry", r); +} + +/** + * Handle an HTTP POST. + */ +const failable post(request_rec* r, const lambda&)>& impl) { + const list ls = httpd::read(r); + debug(r->uri, "modeval::post::url"); + debug(ls, "modeval::post::input"); + + // Evaluate a JSON-RPC request and return a JSON result + const string ct = httpd::contentType(r); + if (contains(ct, "application/json-rpc") || contains(ct, "text/plain")) { + json::JSONContext cx; + const list json = elementsToValues(content(json::readJSON(ls, cx))); + const list > args = httpd::postArgs(json); + + // Extract the request id, method and params + const value id = cadr(assoc(value("id"), args)); + const value func = c_str(json::funcName(cadr(assoc(value("method"), args)))); + const list params = (list)cadr(assoc(value("params"), args)); + + // Evaluate the request expression + const failable val = failableResult(impl(cons(func, params))); + if (!hasContent(val)) + return mkfailure(reason(val)); + + // Return JSON result + return httpd::writeResult(json::jsonResult(id, content(val), cx), "application/json-rpc", r); + } + + // Evaluate an ATOM POST request and return the location of the corresponding created resource + if (contains(ct, "application/atom+xml")) { + + // Evaluate the request expression + const value entry = atom::entryValue(content(atom::readEntry(ls))); + const failable val = failableResult(impl(cons("post", mklist(entry)))); + if (!hasContent(val)) + return mkfailure(reason(val)); + + // Return the created resource location + apr_table_setn(r->headers_out, "Location", apr_pstrdup(r->pool, httpd::url(content(val), r))); + r->status = HTTP_CREATED; + return OK; + } + + return HTTP_NOT_IMPLEMENTED; +} + +/** + * Handle an HTTP PUT. + */ +const failable put(request_rec* r, const lambda&)>& impl) { + const list ls = httpd::read(r); + debug(r->uri, "modeval::put::url"); + debug(ls, "modeval::put::input"); + + // Evaluate an ATOM PUT request and update the corresponding resource + const list path(httpd::pathValues(r->uri)); + const value entry = atom::entryValue(content(atom::readEntry(ls))); + const failable val = failableResult(impl(cons("put", mklist(caddr(path), entry)))); + if (!hasContent(val)) + return mkfailure(reason(val)); + if (val == value(false)) + return HTTP_NOT_FOUND; + return OK; +} + +/** + * Handle an HTTP DELETE. + */ +const failable del(request_rec* r, const lambda&)>& impl) { + debug(r->uri, "modeval::delete::url"); + + // Evaluate an ATOM delete request + const list path(httpd::pathValues(r->uri)); + if (isNil(cddr(path))) { + + // Delete a collection of resources + const failable val = failableResult(impl(cons("deleteall", list()))); + if (!hasContent(val)) + return mkfailure(reason(val)); + if (val == value(false)) + return HTTP_NOT_FOUND; + return OK; + } + + // Delete a resource + const failable val = failableResult(impl(cons("delete", mklist(caddr(path))))); + if (!hasContent(val)) + return mkfailure(reason(val)); + if (val == value(false)) + return HTTP_NOT_FOUND; + return OK; +} + +/** + * Translate a component request. + */ +int translate(request_rec *r) { + gc_scoped_pool pool(r->pool); + if (strncmp(r->uri, "/components/", 12) != 0) + return DECLINED; + r->handler = "mod_tuscany_eval"; + return OK; +} + +/** + * HTTP request handler. + */ +int handler(request_rec *r) { + gc_scoped_pool pool(r->pool); + if(strcmp(r->handler, "mod_tuscany_eval")) + return DECLINED; + httpdDebugRequest(r, "modeval::handler::input"); + + // Set up the read policy + const int rc = httpd::setupReadPolicy(r); + if(rc != OK) + return rc; + + // Get the component implementation lambda + DirConf& dc = httpd::dirConf(r, &mod_tuscany_eval); + const list path(httpd::pathValues(r->uri)); + const list impl(assoctree(cadr(path), dc.implementations)); + if (isNil(impl)) + return HTTP_NOT_FOUND; + + // Handle HTTP method + const lambda&)> l(cadr(impl)); + if (r->header_only) + return OK; + if(r->method_number == M_GET) + return httpd::reportStatus(get(r, l)); + if(r->method_number == M_POST) + return httpd::reportStatus(post(r, l)); + if(r->method_number == M_PUT) + return httpd::reportStatus(put(r, l)); + if(r->method_number == M_DELETE) + return httpd::reportStatus(del(r, l)); + return HTTP_NOT_IMPLEMENTED; +} + +/** + * Convert a list of component references to a list of HTTP proxy lambdas. + */ +const value mkproxy(const value& ref, const string& base) { + return lambda&)>(http::proxy(base + string(scdl::name(ref)))); +} + +const list proxies(const list& refs, const string& base) { + if (isNil(refs)) + return refs; + return cons(mkproxy(car(refs), base), proxies(cdr(refs), base)); +} + +extern const failable&)> > readImplementation(const string& itype, const string& path, const list& px); + +const value confImplementation(DirConf& dc, ServerConf& sc, server_rec& server, const value& comp) { + const value impl = scdl::implementation(comp); + const string path = dc.contributionPath + string(scdl::uri(impl)); + + // Convert component references to configured proxy lambdas + ostringstream base; + if (sc.wiringServerName == "") + base << (server.server_scheme == NULL? "http" : server.server_scheme) + << "://" << (server.server_hostname == NULL? "localhost" : server.server_hostname) + << ":" << (server.port == 0? 80 : server.port) + << "/references/" << string(scdl::name(comp)) << "/"; + else + base << sc.wiringServerName << "/references/" << string(scdl::name(comp)) << "/"; + const list px(proxies(scdl::references(comp), str(base))); + + // Read and configure the implementation + const failable&)> > cimpl(readImplementation(elementName(impl), path, px)); + if (!hasContent(cimpl)) + return reason(cimpl); + return content(cimpl); +} + +/** + * Return a tree of component-name + configured-implementation pairs. + */ +const list componentToImplementationAssoc(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { + if (isNil(c)) + return c; + return cons(mklist(scdl::name(car(c)), confImplementation(dc, sc, server, car(c))), componentToImplementationAssoc(dc, sc, server, cdr(c))); +} + +const list componentToImplementationTree(DirConf& dc, ServerConf& sc, server_rec& server, const list& c) { + return mkbtree(sort(componentToImplementationAssoc(dc, sc, server, c))); +} + +/** + * Read the components declared in a composite. + */ +const failable > readComponents(const string& path) { + ifstream is(path); + if (fail(is)) + return mkfailure >(string("Could not read composite: ") + path); + return scdl::components(readXML(streamList(is))); +} + +/** + * Configure the components declared in the deployed composite. + */ +const bool confComponents(DirConf& dc, ServerConf& sc, server_rec& server) { + if (dc.contributionPath == "" || dc.compositeName == "") + return true; + const failable > comps = readComponents(dc.contributionPath + dc.compositeName); + if (!hasContent(comps)) + return true; + dc.implementations = componentToImplementationTree(dc, sc, server, content(comps)); + debug(dc.implementations, "modeval::confComponents::implementations"); + return true; +} + +/** + * Configuration commands. + */ +const char *confHome(cmd_parms *cmd, unused void *c, const char *arg) { + gc_scoped_pool pool(cmd->pool); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); + sc.home = arg; + return NULL; +} +const char *confWiringServerName(cmd_parms *cmd, unused void *c, const char *arg) { + gc_scoped_pool pool(cmd->pool); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); + sc.wiringServerName = arg; + return NULL; +} +const char *confContribution(cmd_parms *cmd, void *c, const char *arg) { + gc_scoped_pool pool(cmd->pool); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); + DirConf& dc = *(DirConf*)c; + dc.contributionPath = arg; + confComponents(dc, sc, *cmd->server); + return NULL; +} +const char *confComposite(cmd_parms *cmd, void *c, const char *arg) { + gc_scoped_pool pool(cmd->pool); + ServerConf& sc = httpd::serverConf(cmd, &mod_tuscany_eval); + DirConf& dc = *(DirConf*)c; + dc.compositeName = arg; + confComponents(dc, sc, *cmd->server); + return NULL; +} + +/** + * HTTP server module declaration. + */ +const command_rec commands[] = { + AP_INIT_TAKE1("TuscanyHome", (const char*(*)())confHome, NULL, RSRC_CONF, "Tuscany home directory"), + AP_INIT_TAKE1("SCAWiringServerName", (const char*(*)())confWiringServerName, NULL, RSRC_CONF, "SCA wiring server name"), + AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, ACCESS_CONF, "SCA contribution location"), + AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, ACCESS_CONF, "SCA composite location"), + {NULL, NULL, NULL, 0, NO_ARGS, NULL} +}; + +int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, unused server_rec *s) { + return OK; +} + +void childInit(apr_pool_t* p, server_rec* svr_rec) { + gc_scoped_pool pool(p); + ServerConf* c = (ServerConf*)ap_get_module_config(svr_rec->module_config, &mod_tuscany_eval); + if(c == NULL) { + cerr << "[Tuscany] Due to one or more errors mod_tuscany_eval loading failed. Causing apache to stop loading." << endl; + exit(APEXIT_CHILDFATAL); + } +} + +void registerHooks(unused apr_pool_t *p) { + ap_hook_post_config(postConfig, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_child_init(childInit, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_handler(handler, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_translate_name(translate, NULL, NULL, APR_HOOK_FIRST); +} + +} +} +} + +extern "C" { + +module AP_MODULE_DECLARE_DATA mod_tuscany_eval = { + STANDARD20_MODULE_STUFF, + // dir config and merger + tuscany::httpd::makeDirConf, NULL, + // server config and merger + tuscany::httpd::makeServerConf, NULL, + // commands and hooks + tuscany::server::modeval::commands, tuscany::server::modeval::registerHooks +}; + +} diff --git a/sca-cpp/trunk/modules/server/mod-python.hpp b/sca-cpp/trunk/modules/server/mod-python.hpp deleted file mode 100644 index 219613d1e3..0000000000 --- a/sca-cpp/trunk/modules/server/mod-python.hpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -#ifndef tuscany_modpython_hpp -#define tuscany_modpython_hpp - -/** - * Evaluation functions used by mod-eval to evaluate implementation.python - * component implementations. - */ - -#include "string.hpp" -#include "stream.hpp" -#include "function.hpp" -#include "list.hpp" -#include "value.hpp" -#include "debug.hpp" -#include "monad.hpp" -#include "../python/eval.hpp" -#include "../http/httpd.hpp" - -namespace tuscany { -namespace server { -namespace modpython { - -/** - * Evaluate a script component implementation function. - */ -struct evalImplementation { - PyObject* impl; - const list px; - evalImplementation(PyObject* impl, const list& px) : impl(impl), px(px) { - } - const value operator()(const list& params) const { - const value expr = append(params, px); - debug(expr, "modeval::python::evalImplementation::input"); - const failable val = python::evalScript(expr, impl); - debug(val, "modeval::python::evalImplementation::result"); - if (!hasContent(val)) - return mklist(value(), reason(val)); - return mklist(content(val)); - } -}; - -/** - * Read a script component implementation. - */ -const failable&)> > readImplementation(const string& path, const list& px) { - ifstream is(path); - if (fail(is)) - return mkfailure&)> >(string("Could not read implementation: ") + path); - const failable impl = python::readScript(path, is); - if (!hasContent(impl)) - return mkfailure&)> >(reason(impl)); - return lambda&)>(evalImplementation(content(impl), px)); -} - -} -} -} - -#endif /* tuscany_modpython_hpp */ diff --git a/sca-cpp/trunk/modules/server/mod-scheme.hpp b/sca-cpp/trunk/modules/server/mod-scheme.hpp index d62578544f..5c6ea9ef7d 100644 --- a/sca-cpp/trunk/modules/server/mod-scheme.hpp +++ b/sca-cpp/trunk/modules/server/mod-scheme.hpp @@ -32,7 +32,6 @@ #include "function.hpp" #include "list.hpp" #include "value.hpp" -#include "debug.hpp" #include "monad.hpp" #include "../scheme/eval.hpp" #include "../http/httpd.hpp" diff --git a/sca-cpp/trunk/modules/server/mod-wiring.cpp b/sca-cpp/trunk/modules/server/mod-wiring.cpp index 6b4308edda..88a57659ef 100644 --- a/sca-cpp/trunk/modules/server/mod-wiring.cpp +++ b/sca-cpp/trunk/modules/server/mod-wiring.cpp @@ -31,7 +31,6 @@ #include "list.hpp" #include "tree.hpp" #include "value.hpp" -#include "debug.hpp" #include "monad.hpp" #include "../scdl/scdl.hpp" #include "../http/httpd.hpp" diff --git a/sca-cpp/trunk/modules/server/scheme-conf b/sca-cpp/trunk/modules/server/scheme-conf new file mode 100755 index 0000000000..18f3a885bc --- /dev/null +++ b/sca-cpp/trunk/modules/server/scheme-conf @@ -0,0 +1,26 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Generate a scheme server conf +here=`readlink -f $0`; here=`dirname $here` +root=`readlink -f $1` + +cat >>$root/conf/httpd.conf <>$root/conf/httpd.conf <>tmp/conf/httpd.conf < diff --git a/sca-cpp/trunk/modules/server/server-test.py b/sca-cpp/trunk/modules/server/server-test.py deleted file mode 100644 index cdef94e116..0000000000 --- a/sca-cpp/trunk/modules/server/server-test.py +++ /dev/null @@ -1,29 +0,0 @@ -# JSON-RPC test case - -def echo(x): - return x - -# ATOMPub test case - -def getall(): - return ("Sample Feed", "123456789", - ("Item", "111", (("'javaClass", "services.Item"), ("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99))), - ("Item", "222", (("'javaClass", "services.Item"), ("'name", "Orange"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 3.55))), - ("Item", "333", (("'javaClass", "services.Item"), ("name", "Pear"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 1.55)))) - -def get(id): - entry = (("'javaClass", "services.Item"), ("'name", "Apple"), ("'currencyCode", "USD"), ("'currencySymbol", "$"), ("'price", 2.99)) - return ("Item", id, entry) - -def post(entry): - return "123456789" - -def put(id, entry): - return true - -def deleteall(): - return true - -def delete(id): - return true - diff --git a/sca-cpp/trunk/modules/server/wiring-test b/sca-cpp/trunk/modules/server/wiring-test index 5b3a678f7f..29eef996e6 100755 --- a/sca-cpp/trunk/modules/server/wiring-test +++ b/sca-cpp/trunk/modules/server/wiring-test @@ -22,6 +22,7 @@ echo "Testing..." # Setup ../http/httpd-conf tmp 8090 htdocs ./server-conf tmp +./scheme-conf tmp cat >>tmp/conf/httpd.conf < -- cgit v1.2.3