research2: Draft architecture + figures
[swifty.git] / doc / research1 / src / example.tex
1
2
3 Here is an example of how to use swift library:
4
5 \textbf{Seeder example}
6 \begin{itemize}
7  \item The first step is to create socket:\\\\
8 \small{\emph{
9 \hspace*{1cm}   Swift s = socketSwift();
10 }}
11  \item The second step is to bind socket: \\\\
12 \small{\emph{
13 \hspace*{1cm}   SockSwiftaddr ssa = (SockSwiftaddr) calloc (1, sizeof(struct, sockSwiftaddr);\\
14 \hspace*{1cm}   Ssa-$>$sin_port = 9000;\\
15 \hspace*{1cm}   bindSwift(s, ssa, sizeof(*ssa));\\
16 }}
17  \item The third step is to listen from socket. The difference between normal socket listen is that after listen we will
18 have the request data, so we don’t need an accept step:\\\\
19 \small{\emph{
20 \hspace*{1cm}   SockSwiftaddr ret =  (SockSwiftaddr) calloc (1, sizeof(struct, sockSwiftaddr);\\
21 \hspace*{1cm}   char * buf = (char*) calloc(MAXSIZE, ret);\\
22 \hspace*{1cm}   while (1) \{\\
23 \hspace*{1.7cm} size_t len = ListenFromSocket(s, buf, MAXSIZE, 0, ret, sizeof(*ret));\\
24 \hspace*{1.7cm} sendToSwift (s, buf, len, 0, ret, sizeof(*ret));\\
25 \hspace*{1cm}   \}
26 }}
27 \end{itemize}
28
29 \textbf{Peer example}
30 \begin{itemize}
31  \item The first step is to create socket :\\\\
32 \small{\emph{
33 \hspace*{1cm}   Swift s = socketSwift();
34 }}
35  \item The second step is to make a request for receiving data:\\\\
36 \small{\emph{
37 \hspace*{1cm}   SockSwiftaddr from =  (SockSwiftaddr) calloc (1, sizeof(struct, sockSwiftaddr);\\
38 \hspace*{1cm}   from-$>$sin_addr.N = 1;\\
39 \hspace*{1cm}   from-$>$sin_addr.s_addr[0] = (192$<<$24) + (168$<<$16) + (1$<<$8) + 101\\
40 \hspace*{1cm}   char * buf = (char*) calloc(MAXSIZE, ret);\\
41 \hspace*{1cm}   size_t = recvFromSwift (s, buf, len, 0, from, sizeof(*from));\\
42 }}
43 \end{itemize}