quick-start-guide.pdf
(
212 KB
)
Pobierz
Tntnet quick start
Authors: Tommi Mäkitalo, Andreas Welchlin
This quick start includes:
•
how to install tntnet
•
build and run your first application
•
explanation of this first application
•
further readings
Tntnet is developped and tested with linux. It is known to run on Sun Solaris, IBM AIX
and freeBSD.
Installation
For installing Tntnet you need to install cxxtools before.
You find cxxtools on the tntnet homepage
http://www.tntnet.org/download.hms
and
install it with:
tar xzf cxxtools-2.0.tar.gz
cd cxxtools-2.0
./configure
make
su -c 'make install'
Same installation-procedure with tntnet. Install it with:
tar xzf tntnet-2.0.tar.gz
cd tntnet-2.0
./configure
make
su -c 'make install'
Now you have a working Tntnet-Environment.
How to create your first webapplication
To create a webapplication it is necessary to create some initial projectfiles. This is
achieved by entering:
tntnet-config --project=myfirstproject
This creates:
•
a directory “myfirstproject”
•
a source-file “myfirstproject.ecpp” containing your application
•
a configurationfile “tntnet.conf”
•
a property-file for logging-configuration “tntnet.properties”
•
a Makefile
To build the application change to the new directory and execute „make“.
To run the application enter „tntnet -c tntnet.conf“.
Now you can start your Webbrowser and navigate to
http://localhost:8000/myfirstproject
.
You can see the result of your first running tntnet-application, which prints the name of
the application.
What have we done?
The sourcefile myfirstproject.ecpp has been translated to c++. This c++ programm
was used to build a shared library which contains the whole webapplication.
A tntnet-webapplication is a simple web-page with special tags like
<$
...
$>
. The
ecppc-compiler creates a c++-sourcefile and a headerfile with the same basename.
They include a class, which has also the same named as the file. You can look into the
generated code, if you want and sometimes it is useful to read it for further
understanding of tntnet-applications. If the c++-compiler has problems with your
application it is also the best choice to look into the generated code.
Please keep in mind that the linenumbers which are printed by the c++-compiler on
errors correspond to the generated cpp-file. They are not the linenumbers of your
ecpp-source.
The tags
<$
...
$>
include a c++-expression. The result of the expression is printed
into the resulting page, when the page is requested (on runtime). Therefore a
std::ostream
is used, so that the type of the result can be any object, which has an
outputoperator
operator<<(ostream&, T)
defined.
The configurationfile „tntnet.conf“ has 3 configurationvariables. “
Listen
” configures
the (local) IP-adress and tcp-port, where tntnet will listen for incoming requests. If no
port is configured, the default is 80, which is normally only possible when tntnet runs
with root privileges.
The variable “
PropertyFile
” tells tntnet, where to find the logging-configuration.
The entry “
MapUrl
” is the most important one. It tells tntnet what to do with incoming
requests. Without this entry, tntnet answeres every request with „
http-error 404 – not
found“
. “
MapUrl
” maps the url - which is sent from a webbrowser - to a tntnet-
component. A component is the piece of code, which is normally generated by the
ecpp-compiler (
ecppc
).
That's what we did obove with myfirstproject.ecpp. Components are identified by their
(class-)name and the shared library which contains this class. We named our class
“myfirstproject” and our shared library “myfirstproject.so”. The component-identifier is
then myfirstproject@myfirstproject .
So “MapUrl” tells tntnet to call this component, when the url /test.html is requested.
How to add an image to your webapplication
A nice webapplication is colorfull and has some images. Let's add one.
Create or fetch some pictures. Say you have a picture “
picture.jpg”
. Put it into your
working-directory.
Modify your html-page “myfirstproject .ecpp” to show an image, first:
<html>
<head>
<title>ecpp-application myfirstproject</title>
</head>
<body>
<h1>myfirstproject</h1>
<img src=”picture.jpg”>
</body>
</html>
Next we compile the modified webpage including the picture and link everything
together. We need to tell the ecpp-compiler (
ecppc)
, that the picture is a binary file and
which mime-type to generate. The flag -b tells ecppc not to look for tags like <$...$>.
The component needs to tell the browser the mime-type which is “image/jpg” for your
picture. The option
-m
is used to tell ecppc the mime-type. The picture will be
compiled into the component.
ecppc myfirstproject.ecpp
g++ -c -fPIC myfirstproject.cpp
ecppc -b -m image/jpeg picture.jpg
g++ -c -fPIC picture.cpp
g++ -o myfirstproject.so -shared myfirstproject.o picture.o -lecpp
But you can compile this easier by editing the generated Makefile and change the line:
myfirstproject.so: myfirstproject.o
to:
myfirstproject.so: myfirstproject.o picture.o
Before tntnet is started it is necessary to extend our configuration. Tntnet needs to
know, that “picture.jpg” is found in the shared library “myfirstproject.so”. Our new
tntnet.conf
looks like this:
MapUrl /myfirstproject.html myfirstproject@myfirstproject
MapUrl /picture.jpg picture@myfirstproject
Listen 0.0.0.0 8000
PropertyFile tntnet.properties
Now we start our modified webapplication which is found in myfirstproject.so. Start
tntnet like before and look at the modified page including your image.
Generalise the configuration
When adding new pages to tntnet applications you have to ensure, that tntnet finds all
the components. Until now we have added each single component into tntnet.conf.
There is a way to generalise it by using regular expressions. Just modify tntnet.conf
like this:
MapUrl /(.*).html $1@ myfirstproject
MapUrl /(.*).jpg $1@ myfirstproject
Listen 0.0.0.0 8000
PropertyFile tntnet.properties
Every request will be checked by tntnet for matching the first of all regular expressions
which are defined. Every request with the suffix “.html” or “.jpg” makes tntnet to look
for a component with the basename of the request. Ok – there is one funny thing in our
configuration: we get our picture with http://localhost:8000/picture.html. But tntnet
does not care and nor does the browser.
Adding some C++-processing
Tntnet is made for writing web applications in C++. In the first example you saw one
type of tag:
<$
...
$>
. This encloses a C++-expression, which is evaluated and printed
on the resulting page.
Web applications often need to do some processing like fetching data from a database
or something. The tags
<{
...
}>
enclose a C++-processing-block. This C++-code is
executed, when a browser sends a request to fetch the page.
As a short form you can put the character '
%
' into the first column, which means, that
the rest of the line is C++.
We change our myfirstproject.ecpp to look like that:
<html>
<head>
<title>ecpp-application myfirstproject</title>
</head>
<body>
<h1>myfirstproject</h1>
<{
// we have a c++-block here
double arg1 = 1.0;
double arg2 = 3.0;
double result = arg1 + arg2;
}>
Plik z chomika:
Satoremihi7
Inne pliki z tego folderu:
gxemul compiled.7z
(7660 KB)
bonk-0.6.tar.gz
(17 KB)
算法导论.pdf
(12811 KB)
888.11.algo.2.pdf
(161 KB)
nnedi3.zip
(12470 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 16.11.2022
Pliki dostępne do 19.01.2025
Pliki dostępne do 19.04.2020
;)
Zgłoś jeśli
naruszono regulamin