Re: Is http server and http client possbile?
"markreds81" <[email protected]>
| Newsgroups | gmane.comp.hardware.rabbit-semiconductor |
|---|---|
| Message-ID | <[email protected]> |
Whay don't you set a semaphore that lock the client costate until the server task it's finished.
This semaphore can be a simple int/boolean global variable. In the cgi callback function you can make this:
int http_myfuncCGI(HttpState *state) {
[..]
switch (state->substate) {
case 0:
semaphore = 0;
state->substate++;
break;
case 1:
// send data to client by fill the state->socket buffer
// blah blah blah
if (I've finished to send data) {
state->substate++;
}
break;
case 2:
semaphore = 1;
return 1;
}
return 0;
}
In you http client costate:
costate {
waitfor(semaphore);
[...}
}
--- In [email protected], "racqueldesign" <embeddedsupplies@...> wrote:
>
> Thanks Marco. I will try your suggestion. However, I was hoping the cgi would send whatever is in its buffer to the client before it goes to another task which is POSTing to a server.
>
> So I am guess its not possible to indicate to the cgi(httpstate *) to send the content of its buffer NOW instead of when its free.
>
>
>
> --- In [email protected], "markreds81" <marco@> wrote:
> >
> >
> >
> > Yes, it is!
> > Server and client don't use the same socket but be sure the MAX_TCP_SOCKET_BUFFERS define is enought.
> >
> > In addition keep in mind that httpc_skip_headers() and httpc_read_body() functions can take a while to complete their task due to network delays (especially if you use a slow connection like GPRS but I don't know if it is your context). So during that tasks you should yield to other costates. So try to write your own (co)function based on these ones. You can take a look to the http_client.lib source code. Here an example:
> >
> > [...]
> > // fill buf with data to post
> > // blah, blah, blah
> > // s is a pointer to httpc_Socket structure passed as parameter to my confunction
> > rc = ((sock_write(s->sock, buf, i) != -1) && (sock_write(s->sock, (byte *)&ev, sizeof(ev)) != -1)) ? 0 : -EIO;
> > if (!rc) {
> > // waiting for a response from the server
> > tm = SEC_TIMER + 5L; // wait max 5 seconds
> > while ((s->state == HTTPC_STATE_HEADER) && (rc >= 0) && (tm > SEC_TIMER)) {
> > // I'm not really interested in headers, I'm just waiting for the body part of the response
> > rc = httpc_read_header(s, NULL, 0, 0);
> > yield;
> > }
> > i = 0;
> > while ((s->state == HTTPC_STATE_BODY) && (rc >= 0) && (i < sizeof(buf)) && (tm > SEC_TIMER)) {
> > rc = httpc_read_body(s, buf + i, sizeof(buf) - i);
> > if (rc > 0) {
> > i += rc;
> > // received a chunk of data from the server
> > }
> > yield;
> > }
> > }
> > [...]
> >
> >
> > In my application (a datalogger connected to internet via GPRS) I wrote my cofunction and it works. I hope this is clear enought and that it can helps you.
> >
> > Marco
> >
> >
> > --- In [email protected], "racqueldesign" <embeddedsupplies@> wrote:
> > >
> > > Hi,
> > >
> > > I have a HTTP server running and the cgi is working fine.
> > > I added http client and now the cgi is taking about 10 seconds to respond.
> > >
> > > I think the cgi keeps the response to any HTTP REQUEST until the http client task is complete. My question is, how do I get the cgi to send the response(infomation in its buffer) immediately? I added a yield statement hoping to give control back to the http_handler but that didn't work. I am not sure why my cgi won't send response right-away. Do you guys have any advice?
> > >
> > >
> > > while (1) {
> > >
> > > costate {
> > > http_handler();
> > > }
> > >
> > > costate {
> > > yield; //GIVE CONTROL BACK TO http_handler(), cgi
> > >
> > > retval = httpc_init (&hsock, sock);
> > > if (retval)
> > > {
> > > printf ("error %d calling httpc_init()\n", retval);
> > > }
> > >
> > > retval = httpc_post( &hsock, DEST, 80, FILENAME, NULL, PostUpdate_Msg);
> > > if(retval){
> > > printf ("error %d calling httpc_post()\n", retval);
> > > }
> > >
> > >
> > > //Skip Header reading and only read body
> > > httpc_skip_headers(&hsock); //skip header reading
> > > do
> > > {
> > > retval = httpc_read_body( &hsock, PostResponse_msg, 128);
> > > } while(hsock.state == HTTPC_STATE_BODY);
> > >
> > > httpc_close(&hsock);
> > > }
> > > }
> > >
> >
>