1 /***************************************************************************
4 * Thu Nov 19 14:23:57 2009
5 * Copyright 2009 Marcin Miklas
6 * <marcin.miklas@teleca.com>
7 ****************************************************************************/
12 #include <netinet/in.h>
16 static void shut_down(void);
17 static int disconnect(int socket, ServiceCallbacks* cb);
21 unsigned char* last_image=NULL;
22 int last_image_size = 0;
24 void put_image(unsigned char* img_buf, int img_size) {
25 printf("Image received: size = %d\n",img_size);
31 last_image = malloc(img_size);
32 memcpy(last_image,img_buf,img_size);
33 last_image_size = img_size;
36 unsigned char* get_image(int* size) {
38 *size = last_image_size;
40 unsigned char* img = malloc(last_image_size);
41 memcpy(img,last_image,last_image_size);
46 printf("face found!\n");
49 int main(int args, char** argv)
51 ServiceCallbacks cb = {0};
52 cb.log = (void (*)(const char*)) &puts;
53 cb.put_image = &put_image;
54 cb.get_image = &get_image;
55 cb.face_found = &face_found;
61 static int listener = 0;
63 fd_set all; // all active sockets
65 void* service(ServiceCallbacks* cb)
67 LOG1("Activating service with SID '%d'\n", DEFAULT_SID);
69 /* Activating a service means registering it with the Resource Manager,
70 * so that it is visible to applications. The return value is the initial
71 * listener socket (h_in3).
73 listener = n_activate(DEFAULT_SID, NULL, NULL);
81 fd_set read; // sockets ready for reading
84 FD_SET(listener, &all);
86 fdmax = listener; //biggest socket number (right now,
87 //'listener' is the only open socket)
89 while (!quit && (!cb->run_check || *(cb->run_check)))
91 read = all; //make a copy of the master set
92 errors = all; //will contain sockets with error conditions set
93 if (Hselect(Hgetinstance(), fdmax+1, &read, NULL, &errors, NULL) == -1)
95 LOG("Error on select\n");
97 /* Select modifies read so that it includes the sockets
98 * that are ready for reading. Let's check which ones they were:
101 for (i = 0; i <= fdmax; i++)
103 if (FD_ISSET(i, &read)) { // found a socket ready to be read
104 if (i == listener) //service socket
106 LOG("Accepting new connection.\n");
107 int socket = n_accept(i, NULL, 0);
109 LOG1("\terror: %d\n", socket);
112 LOG1("\tdone - socket %d connected.\n", socket);
113 FD_SET(socket, &all); //add to master set
116 } //service socket handler
117 else //data on an existing socket
119 int err = read_smsg(&i, HSReceiveBlocking, cb);
120 if (err < 0) //error reading service message
136 } //incoming data handler
137 } // Handle sockets in the read set
139 if (FD_ISSET(i, &errors)) {
140 if (i == listener) //error on service socket
142 LOG("Error in service socket; quitting.\n");
149 } // Read socket loop
155 static int disconnect(int socket, ServiceCallbacks* cb)
157 FD_CLR(socket, &all);
158 return n_disconnect(socket);
161 static void shut_down()
163 printf("Shutting down...\n");
164 printf("Deactivating service...\n");
165 HErrorCode err = n_deactivate(listener, NULL);
167 printf("Deactivated.\n");
168 FD_CLR(listener, &all);
171 // Close all open sockets:
172 for (i = 0; i <= fdmax; i++)
173 if (FD_ISSET(i, &all))