]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
fwp_client modification - wake up when a new message comes
authorMartin Molnar <molnar@sum.(none)>
Sun, 13 Jan 2008 17:14:22 +0000 (18:14 +0100)
committerMartin Molnar <molnar@sum.(none)>
Sun, 13 Jan 2008 17:14:22 +0000 (18:14 +0100)
fwp/fwp_manager/fwpmgr.c
fwp/libfwp/include/fwp_ctable.h
fwp/libfwp/include/fwp_msg.h
fwp/libfwp/src/fwp_ctable.c
fwp/libfwp/src/fwp_proto.c

index e34bbe10c538390ae3089206341040c2b30b935f..80280873314a8486c36cf909158c2c70b98a1e13 100644 (file)
@@ -13,6 +13,7 @@
 #include <unistd.h>
 #include <pthread.h>
 #include <stdio.h>
+#include <semaphore.h>
 
 #define UN_MNGT_SOCKET "/var/run/fwpsock" 
 
@@ -43,6 +44,8 @@ struct fwp_ctable ctable_accepted;
 unsigned char buffer[FWP_MTU];
 unsigned int vres_id;
 
+sem_t sem_msg_in;
+
 /***/
 
 void fwp_adm_test(struct fwp_ctable *ct)
@@ -154,6 +157,7 @@ void* fwp_rx_thread(void* arg)
 
                if ((code == NEGOTIATE_REQUEST)||(code == NEGOTIATE_RESPONSE))
                        fwp_msgq_enqueue(&msgq_neg_in, msgb);
+                       sem_post(sem_msq_in);
                else { 
                        printf("Error: not valid negotiate frame\n");
                        fwp_msgb_free(msgb);
@@ -206,57 +210,74 @@ void* fwp_server_thread(void* arg)
        }
 }
 
-
-void* fwp_client_thread(void* ap_address)
+void fwp_client negotiate_request(unsigned int hid, struct msgb *msgb)
 {
        struct fwp_ctable_entry ctentry;
        struct fwp_contract cnt;
+       
+       fwp_msg_contract_pull(msgb, &cnt); 
+
+       memcpy(&ctentry.contract, &cnt, sizeof(cnt));
+       memcpy(&ctentry.saddr, &msgb->addr, sizeof(msgb->addrlen));
+       ctentry.addrlen = msgb->addrlen;
+       ctentry.hid = hid;
+       hid = fwp_ctable_put(&ctable_negotiate, &ctentry);
+
+       msgb->data =  msgb->head;
+       fwp_msg_header_put(msgb, FWP_NEGT_REQ, hid); 
+       msgb->addrlen = sizeof(fwp_server_addr);
+       memcpy(msgb->addr, (struct sockaddr*)&fwp_server_addr, msgb->addrlen);
+
+       fwp_vres_send(vres_id, msgb);
+}
+
+int fwp_client negotiate_response(unsigned int hid, struct msgb *msgb)
+{
+       struct fwp_ctable_entry ctentry;
+       struct fwp_contract cnt_res;
+       struct fwp_contract *cnt_req;
+
+       fwp_msg_contract_pull(msgb, &cnt_res);
+       cnt_req = fwp_ctable_get(&ctable_negotiate, hid);
+
+       if (!cnt_req) /*TODO: test na zhodu res.cnt = req.cnt */
+               return -1; /* not valid response*/
+
+       if (cnt.res.status == FWP_ACCEPTED) 
+               fwp_ctable_add(&ctable_appected, cnt);
+
+       fwp_ctable_remove(&ctable_negotiate, hid);
+
+       /* forward response to app */
+       memcpy(&msgb->addr, &ctentry.saddr, sizeof(msgb->addrlen));
+       msgb->addrlen = ctentry.addrlen;
+
+       fwp_vres_send(vres_id, msgb);
+       return 0;
+}
+
+void* fwp_client_thread(void* arg)
+{
        struct fwp_msgb *msgb;
        unsigned int code, hid;
 
        while (1) {
-               while ((msgb = fwp_msgq_dequeue(&msgq_neg_in))) {
-                       fwp_msg_header_pull(msgb, &code, &hid);
+               sem_wait(sem_msg_in);
+               msgb = fwp_msgq_dequeue(&msgq_neg_in);
+               if (!msgb) 
+                       continue;
                        
-                       switch  (code) {
-                       case  NEGOTIATE_REQUEST: 
-                                                       
-                               fwp_msg_contract_pull(msgb, &cnt); 
-                               memcpy(&ctentry.contract, &cnt, sizeof(cnt));
-                               memcpy(&ctentry.saddr, &msgb->addr, 
-                                      sizeof(msgb->addrlen));
-                               ctentry.addrlen = msgb->addrlen;
-                                       
-                               fwp_ctable_put(&ctable_negotiate, &ctentry);
-                               /*fwp_msgb_push(sizeof(struct fwp_msg_header));*/
-
-                               hid = ctentry.contract.id;
-
-                               fwp_msg_header_put(msgb, code, hid);
-                               /* 
-                               memcpy(msgb->addr, 
-                                      (struct sockaddr*)&fwp_server_addr, 
-                                      peer_addrlen);
-                               msgb->addrlen = sizeof(fwp_server_addr);
-                               memcpy(msgb->head, buffer, len);
-                               msgb->len = len;
-                               
-                               */
-
-                               fwp_vres_send(vres_id,msgb);
+               fwp_msg_header_pull(msgb, &code, &hid);
+                       
+               switch  (code) {
+                       case  FWP_NEGT_REQ: 
+                               fwp_client_negotiate_request(hid, msgb);                        
                                break;
 
-                       case NEGOTIATE_RESPONSE: 
-                               
-                       /*      fwp_ctable_del(negt_ctable,id, cnt); 
-                               fwp_ctable_add(accept_ctable, cnt);
-
-                               contract_to_msg(cnt, msg);
-                               fwp_vres_send(client,msg,..);*/
+                       case FWP_NEGT_RES: 
+                               fwp_client negotiate_response(hid, msgb);
                                break;
-                       
-                       }
-                }
+               }
        }
 }
 
@@ -266,6 +287,7 @@ int main(int argc, char* argv[])
        pthread_t rx_thread_id, server_thread_id, client_thread_id;
        pthread_attr_t attr;
        
+       sem_init(&sem_msg_in, 0, 0);
        mode = FWP_SERVER;
        pthread_attr_init(&attr);
        rc = pthread_create(&rx_thread_id, &attr, fwp_rx_thread, (void*) NULL);
index 1ec7e6c1aa61f897714de31ab8c12914e5268381..1c810723e2e04d6b8c22b2ad25724db6e59c85de 100644 (file)
@@ -11,7 +11,8 @@ enum ctable_entry_status_t {
        FWP_CTENTRY_FULL = 1
 };
 
-struct fwp_ctable_entry{
+struct fwp_ctable_entry {
+       unsigned int hid;
        struct fwp_contract contract;
        struct sockaddr saddr;
        socklen_t addrlen;
index 6bc1eb4b0e82a206feb61953db8c374c0c6f5006..63d8d390f876f378e958a9022309143d96d473f5 100644 (file)
@@ -4,8 +4,8 @@
 #include <stdint.h>
 #include "fwp_msgb.h"
 
-#define NEGOTIATE_REQUEST  0x02
-#define NEGOTIATE_RESPONSE 0x03
+#define FWP_NEGT_REQ  0x02  /* negotiation request */
+#define FWP_NEGT_RES  0x03  /* negotiation response */
 
 struct fwp_msg_header{
        uint8_t code;
index b31927481b8e27515df7177e6fbf5b589dbf02f9..79316d256763c3bd1535c3781c508e869cbc49bd 100644 (file)
@@ -31,9 +31,9 @@ int fwp_ctable_put(struct fwp_ctable* ctable, struct fwp_ctable_entry* entry)
        return id;
 }
 
-int fwp_ctable_get(struct fwp_ctable* ctable, struct fwp_ctable_entry* entry)
+struct fwp_ctable_entry* fwp_ctable_get(struct fwp_ctable* ctable, unsigned int id)
 {
-       return 0;
+       return (&ctable->entry[id]);
 }
 
 inline void fwp_ctable_remove(struct fwp_ctable* ctable, unsigned int id)
index 8e9b433c5179885207609bc705898358994445bb..81d86c4a741f521470e4ee3a3d90db6701c29e2e 100644 (file)
@@ -79,8 +79,10 @@ int fwp_send(int vres_id, void *buffer, size_t size, int flags,
                msgb = fwp_msgb_alloc(buffer_size);
        else {*/
                msgb = fwp_msgb_alloc(0);
-               msgb->head = msgb->data = msgb->tail = buffer;
+               msgb->head = msgb->data = buffer;
+               msgb->tail = msgb->head + size;
                msgb->buf_size = msgb->len = size;
+
        /*}*/
 
        memcpy((void*)&msgb->addr, (void*)to, *tolen);