2 * Copyright (C) 2008 Nokia Corporation
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.
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.
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.
20 /* Wrapper functions for basic NoTA I/O
21 * (sending, receiving, connecting, disconnecting).
23 * TODO: Update error handling to use perror throughout,
24 * once it is thoroughly implemented in h_in3.
32 // Activates a service. This version returns the listener socket,
34 int n_activate(int sid, void* key, void* callback)
37 // Create an initial socket for monitoring incoming connections
38 int socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
41 perror("Error creating socket");
44 nota_addr_t addr = { sid, 0 }; // { Service, Port }
46 sleep(2); //FIXME: this is so that l_in has time to update cmap
48 int error = Hbind(Hgetinstance(), socket, (struct sockaddr*)&addr, sizeof(addr));
50 perror("Error registering service");
55 // Set the socket to listen:
56 error = Hlisten(Hgetinstance(), socket, 1);
58 perror("Error listening to socket");
64 // In this version, 'sid' means the listener socket. In h_in3,
65 // deactivating a service means closing the socket bound to its
67 int n_deactivate(int sid, void* key)
69 return n_disconnect(sid);
72 int n_accept(int to, void* param1, void* param2)
75 int error = Haccept(Hgetinstance(), to, (struct sockaddr*)param1, (socklen_t*)param2);
78 perror("Error accepting incoming connection");
84 // Creates a socket and connects it to the service
85 int n_connect(int sid, void* key, void* callback)
88 printf("Connecting to service %d...\n", sid);
89 socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
92 printf("Error connecting to SID %d: %d\n", sid, socket);
95 nota_addr_t addr = { sid, 0 }; // Service, port
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));
101 printf("Error connecting to SID %d: %d\n", sid, error);
102 Hclose(Hgetinstance(), socket);
105 else printf("\tdone - socket %d connected to service %d.\n", socket, sid);
110 int n_send(int socket, void* buffer, int buffer_len, int mode)
114 error = Hsend(Hgetinstance(), socket, buffer, buffer_len, mode);
117 printf("Write error to socket %d: %d\n", socket, error);
125 int n_read(int socket, void *buffer, int buffer_len, int mode)
129 error = Hrecv(Hgetinstance(), socket, buffer, buffer_len, mode);
132 printf("Read error from socket %d: %d\n", socket, error);
139 // Close a connection
140 int n_disconnect(int socket)
142 printf("Closing socket %d\n", socket);
144 int error = Hclose(Hgetinstance(), socket);
146 printf("Error closing socket %d: %d\n", socket, error);