You are on page 1of 6

AS-0.

1102 - project document


111 - CSP Authors:
Filip Alexander Gran (67030V) fgrano@cc.hut.fi Perttu Matias Heikkil (66586M) pmheikki@cc.hut.fi Jani Tapani Koskela (80469E) jtkoske1@cc.hut.fi

Last updated:
8.12.2008

Instructions for compiling and use


CSP has been compiled and tested on GNU/Linux GCC 4.2 & 4.3, cmake 2.6, Apache 2.2.10. It uses no special libraries and should work on most platforms. To install goto build directory and invoke ./mi or cmake ../ -DCMAKE_BUILD_TYPE=Debug DCMAKE_INSTALL_PREFIX=/usr/ ; make install. After this you can invoke csp filename.csp to interpret a script. C++ source will be written to filename.cc and binary to filename.cgi. CSP is used to interpret csp-scripts into c++ code and compile the resulting program. It recognizes a set of tags and interprets them separately, scripting elements can also be used to implement most things. Available tags: Library include: <%@ page import="supportlib.hh" %> Includes a library, places a #include directive at the top of the output. Can be used anywhere in the csp-script excluding scripting elements. File include: <%@ include file=header.csp %> Includes a file at the location where the tag is placed, this file is also interpreted. Recursion works as well (you could place another file include inside the included file). Can be used anywhere excluding scripting elements. Scripting element: <% int c = 0 %> Everything inside the <% and %> tag is copied directly to the resulting file and will be interpreted by g++. Scripting elements can extend over several lines. Supportlib string Support::getParams(string paramname) returns GET parameter from QUERY_STRING environmental variable int Support::checkEnv() checks if QUERY_STRING exists, returns 0 if QUERY STRING contains parameters else returns 1 string Session::getCookie(string paramname) returns HTTP_COOKIE parameter with paramname Cookie <c:cookie param="CSPID" value="732423sdfs73242"> Adds a new parameter to the cookie. At least one param-value pair is required to

use cookies. At first use creates a new Session. Can be used anywhere except inside scripting elements and after <c:cookie expire= > tag. <c:cookie expire="1"> Outputs the cookie above mimetype. Can only be used once and no param-value pairs can be used after it. Flow Control IF tag: <c:if test="${number != 0}"> <c:/if> Inserts an if statement into the script, test=${ } uses the same syntax as standard C if. Foreach: <c:foreach var=i items=${array}> <c:/foreach> Not properly implemented yet. If the user wishes to use Cookies or supportlib functionality supportlib.hh must be included. Example runs: Test 1: ~/C++/CSP$ ./CSP demo.csp Test 2: ~/C++/CSP$ export REQUEST_METHOD="GET" ~/C++/CSP$ export QUERY_STRING="fname=Matti&lname=Meik%C3%A4l%C3%A4inen" ~/C++/CSP$ ./CSP demo2.csp INPUT(.csp) OUTPUT(.cc) <%@ page import="supportlib.hh" %> <%@ include file="header.csp" %> <% int number = 5; %> <c:if test="${number == 5}" > <% std::cout << "Numero tosiaan on viisi!" << std::endl; %> <c:/if> </body> </html> #include <iostream> #include "supportlib.hh" int main(void){ std::cout << "Contenttype:text/html;charset=iso-8859-1\r\n\r\n" << std::endl; std::cout << "<html>\n"; std::cout << "<body>\n"; std::cout << "<h1>Guest book</h1>\n"; std::cout << "<br>\n"; int number = 5; if (number == 5) { std::cout << "Numero tosiaan on viisi!" << std::endl; } std::cout << "</body>\n"; std::cout << "</html>\n"; } <%@ page import="supportlib.hh" %> <c:cookie param="demoID" value="666"> <c:cookie expire="1"> #include <iostream> #include "supportlib.hh" int main(void){

<html> <body> <h1>Support Library Demo</h1> <br> <% Support suplib; std::string firstname, lastname; firstname = suplib.getParam("fname"); lastname = suplib.getParam("lname"); %> <c:if test="${firstname.compare("") != 0}"> <% std::cout << "Nimesi on " << firstname << " " << lastname << std::endl; %> <c:/if> </body> </html>

Session session; session.setCookie("demoID", "666"); std::cout << session.setCookie(1) << std::endl; std::cout << "Contenttype:text/html;charset=iso-8859-1\r\n\r\n" << std::endl; std::cout << "<html>\n"; std::cout << "<body>\n"; std::cout << "<h1>Support Library Demo</h1>\n"; std::cout << "<br>\n"; Support suplib; std::string firstname, lastname; firstname = suplib.getParam("fname"); lastname = suplib.getParam("lname"); if (firstname.compare("") != 0) { std::cout << "Nimesi on " << firstname << " " << lastname << std::endl; } std::cout << "</body>\n"; std::cout << "</html>\n"; }

Program architecture
CSP-compiler consists of 4 modules, CSPCompiler (main) preprosesses the script and writes everything to a stringstream. This stringstream is then sent to the interpreter for further processing. The taglib parses all tags starting with <c: and returns the interpretion. Write has separate functions for writing headers, body and mimetype. Supportlibrary is included in the resulting c++ application and includes functions used at runtime to handle HTTP sessions and read parameters submitted via HTTP GET. If Interpreter completes successfully (no exceptions are thrown) main() writes the .cc file and uses g++ to compile it. The diagram below illustrates the structure of the program. The structure of the program evolved somewhat over the course of the project. The structure is fairly modular, simple and can be extended without too many changes. Taglib was intended to include a tag extension API but we didn't have enough time to implement it. Supportlibrary can easily be replaced with a user defined library. Every module has a clear purpose.

Class Diagram 1.

Data structures and algorithms


C++ Standard Template Library (STL) containers and algorithms have been used in project. Supportlibrary has two algorithms and those functions are in parseString and parseCookie. These both parse input strings to find parameter-value pairs and put those pairs in map container. In main there is preprocess function that uses recursion to go through all included files.

Known bugs
Supportlib::getParam() supports and other special characters but this only works on command line, not in the browser. Foreach is not properly implemented. We'll try to fix these shortcomings before the demo! Extra whitespaces within tags are generally not supported and will cause errors.

Tasks sharing and schedule


The group met up frequently to discuss strategies and plans. IRC and email was also used to help group members communicate, request help on different tasks and share ideas. Initially the responsibility of each group member was shared in the following way: Perttu: Support Library Jani: Main, write Filip: Interpreter 18.11.2008 we met to re-evaluate the workload and responsibilities in the group. Perttu: Support Library, documentation Jani: Main, test application, if and foreach implementation, documentation Filip: Interpreter, taglib, write, documentation We remained well on schedule until the middle of the project, some limitations in our design and lack of time were the main causes of delays. Project planning 28.10.2008 Meeting 30.10.2008 Meeting 3.11.2008 Meeting 4.11.2008 Meeting, project plan finished 7.11.2008 Project plan deadline Assistant meeting 10.11.2008 Programming 14.11.2008 Meeting 18.11.2008 Basic functionality implemented, meeting to re-evaluate workload. 24.11.2008 Documentation and project status meeting 27.11.2008 Meeting 5.12.2008 Feature freeze 8.12.2008 Testing 8.12.2008 Meeting, documentation finished, demo application finished, CSP finished Demo 9.12.2008 We initially thought that the supportlibrary would be possible to develop at the same time as the other modules and syntax were constantly changing, this proved to be difficult. The more accurate specification of syntax and structure in the planning stage would've been an advantage later on. Also the idea that every part of the program was the responsibility of every member of the group didn't work as well as we had hoped. Time spent by each member Perttu: 30h + 24h Jani: 23h + 26h Filip: 30h + 26h

Differences to the original plan


We decided to add a separate taglib that could be easily extended if wanted and supportlib was changed from a library used by CSP to a library used by the resulting script at runtime. Other than that we followed the initial plan. The plan could've been slightly more specific regarding http sessions and flow control tags.

References
Stanley B. Lippman, Jose Lajoie, Barbara E. Moo: C++ Primer (4th Edition).

You might also like