19 Sep 2016
•
linux
•
csharp
•
mono
•
apache
On my previous post I wrote how to configure application virtual host. We can have multiple application in a virtual host like multiple application in a web site on IIS. To do that, the steps are the same as I did on my previous post. Let’s say we have a scenario, having a site config as the sample on previous article, an application port 99, default document root /var/www/vhosts/defaultsite/root
. The detail virtual host config as follow
<VirtualHost *:99>
ServerAdmin admin@test.com
ServerName neutro.local
ServerAlias neutrofoton.com *.neutrofoton.com
MonoServerPath neutro.local "/usr/bin/mod-mono-server4"
MonoDebug neutro.local true
MonoSetEnv neutro.local MONO_IOMAP=all
MonoApplications neutro.local "/:/var/www/vhosts/defaultsite/root"
MonoAutoApplication disabled
AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
DocumentRoot /var/www/vhosts/defaultsite/root
DirectoryIndex Default.aspx index.aspx index.html
<Location "/">
Allow from all
Order allow,deny
MonoSetServerAlias neutro.local
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
</Location>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
</IfModule>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The steps to enable site config is the same as privious post. To make virtual host have multiple applications, we have to modify the site config file as follow. In this sample, we are going to add 2 applications to this virtual host
<VirtualHost *:99>
ServerAdmin admin@test.com
ServerName neutro.local
ServerAlias neutrofoton.com *.neutrofoton.com
MonoServerPath neutro.local "/usr/bin/mod-mono-server4"
MonoDebug neutro.local true
MonoSetEnv neutro.local MONO_IOMAP=all
MonoApplications neutro.local "/:/var/www/vhosts/defaultsite/root"
MonoAutoApplication disabled
AddHandler mono .aspx .ascx .asax .ashx .config .cs .asmx .axd
DocumentRoot /var/www/vhosts/defaultsite/root
DirectoryIndex Default.aspx index.aspx index.html
<Location "/">
Allow from all
Order allow,deny
MonoSetServerAlias neutro.local
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
</Location>
Alias /project1 "/home/neutro/Workspace/dotnet/project1"
MonoApplications project1 "/project1:/home/neutro/Workspace/dotnet/project1"
MonoServerPath project1 "/usr/bin/mod-mono-server4"
<Location "/project1">
Allow from all
Order allow,deny
MonoSetServerAlias project1
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
Require all granted
</Location>
Alias /project2 "/home/neutro/Workspace/dotnet/project2"
MonoApplications project2 "/project2:/home/neutro/Workspace/dotnet/project2"
MonoServerPath project2 "/usr/bin/mod-mono-server4"
<Location "/project2">
Allow from all
Order allow,deny
MonoSetServerAlias project2
SetHandler mono
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
Require all granted
</Location>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
</IfModule>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
In the new virtual host configuration we have:
/project1
, with physical application’s files in /home/neutro/Workspace/dotnet/project1
/project2
, with physical application’s files in /home/neutro/Workspace/dotnet/project2
Let’s assume we have a simple index.aspx
file in /home/neutro/Workspace/dotnet/project1
and /home/neutro/Workspace/dotnet/project2
We need to give access to both folders and theirs file contents
$ find project1 -type d -exec chmod 755 {} \;
$ find project1 -type f -exec chmod 644 {} \;
$ find project2 -type d -exec chmod 755 {} \;
$ find project2 -type f -exec chmod 644 {} \;
After modifying the config file and give access to the application directories, we need to register the applications in /etc/mono-server4/debian.webapp
<apps>
<web-application>
<name>project1</name>
<vpath>/project1</vpath>
<path>/home/neutro/Workspace/dotnet/project1</path>
<vhost>neutro.local</vhost>
</web-application>
<web-application>
<name>project2</name>
<vpath>/project2</vpath>
<path>/home/neutro/Workspace/dotnet/project2</path>
<vhost>neutro.local</vhost>
</web-application>
</apps>
The final step is restart apache, and now we should be able to open in browser http://localhost:99/project1 or http://localhost:99/project2.
References
- http://stackoverflow.com/questions/19279286/fail-to-start-a-mono-site-with-apache2-404-error-with-mod-mono
18 Sep 2016
•
linux
•
csharp
•
mono
•
apache
I used to write a post about install and configure Mono on OS X Mountain Lion. On this post I want to summary what I did the similar things on Ubuntu.
The details environment I use :
- Ubuntu 14.04 LTS
- Mono JIT compiler version 4.4.2
- Apache 2.4.7
Install Mono, Apache 2, Mod Mono
To install Mono, the first step is add package repository to our system.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
$ echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
$ sudo apt-get update
To enable mod_mono installation on Ubuntu 13.10 or later, and Debian 8.0 and later (and their derivatives), we need to add a second repository to our system, in addition to the generic Debian/Ubuntu repository above (if you don’t use sudo, be sure to switch to root):
$ echo "deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list
Then run package update to update and a package upgrade to upgrade existing packages to the latest available.
$ sudo apt-get update
$ sudo apt-get upgrade
To install mono, run the following command
$ sudo apt-get install mono-complete
If we do not have Apache 2 on our system, we need to install it first. Or we can check the existing version apache2 -v
$ sudo apt-get install apache2
To be able to host ASP.NET application on apache, we need to install mod_mono
. mod_mono
is a module for the Apache HTTP Server that allows us to host ASP.NET pages and other assemblies on multiple platforms by using Mono.
To install Mod Mono and its dependencies run the following command
$ sudo apt-get update && sudo apt-get install libapache2-mod-mono
$ apt-get install mono-apache-server4
More …
17 Sep 2016
•
linux
•
virtualbox
VM VirtualBox is one of free popular hypervisor for x86 computer from Oracle. I use it as part of my development environment, since occasionally I need several different Operating Systems for development and software testing or deployment testing. When we work with VirtualBox we often need to share folder between host and guest operating system.
More …
11 May 2016
•
cpp
C++11 adds a new feature called lambda expressions. This allows us to write anonymous functions inline, removing the need to write a separate function or to write a function object, and makes code easier to understand.
For those who are familiar with C# lambda expression, C++ lambda expression is similar. But lambda in C++ has slightly different syntax.
[capture_block](parameters) mutable exception_specification -> return_type {body}
-
capture_block
is a comma-separated list of zero or more captures, optionally beginning with a capture-default. Capture list can be passed as follows (see below for the detailed description):
[a,&b]
where a is captured by value and b is captured by reference.
[this]
captures the this pointer by value
[&]
captures all automatic variables odr-used in the body of the lambda by reference
[=]
captures all automatic variables odr-used in the body of the lambda by value
[x]
captures only x by value and nothing else.
[]
captures nothing
-
parameters
is (optional) list of parameters for the lambda expression.
-
mutable
is (optional) if variables from the enclosing scope are captured by value, a copy of those variables will become available in the body of the lambda expression. Those copies are marked as const by default, meaning the lambda body cannot change the value of those copies. If the lambda expression is marked as mutable, those copies are not const and the body can modify those local copies.
-
exception_specification
is (optional) and can be used to specify which exceptions the body of the lambda expression can throw.
-
return_type
is Return type. If not present it's implied by the function return statements (or void if it doesn't return any value)
Lambda and Direct Invoke
The following snipped code is an example of lambda expression.
//example lambda and directly invoked
string result = [](const string& str) -> string {return "Hi " + str;}("neutro");
cout << result << endl;
The lambda expression above shows that it has a const string&
parameter and return a string
type. To execute the lambda is by placing round brackets ()
and put object that will be passed to inside it. The "neutro"
literal string will substitute str
parameter. The output of the above code is :
Lambda as Variable
Pointer to a lambda expression can be stored and executed through the function pointer. C++ provides std::function
which is a STL template class that provides a very convenient wrapper to a simple function, to a functor or to a lambda expression. We can also use the C++11 auto
keyword to make it easier.
To make it clear the following code contains couples of various lambda expressions and store them in variables of type auto
or its equivalent in std::function
STL template class.
//lambda as variable
auto lambda1 = [] { cout <<"Hello lambda without parameter 1" << endl; };
lambda1();
auto lambda2 = [](void) { cout <<"Hello lambda without parameter 2" << endl; };
lambda2();
//auto lambda3 = [](void) -> void { cout <<"Hello lambda without parameter 2" << endl; };
function<void()> lambda3 = [](void) -> void { cout <<"Hello lambda without parameter 2" << endl; };
lambda3();
//auto lambda4 = [](const string& str) -> string {return "Hello from " + str;};
function<string(const string&)> lambda4 = [](const string& str) -> string {return "Hello from " + str;};
string result = lambda4("neutro");
cout << result << endl;
The snipped code above shows how lambda expressions stored in variable of auto type or theirs equivalent in std::function
type and how to invoke the lambda. Here is a good article about std::function
.
Lambda Capture Block
Lambda Capture Block basically describes how we want to capture variables from the enclosing scope. Capturing a variable means that the variable becomes available inside the body of the lambda. The detail variant of capture block has been described on previous section. The following code shows various scenario how to play with capture block.
//lambda capture block
int variableA = 1;
int variableB = 1;
int variableC = 1;
cout << "Init Values : "<<endl;
cout << "variableA : "<<variableA <<endl;
cout << "variableB : "<<variableB <<endl;
cout << "variableC : "<<variableC <<endl;
cout << endl;
//--------------------------------------
cout << "---## lamda 1 ##---"<<endl;
auto lambda1 = [=](int param)-> int //==> [=] lambda has default access to variable is by value
{
param = param * 2;
cout << "param inside lambda1 : "<<param << endl;
return param;
};
lambda1(variableA);
cout << "After lamda1, variableA : " <<variableA <<endl;
cout<<endl;
//--------------------------------------------
cout << "---## lamda 2 ##---"<<endl;
auto lambda2 = [&](int param) -> int //==> [&] lambda has default access to variable is by reference
{
param = param * 2;
variableB = variableB * 2;
cout << "param inside lambda2 : "<<param << endl;
cout << "variableB inside lambda2 : "<<variableB << endl;
return param;
};
lambda2(variableA);
cout << "After lamda2, variableA : " <<variableA <<endl;
cout << "After lamda2, variableB : " <<variableB <<endl;
cout<<endl;
//-------------------------------------
cout << "---## lamda 3 ##---"<<endl;
auto lambda3 = [&](int* param) -> int
{
*param = (*param) * 2;
variableB = variableB * 2;
cout << "param inside lambda3 : "<<*param << endl;
cout << "variableB inside lambda3 : "<<variableB << endl;
return *param;
};
int result3 = lambda3(&variableA);
cout << "After lamda3, result3 : " <<result3 <<endl;
cout << "After lamda3, variableA : " <<variableA <<endl;
cout << "After lamda3, variableB : " <<variableB <<endl;
cout<<endl;
//--------------------------------
cout << "---## lamda 4 ##---"<<endl;
auto lambda4 = [=, &variableB, &variableC](int param) -> int //==> [=, &variableB, &variableC] captures by value by default,
// except variables variableB and variableC, which are captured by reference.
{
param = (param) * 2;
variableB = variableB * 2;
variableC = variableC * 2;
cout << "param inside lambda4 : "<<param << endl;
cout << "variableB inside lambda4 : "<<variableB << endl;
cout << "variableC inside lambda4 : "<<variableC << endl;
return param;
};
lambda4(variableA);
cout << "After lamda4, variableA : " <<variableA <<endl;
cout << "After lamda4, variableB : " <<variableB <<endl;
cout << "After lamda4, variableC : " <<variableC <<endl;
cout << endl;
//--------------------------------
cout << "---## lamda 5 ##---"<<endl;
auto lambda5 = [variableC](int param) mutable -> int //==> [variableC] captures only variableC by value and nothing else.
{
param = (param) * 2;
variableC = variableC * 2; //should mark as mutable lambda, since Cannot assign to a variable captured by copy in a non-mutable lambda
//variableB = variableB * 2; //ERROR : variableB not captured, since only capture specific variable that's variableC
///
cout << "param inside lambda5 : "<<param << endl;
cout << "variableC inside lambda5 : "<<variableC << endl;
return param;
};
lambda5(variableA);
cout << "After lamda5, variableA : " <<variableA <<endl;
cout << "After lamda5, variableC : " <<variableC <<endl;
The output of the sample code is :
Init Values :
variableA : 1
variableB : 1
variableC : 1
---## lamda 1 ##---
param inside lambda1 : 2
After lamda1, variableA : 1
---## lamda 2 ##---
param inside lambda2 : 2
variableB inside lambda2 : 2
After lamda2, variableA : 1
After lamda2, variableB : 2
---## lamda 3 ##---
param inside lambda3 : 2
variableB inside lambda3 : 4
After lamda3, result3 : 2
After lamda3, variableA : 2
After lamda3, variableB : 4
---## lamda 4 ##---
param inside lambda4 : 4
variableB inside lambda4 : 8
variableC inside lambda4 : 2
After lamda4, variableA : 2
After lamda4, variableB : 8
After lamda4, variableC : 2
---## lamda 5 ##---
param inside lambda5 : 4
variableC inside lambda5 : 4
After lamda5, variableA : 2
After lamda5, variableC : 2
Reference
- Wrox Professional C++
- http://en.cppreference.com/w/cpp/language/lambda
- https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/
- http://www.drdobbs.com/cpp/lambdas-in-c11/240168241?pgno=1
27 Mar 2016
•
xcode
•
code block
•
visual studio
•
macos
•
linux
•
windows
•
cpp
Boost is a set of libraries for the C++ programming language that provide support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains over eighty individual libraries.[2]
The other interesting points of Boost are :
- Open source
- Cross platform
- Complement to STL rather than a replacement
- Many of Boost developers are on the C++ standard committee
- Well documented
- Most of the Boost libraries are licensed under the Boost Software License, designed to allow Boost to be used with both free and proprietary software projects
Installation Boost
Before jumping into steps of configuring Boost on various IDE, let’s begin with Boost installation. To be noted that on this post I run Xcode on OS X, Code::Blocks on Linux (Ubuntu) and Visual Studio on Windows. The detail environments I use are :
- OS X 10.11.4 El Capitan
- Ubuntu 14.04.4 LTS
- Xcode Version 7.2
- Code::Blocks 13.12, gcc 4.8.4
- Visual Studio 2013
- Boost 1.60.0
OS X and Linux (Ubuntu)
There are several ways of Boost installation. Instead of build from source code, we can use package manager such as MacPorts, Homebrew, Advance Package Tool, etc. In this post we will build Boost from source code. The installation steps (from source code) on OS X and Ubuntu are the similar. To make it consistent, I use the same installation path for OS X and Ubuntu that is /usr/local/boost_1_60_0
. You can use different path if you want. The steps are :
- Download boost library from Boost website
- Extract it.
- Open terminal, navigate to the extracted directory
- Create directory on
/usr/local/boost_1_60_0
, and ensure IDE has access to the directory. On my case I don't need this step on OS X, but on ubuntu it does.
``` bash
sudo mkdir /usr/local/boost_1_60_0
sudo chmod 777 -r boost_1_60_0
```
-
Run command :
``` bash
./bootstrap.sh --prefix=/usr/local/boost_1_60_0
./b2 install
```
This last step quite take time. So you can have coffee while waiting for it :)
Once the installation finish, we should have generated directory. They are /usr/local/boost_1_60_0/include
contains header files and /usr/local/boost_1_60_0/lib
contains libraries.
Windows
The Boost installation step on Windows is also similar to the installation step on OS X and Ubuntu.
The steps are :
- Download boost library from Boost website
- Extract it to C:\\boost_1_60_0
- Open Visual Studio command prompt. I use Visual Studio 2013 x86 Native Tools Command Prompt native tool (I have not test using default Windows Command Prompt)
``` bat
C:\> cd C:\boost_1_60_0
C:\boost_1_60_0> bootstrap.bat
C:\boost_1_60_0> .\b2
```
As on OS X and Ubuntu, the last step quite take time.
More …