

This example shows some simple usage of ecore\+\_\+pipe. We are going to create a pipe, fork our process, and then the child is going to communicate to the parent the result of its processing through the pipe.

As always we start with our includes, nothing special\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}

\end{DoxyCodeInclude}
 The first thing we are going to define in our example is the function we are going to run on the child process, which, as mentioned, will do some processing and then will write the result to the pipe\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{do\_lengthy\_task(\mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}}\ *pipe)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ i,\ j;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *buffer;}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ 20;\ i++)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ sleep(1);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ buffer\ =\ malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char})\ *\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ i;\ j++)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ buffer[j]\ =\ \textcolor{charliteral}{'a'}\ +\ j;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(pipe,\ buffer,\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ free(buffer);}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(pipe,\ \textcolor{stringliteral}{"{}close"{}},\ 5);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
The sleep was added so the parent process would think the child process was doing something interesting...
\end{DoxyNote}
Next up is our function for handling data arriving in the pipe. It copies the data to another buffer, adds a terminating NULL and prints it. Also if it receives a certain string it stops the main loop(effectively ending the program)\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{handler(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *buf,\ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ len)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str\ =\ malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char})\ *\ len\ +\ 1);}
\DoxyCodeLine{\ \ \ memcpy(str,\ buf,\ len);}
\DoxyCodeLine{\ \ \ str[len]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}received\ \%u\ bytes\(\backslash\)n"{}},\ len);}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}content:\ \%s\(\backslash\)n"{}},\ (\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *)str);}
\DoxyCodeLine{\ \ \ free(str);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (len\ \&\&\ !strncmp(buf,\ \textcolor{stringliteral}{"{}close"{}},\ len\ <\ 5\ ?\ len\ :\ 5))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}close\ requested\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 And now on to our main function, we start by declaring some variables and initializing ecore\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}}\ *pipe;}
\DoxyCodeLine{\ \ \ pid\_t\ child\_pid;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}

\end{DoxyCodeInclude}
 And since we are talking about pipes let\textquotesingle{}s create one\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ pipe\ =\ \mbox{\hyperlink{group__Ecore__Pipe__Group_gaa1fb078d1865197887f558e19532e02d}{ecore\_pipe\_add}}(handler,\ NULL);}

\end{DoxyCodeInclude}
 Now we are going to fork\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ child\_pid\ =\ fork();}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
duh...
\end{DoxyNote}
The child process is going to do the our fancy processing\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!child\_pid)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_gab9df469d955d67dbcd95fd6f2dabdc79}{ecore\_pipe\_read\_close}}(pipe);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ do\_lengthy\_task(pipe);}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
It\textquotesingle{}s very important to call \mbox{\hyperlink{group__Ecore__Pipe__Group_gab9df469d955d67dbcd95fd6f2dabdc79}{ecore\+\_\+pipe\+\_\+read\+\_\+close()}} here so that the child process won\textquotesingle{}t read what it is writing to the pipe itself.
\end{DoxyNote}
And the parent is going to run ecore\textquotesingle{}s main loop waiting for some data\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_ga60099202e0880dd3de617ca14fa0c58b}{ecore\_pipe\_write\_close}}(pipe);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
Calling \mbox{\hyperlink{group__Ecore__Pipe__Group_ga60099202e0880dd3de617ca14fa0c58b}{ecore\+\_\+pipe\+\_\+write\+\_\+close()}} here isn\textquotesingle{}t important but since we aren\textquotesingle{}t going to write in the pipe it is good practice.
\end{DoxyNote}
And finally when done processing(the child) or done receiving(the parent) we delete the pipe and shutdown ecore\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Pipe__Group_gaa7ed35ca29f47097e30ccd83776d5866}{ecore\_pipe\_del}}(pipe);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
