Commit 196066fd authored by 's avatar

Bug #413 fixed: non blocking way of reading packets


git-svn-id: file:///home/svn/mapi/trunk@1110 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 90e3ebe4
......@@ -204,7 +204,7 @@ int main(int argc, char *argv[])
else
{
bufid = mapi_apply_function(fd, "TO_BUFFER");
bufid = mapi_apply_function(fd, "TO_BUFFER", 0);
if(bufid < 0)
{
......
......@@ -96,7 +96,7 @@ If \fIstr\fP is not found in the payload, the packet will be discarded.
Type of results: \fBnone\fP.
.\"--------------------------------------------------------
.IP "\fBTO_BUFFER\fP"
.IP "\fBTO_BUFFER\fP (int \fIflag\fP)"
Stores the packets of a network flow in a buffer for further reading using the
.B mapi_get_next_pkt()
function.
......@@ -104,7 +104,13 @@ When it applied to a network flow, every subsequent call to
.B mapi_get_next_pkt()
returns the next packet of the flow that was stored in the buffer.
.B mapi_read_results()
does not return any results for this function.
does not return any results for this function. The argument \fIflag\fP
determines whether the buffer will be blocking or not. The corresponding
values are \fBWAIT\fP and \fBNOWAIT\fP equivalently. The \fBNOWAIT\fP
value guarantees that subsequent calls to
.B mapi_get_next_pkt()
will return \fBNULL\fP in case there are not any packets stored in the
buffer.
Type of results: \fBstruct mapipkt*\fP (from mapi_get_next_pkt).
......
......@@ -2189,10 +2189,6 @@ mapi_get_next_pkt(int fd,int fid)
return NULL;
}
if (res.res == NULL) {
DEBUG_CMD(printf("result is NULL [%s:%d]\n",__FILE__,__LINE__));
}
return res.res;
}
......
......@@ -50,6 +50,11 @@ enum cooking_direction {
BOTH_SIDE //all data
};
enum toba_flg {
WAIT = 0,
NOWAIT
};
typedef enum mapi_offline_device_status {
DEVICE_ONLINE,
DEVICE_SETUP,
......
......@@ -19,6 +19,7 @@
#include "mapi_errors.h"
#include "mapid.h"
#include "fhelp.h"
#include "mapi.h"
#define NUM_PKTS 50 //Number of packets stored in buffer
......@@ -39,6 +40,15 @@ static int toba_instance(mapidflib_function_instance_t *instance,
MAPI_UNUSED mapidflib_flow_mod_t *flow_mod)
{
instance->def->shm_size= sizeof(to_buffer_t)+NUM_PKTS*(sizeof(struct mapid_pkthdr)-sizeof(int)+instance->hwinfo->cap_length);
enum toba_flg flag;
mapiFunctArg *fargs = instance->args;
flag = getargint(&fargs);
if (flag != WAIT && flag != NOWAIT)
return MFUNCT_INVALID_ARGUMENT_1;
return 0;
};
......@@ -49,10 +59,17 @@ static int toba_init(mapidflib_function_instance_t *instance,
to_buffer_t *mbuf;
int ret;
enum toba_flg flag;
mapiFunctArg *fargs = instance->args;
flag = getargint(&fargs);
if (flag != WAIT && flag != NOWAIT)
return MFUNCT_INVALID_ARGUMENT_1;
mbuf=instance->result.data;
mbuf->buf=(char*)instance->result.data+sizeof(to_buffer_t);
//adding semaphore
if((ret=fhlp_create_semaphore(&mbuf->sem,2))!=0) {
DEBUG_CMD(printf("Error initializing semaphore: %d\n",ret));
......@@ -179,7 +196,20 @@ static int toba_client_read_result(mapidflib_function_instance_t *instance,mapi_
ushort * array;
} argument;
int condid;
int condid, errno2;
mapiFunctArg *fargs = instance->args;
enum toba_flg flag = getargint(&fargs);
if (flag == WAIT) {
sem_sub.sem_num = 0;
sem_sub.sem_op = -1;
sem_sub.sem_flg = 0;
} else if (flag == NOWAIT) {
sem_sub.sem_num = 0;
sem_sub.sem_op = -1;
sem_sub.sem_flg = IPC_NOWAIT;
}
if(tb->last_pkt==1 && tb->written==tb->read) {
res->res=NULL;
......@@ -189,17 +219,23 @@ static int toba_client_read_result(mapidflib_function_instance_t *instance,mapi_
}
argument.val = 1;
//wait for packet(semaphore blocks when no packets ware ready in the buffer)
//wait for packet(semaphore blocks when no packets were ready in the buffer)
condid=semget(tb->sem.key,1,IPC_CREAT|0660);
if((errno=semop(condid,&sem_sub,1))==-1)
{
//error....
if((errno2=semop(condid,&sem_sub,1))==-1) {
if (errno == EAGAIN && flag == NOWAIT) {
res->res = 0;
res->size = 0;
semop(condid, &sem_wait_sub, 1);
return 0;
}
else {
#ifdef DEBUG
printf("Error in semop\n"); //Debug
printf("Error in semop\n"); //Debug
#endif
return MAPI_SEM_ERR;
}
return MAPI_SEM_ERR;
}
}
tb->read++;
......@@ -217,7 +253,7 @@ static mapidflib_function_def_t finfo={
"", //libname
"TO_BUFFER", //name
"Copies packets to a buffer that can be read by the client", //descr
"", //argdescr
"i", //argdescr
MAPI_DEVICE_ALL, //devtype
MAPIRES_SHM,
0, //shm size. Set by instance
......
......@@ -10,6 +10,7 @@ int main(int argc, char *argv[])
struct mapipkt *pkt;
int err_no =0 , flag=0;
char error[512];
int i;
if(argc!=2)
{
......@@ -23,7 +24,7 @@ int main(int argc, char *argv[])
return -1;
}
DOT;
if((fid=mapi_apply_function(fd, "TO_BUFFER"))<0){
if((fid=mapi_apply_function(fd, "TO_BUFFER", 0))<0){
fprintf(stderr, "Could not apply TO_BUFFER to flow %d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
......@@ -57,8 +58,51 @@ DOT;
return -1;
}
DOT;
/* non-blocking buffer test */
if ((fd = mapi_create_flow(argv[1]))<0){
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if((fid=mapi_apply_function(fd, "TO_BUFFER", NOWAIT))<0){
fprintf(stderr, "Could not apply TO_BUFFER to flow %d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd)<0){
fprintf(stderr, "Connecting to flow failed %d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
/*
* Sanity check
*/
for (i=0; i < 50; i++) {
if((pkt=mapi_get_next_pkt(fd, fid)) != NULL){
printf("Got packet\n");
}
else {
printf("No packet\n");
}
}
DOT;
if(mapi_close_flow(fd)<0){
fprintf(stderr,"Close flow failed\n");
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
printf("\nmapi_get_next_packet OK\n");
......@@ -110,7 +154,7 @@ DOT;
return -1;
}
DOT;
if((fid=mapi_apply_function(fd, "TO_BUFFER"))<0){
if((fid=mapi_apply_function(fd, "TO_BUFFER", WAIT))<0){
fprintf(stderr, "Could not apply TO_BUFFER to flow %d\n", fd);
mapi_read_error( &err_no, error);
fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment