Initial commit
[jamendo] / branches / nota-show-app / src / application.c
diff --git a/branches/nota-show-app/src/application.c b/branches/nota-show-app/src/application.c
new file mode 100644 (file)
index 0000000..2e3a545
--- /dev/null
@@ -0,0 +1,124 @@
+/* Application Node */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "notaio.h"
+#include "pdu.h"       /* defines the test string (TESTSTRING)
+                                        * and Service ID (DEFAULT_SID) to use.
+                                        */
+
+char* get_image(int* len) {
+       const char* IMG_PATH= "img/%02d.jpg";
+       const int IMG_CNT = 3;
+       static int img_idx = 0;
+
+       char path[256];
+       FILE* img_file; 
+       int img_size;
+       char* img_buf = 0;
+       int rl;
+
+       sprintf(path,IMG_PATH,img_idx);
+       printf("Sending image %s\n",path);
+       
+       img_file = fopen(path,"r");
+       if (!img_file) {
+               printf("get_image: ERROR opening image\n");
+               return NULL;
+       }
+
+       fseek(img_file,0,SEEK_END);
+       img_size = ftell(img_file);
+       fseek(img_file,0,SEEK_SET);
+       printf("Image size %d\n",img_size);
+
+       img_buf = malloc(img_size);
+       rl=fread(img_buf,1,img_size,img_file);
+
+       if (rl!=img_size) {
+               printf("get_image: ERROR reading image\n");
+               free(img_buf);
+               img_buf = 0;
+       }
+       img_idx = (img_idx+1) % IMG_CNT;
+
+       if(len) *len = img_size;
+       
+       return img_buf;
+}
+
+int main(int argc, char* argv[])
+{
+       /* Service socket to use for connecting to the service node. */
+       HSSockID sockid;
+       HErrorCode err;
+
+/* The first parameter specifies the service to connect to (SID).
+ * The other parameters are reserved for legacy purposes.
+ */
+       sockid = n_connect(DEFAULT_SID, NULL, NULL);
+
+/* sockid will be the number of the socket we got (>0), if successful,
+ * or <0 if something went wrong. Let's check:
+ */
+       if (sockid < 0) {
+               return -1;
+       }
+
+       printf("Connected socket %d to service '%d'.\n", sockid, DEFAULT_SID);
+
+       while(1) {
+               int img_len = 0;
+               char* img;
+               int len = 0;
+               ServiceMessage* smsg;
+
+               img = get_image(&img_len);
+               if(img) {
+                       smsg = pack_pdu(PUT_IMAGE, (uns8*)img, img_len, &len);
+                       free(img);
+                               
+                       /* Send the through the socket specified by sockid. The last parameter, 
+                        * HSSendBlocking, means we will
+                        * use blocking mode, i.e. the call to Hsend will only return once all
+                        * data has been sent (or an error is detected).
+                        */
+                       err = n_send(sockid, smsg, len, HSSendBlocking);
+                       free(smsg);
+                       if(err < 0){
+                               return -1;
+                       }
+
+                       /* If no error condition was raised, Hsend will return the
+                        * number of bytes sent. Let's check this against what we
+                        * intended to send, just to be sure (with a blocking call, the
+                        * return value should always be equal to the "data_len" parameter
+                        * of Hsend).
+                        */
+                       if (err == len)
+                               printf("Send successful.\n");
+                       else
+                               printf("Sent %d out of %d bytes.\n", err, len);
+
+                       printf("Would you like to disconnect (\"y\" for yes)?\n");
+                       char c = getchar();
+                       if (c == 'y') {
+                               // We're done; tell the server to close the connection.
+                               len = 0;
+                               smsg = pack_pdu (DISCONNECT,NULL, 0, &len);
+                               n_send(sockid, smsg, len, HSSendBlocking);
+                               free(smsg);
+       
+                               if(err < 0) {
+                                  return -1;
+                               }
+                               break;
+                       }
+               }
+       }
+       
+       n_disconnect(sockid);
+       
+       return 0;
+}
+