Initial commit
[jamendo] / branches / nota-show-app / src / notaio.c
diff --git a/branches/nota-show-app/src/notaio.c b/branches/nota-show-app/src/notaio.c
new file mode 100644 (file)
index 0000000..f417d5b
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2008 Nokia Corporation
+ * All rights reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as 
+ * published by the Free Software Foundation.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+/* Wrapper functions for basic NoTA I/O
+ * (sending, receiving, connecting, disconnecting).
+ *
+ * TODO: Update error handling to use perror throughout,
+ * once it is thoroughly implemented in h_in3.
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include "notaio.h"
+
+#ifdef USE_H_IN3
+
+// Activates a service. This version returns the listener socket,
+// if successful.
+int n_activate(int sid, void* key, void* callback)
+{
+       errno = 0;
+       // Create an initial socket for monitoring incoming connections
+       int socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
+       if (socket < 0)
+       {
+               perror("Error creating socket");
+               return socket;
+       }
+       nota_addr_t addr = { sid, 0 };  // { Service, Port }
+       errno = 0;
+       sleep(2);       //FIXME: this is so that l_in has time to update cmap
+       // Register service:
+       int error = Hbind(Hgetinstance(), socket, (struct sockaddr*)&addr, sizeof(addr));
+       if (error < 0) {
+               perror("Error registering service");
+               return error;
+       }
+
+       errno = 0;
+       // Set the socket to listen:
+       error = Hlisten(Hgetinstance(), socket, 1);
+       if (error < 0) {
+               perror("Error listening to socket");
+               return error;
+       }
+       return socket;
+}
+
+// In this version, 'sid' means the listener socket. In h_in3,
+// deactivating a service means closing the socket bound to its
+// address.
+int n_deactivate(int sid, void* key)
+{
+       return n_disconnect(sid);
+}
+
+int n_accept(int to, void* param1, void* param2)
+{
+       errno = 0;
+       int error = Haccept(Hgetinstance(), to, (struct sockaddr*)param1, (socklen_t*)param2);
+       if (errno != 0)
+       {
+               perror("Error accepting incoming connection");
+               return -errno;
+       }
+       return error;
+}
+
+// Creates a socket and connects it to the service
+int n_connect(int sid, void* key, void* callback)
+{
+       int socket;
+       printf("Connecting to service %d...\n", sid);
+       socket = Hsocket(Hgetinstance(), AF_NOTA, SOCK_STREAM, 0);
+       if (socket < 0)
+       {
+               printf("Error connecting to SID %d: %d\n", sid, socket);
+               return socket;
+       }
+       nota_addr_t addr = { sid, 0 };  // Service, port
+       errno = 0;
+       sleep(2);       //FIXME: this is so that l_in has time to update cmap
+       int error = Hconnect(Hgetinstance(), socket, (struct sockaddr*)&addr, sizeof(addr)); 
+       if (error < 0)
+       {
+               printf("Error connecting to SID %d: %d\n", sid, error);
+               Hclose(Hgetinstance(), socket);
+               return error;
+       }
+       else printf("\tdone - socket %d connected to service %d.\n", socket, sid);
+       return socket;
+}
+
+// Send data
+int n_send(int socket, void* buffer, int buffer_len, int mode)
+{
+       int error = 0;
+       errno = 0;
+       error = Hsend(Hgetinstance(), socket, buffer, buffer_len, mode);
+       if (error < 0)
+       {
+               printf("Write error to socket %d: %d\n", socket, error);
+               return error;
+       }
+       return error;
+}
+
+
+// Receive data
+int n_read(int socket, void *buffer, int buffer_len, int mode)
+{
+       int error = 0;
+       errno = 0;
+       error = Hrecv(Hgetinstance(), socket, buffer, buffer_len, mode);
+       if (error < 0)
+       {
+               printf("Read error from socket %d: %d\n", socket, error);
+       }
+       return error; 
+}
+
+
+
+// Close a connection
+int n_disconnect(int socket)
+{
+    printf("Closing socket %d\n", socket);
+       errno = 0;
+       int error = Hclose(Hgetinstance(), socket);
+    if (error < 0) 
+               printf("Error closing socket %d: %d\n", socket, error);
+       else 
+               printf("\tdone.\n");
+       return error;
+}
+
+#endif //USE_H_IN3
+