Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel' of newskysaw.cs.northwestern.edu:/home/palacios/palacios into...
[palacios.git] / linux_usr / v3_user_keyed_stream_example.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <sys/select.h>
7 #include <malloc.h>
8
9 #define sint64_t int64_t
10
11 #include "v3_user_keyed_stream.h"
12
13 void usage()
14 {
15     fprintf(stderr,"v3_user_keyed_stream_example /dev/v3-vm0 user:mystreamtype:mystream busywait|select\n");
16 }
17
18
19 int do_work(struct palacios_user_keyed_stream_op *req, 
20             struct palacios_user_keyed_stream_op **resp)
21 {
22     uint64_t datasize;
23     
24     //
25     //
26     // Process request here
27     // 
28     // req->len      : total structure length
29     // req->type     : request type (currently open/close key and read/write key
30     // req->xfer     : unused
31     // req->user_key : the opaque key previously provided by you by an open key
32     // req->buf_len  : length of data
33     // req->buf      : buffer (contains key name (open key) or value (write key))
34     //
35
36     // now built a response
37     *resp = malloc(sizeof(struct palacios_user_keyed_stream_op) + datasize);
38     (*resp)->len = sizeof(struct palacios_user_keyed_stream_op) + datasize;
39     (*resp)->buf_len = datasize;
40     (*resp)->type = req->type;
41     (*resp)->user_key = req->user_key;
42
43     //
44     // The response 
45     // 
46     // resp->len      : total structure length 
47     // resp->type     : response type - must match the request
48     // resp->xfer     : contains the size of data read or written (in read key or write key)
49     // resp->user_key : unused
50     // resp->buf_len  : length of data following
51     // resp->buf      : buffer (contains the data (read key))
52     
53
54     return 0;
55 }
56
57 int main(int argc, char *argv[])
58 {
59     int devfd;
60     int mode=0;
61     char *vm, *url;
62
63     if (argc!=4) { 
64         usage();
65         exit(-1);
66     }
67     
68     vm=argv[1];
69     url=argv[2];
70     mode = argv[3][0]=='s';
71
72     // The URL should begin with user:
73     // the remainder can be used to demultiplex internally
74     // for example user:file:foo might refer to a user-side file-based implementation
75     //
76
77     if (strncmp(url,"user:",5)) { 
78         fprintf(stderr, "URL %s is not a user: url\n");
79         exit(-1);
80     }
81
82     fprintf(stderr,"Attempting to attach to vm %s as url %s\n", vm, url);
83     
84     if ((devfd = v3_user_keyed_stream_attach(vm,url))<0) { 
85         perror("failed to attach");
86         exit(-1);
87     }
88     
89     fprintf(stderr,"Attachment succeeded, I will now operate in %s mode\n", mode==0 ? "busywait" : "select");
90     
91     if (mode==0) { 
92         //busywait
93
94         struct palacios_user_keyed_stream_op *req;
95         struct palacios_user_keyed_stream_op *resp;
96         uint64_t datasize;
97
98         while (1) { 
99             while (!(v3_user_keyed_stream_have_request(devfd))) { 
100             }
101             v3_user_keyed_stream_pull_request(devfd, &req);
102
103             do_work(req, &resp);
104             
105             v3_user_keyed_stream_push_response(devfd, resp);
106
107             free(resp);
108             free(req);
109         }
110     } else {
111
112         struct palacios_user_keyed_stream_op *req;
113         struct palacios_user_keyed_stream_op *resp;
114         uint64_t datasize;
115         fd_set   readset;
116         int rc;
117
118         // select-based operation so that you can wait for multiple things
119         
120         while (1) { 
121             FD_ZERO(&readset);
122             FD_SET(devfd,&readset);
123
124             rc = select(devfd+1, &readset, 0, 0, 0);  // pick whatever you want to select on, just include devfd
125
126             if (rc>0) { 
127                 if (FD_ISSET(devfd,&readset)) { 
128                     // a request is read for us!
129                     v3_user_keyed_stream_pull_request(devfd, &req);
130
131                     do_work(req, &resp);
132                     
133                     v3_user_keyed_stream_push_response(devfd, resp);
134                     
135                     free(resp);
136                     free(req);
137                 }
138             }
139         }
140     }
141
142     v3_user_keyed_stream_detatch(devfd);
143
144     return 0;
145                     
146 }