Initial commit
[jamendo] / branches / nota-show-app / src / notaio.c
1 /*
2  * Copyright (C) 2008 Nokia Corporation
3  * All rights reserved
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as 
7  * published by the Free Software Foundation.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 /* Wrapper functions for basic NoTA I/O
21  * (sending, receiving, connecting, disconnecting).
22  *
23  * TODO: Update error handling to use perror throughout,
24  * once it is thoroughly implemented in h_in3.
25  */
26 #include <stdio.h>
27 #include <unistd.h>
28 #include "notaio.h"
29
30 #ifdef USE_H_IN3
31
32 // Activates a service. This version returns the listener socket,
33 // if successful.
34 int n_activate(int sid, void* key, void* callback)
35 {
36         errno = 0;
37         // Create an initial socket for monitoring incoming connections
38         int socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
39         if (socket < 0)
40         {
41                 perror("Error creating socket");
42                 return socket;
43         }
44         nota_addr_t addr = { sid, 0 };  // { Service, Port }
45         errno = 0;
46         sleep(2);       //FIXME: this is so that l_in has time to update cmap
47         // Register service:
48         int error = Hbind(Hgetinstance(), socket, (struct sockaddr*)&addr, sizeof(addr));
49         if (error < 0) {
50                 perror("Error registering service");
51                 return error;
52         }
53
54         errno = 0;
55         // Set the socket to listen:
56         error = Hlisten(Hgetinstance(), socket, 1);
57         if (error < 0) {
58                 perror("Error listening to socket");
59                 return error;
60         }
61         return socket;
62 }
63
64 // In this version, 'sid' means the listener socket. In h_in3,
65 // deactivating a service means closing the socket bound to its
66 // address.
67 int n_deactivate(int sid, void* key)
68 {
69         return n_disconnect(sid);
70 }
71
72 int n_accept(int to, void* param1, void* param2)
73 {
74         errno = 0;
75         int error = Haccept(Hgetinstance(), to, (struct sockaddr*)param1, (socklen_t*)param2);
76         if (errno != 0)
77         {
78                 perror("Error accepting incoming connection");
79                 return -errno;
80         }
81         return error;
82 }
83
84 // Creates a socket and connects it to the service
85 int n_connect(int sid, void* key, void* callback)
86 {
87         int socket;
88         printf("Connecting to service %d...\n", sid);
89         socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
90         if (socket < 0)
91         {
92                 printf("Error connecting to SID %d: %d\n", sid, socket);
93                 return socket;
94         }
95         nota_addr_t addr = { sid, 0 };  // Service, port
96         errno = 0;
97         sleep(2);       //FIXME: this is so that l_in has time to update cmap
98         int error = Hconnect(Hgetinstance(), socket, (struct sockaddr*)&addr, sizeof(addr)); 
99         if (error < 0)
100         {
101                 printf("Error connecting to SID %d: %d\n", sid, error);
102                 Hclose(Hgetinstance(), socket);
103                 return error;
104         }
105         else printf("\tdone - socket %d connected to service %d.\n", socket, sid);
106         return socket;
107 }
108
109 // Send data
110 int n_send(int socket, void* buffer, int buffer_len, int mode)
111 {
112         int error = 0;
113         errno = 0;
114         error = Hsend(Hgetinstance(), socket, buffer, buffer_len, mode);
115         if (error < 0)
116         {
117                 printf("Write error to socket %d: %d\n", socket, error);
118                 return error;
119         }
120         return error;
121 }
122
123
124 // Receive data
125 int n_read(int socket, void *buffer, int buffer_len, int mode)
126 {
127         int error = 0;
128         errno = 0;
129         error = Hrecv(Hgetinstance(), socket, buffer, buffer_len, mode);
130         if (error < 0)
131         {
132                 printf("Read error from socket %d: %d\n", socket, error);
133         }
134         return error; 
135 }
136
137
138
139 // Close a connection
140 int n_disconnect(int socket)
141 {
142     printf("Closing socket %d\n", socket);
143         errno = 0;
144         int error = Hclose(Hgetinstance(), socket);
145     if (error < 0) 
146                 printf("Error closing socket %d: %d\n", socket, error);
147         else 
148                 printf("\tdone.\n");
149         return error;
150 }
151
152 #endif //USE_H_IN3
153