Initial commit
[jamendo] / branches / nota-show-app / src / application.c
1 /* Application Node */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "notaio.h"
6 #include "pdu.h"        /* defines the test string (TESTSTRING)
7                                          * and Service ID (DEFAULT_SID) to use.
8                                          */
9
10 char* get_image(int* len) {
11         const char* IMG_PATH= "img/%02d.jpg";
12         const int IMG_CNT = 3;
13         static int img_idx = 0;
14
15         char path[256];
16         FILE* img_file; 
17         int img_size;
18         char* img_buf = 0;
19         int rl;
20
21         sprintf(path,IMG_PATH,img_idx);
22         printf("Sending image %s\n",path);
23         
24         img_file = fopen(path,"r");
25         if (!img_file) {
26                 printf("get_image: ERROR opening image\n");
27                 return NULL;
28         }
29
30         fseek(img_file,0,SEEK_END);
31         img_size = ftell(img_file);
32         fseek(img_file,0,SEEK_SET);
33         printf("Image size %d\n",img_size);
34
35         img_buf = malloc(img_size);
36         rl=fread(img_buf,1,img_size,img_file);
37
38         if (rl!=img_size) {
39                 printf("get_image: ERROR reading image\n");
40                 free(img_buf);
41                 img_buf = 0;
42         }
43         img_idx = (img_idx+1) % IMG_CNT;
44
45         if(len) *len = img_size;
46         
47         return img_buf;
48 }
49
50 int main(int argc, char* argv[])
51 {
52         /* Service socket to use for connecting to the service node. */
53         HSSockID sockid;
54         HErrorCode err;
55
56 /* The first parameter specifies the service to connect to (SID).
57  * The other parameters are reserved for legacy purposes.
58  */
59         sockid = n_connect(DEFAULT_SID, NULL, NULL);
60
61 /* sockid will be the number of the socket we got (>0), if successful,
62  * or <0 if something went wrong. Let's check:
63  */
64         if (sockid < 0) {
65                 return -1;
66         }
67
68         printf("Connected socket %d to service '%d'.\n", sockid, DEFAULT_SID);
69
70         while(1) {
71                 int img_len = 0;
72                 char* img;
73                 int len = 0;
74                 ServiceMessage* smsg;
75
76                 img = get_image(&img_len);
77                 if(img) {
78                         smsg = pack_pdu(PUT_IMAGE, (uns8*)img, img_len, &len);
79                         free(img);
80                                 
81                         /* Send the through the socket specified by sockid. The last parameter, 
82                          * HSSendBlocking, means we will
83                          * use blocking mode, i.e. the call to Hsend will only return once all
84                          * data has been sent (or an error is detected).
85                          */
86                         err = n_send(sockid, smsg, len, HSSendBlocking);
87                         free(smsg);
88                         if(err < 0){
89                                 return -1;
90                         }
91
92                         /* If no error condition was raised, Hsend will return the
93                          * number of bytes sent. Let's check this against what we
94                          * intended to send, just to be sure (with a blocking call, the
95                          * return value should always be equal to the "data_len" parameter
96                          * of Hsend).
97                          */
98                         if (err == len)
99                                 printf("Send successful.\n");
100                         else
101                                 printf("Sent %d out of %d bytes.\n", err, len);
102
103                         printf("Would you like to disconnect (\"y\" for yes)?\n");
104                         char c = getchar();
105                         if (c == 'y') {
106                                 // We're done; tell the server to close the connection.
107                                 len = 0;
108                                 smsg = pack_pdu (DISCONNECT,NULL, 0, &len);
109                                 n_send(sockid, smsg, len, HSSendBlocking);
110                                 free(smsg);
111         
112                                 if(err < 0) {
113                                    return -1;
114                                 }
115                                 break;
116                         }
117                 }
118         }
119         
120         n_disconnect(sockid);
121         
122         return 0;
123 }
124