Update:
authorBogdan Drutu <bgodandrutu@gmail.com>
Fri, 1 Apr 2011 20:42:07 +0000 (23:42 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 2 Apr 2011 07:27:23 +0000 (10:27 +0300)
- complete api
- add a client/server example

makefile
run.sh
src/client.c [new file with mode: 0644]
src/lib_swift.c
src/lib_swift.h
src/main.c [deleted file]
src/server.c [new file with mode: 0644]

index f1f33d7..45f76ef 100644 (file)
--- a/makefile
+++ b/makefile
@@ -4,19 +4,23 @@ BIN=bin
 SRC=src
 LIB=lib
 
-ALL: directory lib main
+ALL: directory $(LIB)/libswift.so $(BIN)/server $(BIN)/client
 
 directory:
        mkdir -p $(BIN)
        mkdir -p $(LIB)
 
-lib: object
+$(LIB)/libswift.so: $(BIN)/lib_swift.o
        gcc -shared $(BIN)/lib_swift.o -o $(LIB)/libswift.so
 
-main:
-       gcc -Wall $(SRC)/main.c -o $(BIN)/main -lswift -L$(LIB)
+$(BIN)/client: $(SRC)/client.c
+       gcc -Wall $(SRC)/client.c -o $(BIN)/client -lswift -L$(LIB)
 
-object: $(SRC)/lib_swift.c
+$(BIN)/server: $(SRC)/server.c
+       gcc -Wall $(SRC)/server.c -o $(BIN)/server -lswift -L$(LIB)
+
+
+$(BIN)/lib_swift.o: $(SRC)/lib_swift.c
        $(CC) $(CFLAGS) -o $(BIN)/lib_swift.o -c $(SRC)/lib_swift.c
        
 clean:
diff --git a/run.sh b/run.sh
index e6e7140..ce6a48c 100755 (executable)
--- a/run.sh
+++ b/run.sh
@@ -2,4 +2,7 @@
 
 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:lib
 
-./bin/main $*
+./bin/server &
+./bin/client &
+
+
diff --git a/src/client.c b/src/client.c
new file mode 100644 (file)
index 0000000..4fdfecc
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "lib_swift.h"
+
+int main()
+{
+       Swift s = socketSwift(1);
+       struct sockSwiftaddr my_addr;
+       char buf[100];
+       
+       // populate sockSwiftaddr
+       my_addr.sin_family = AF_INET;
+       my_addr.sin_port = htons(SWIFT_PORT);
+       my_addr.sin_addr.N = 1; 
+       my_addr.sin_addr.s_addr[0] = htonl(INADDR_LOOPBACK);    
+       
+       recvfromSwift(s, buf, 100, 0, &my_addr, sizeof(my_addr));
+       
+       struct listsockaddr lsa =  transformFromSwiftToAddr(my_addr);
+       printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(lsa.sa[0].sin_addr), ntohs(my_addr.sin_port), buf);
+       
+       closeSwift(s);
+       return 0;       
+}
index d244b20..d26a208 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 
+#define DEBUG
 #include "lib_swift.h"
 
 struct sockSwiftaddr transformFromAddrToSwift(struct listsockaddr lsa)
@@ -39,17 +40,68 @@ struct listsockaddr transformFromSwiftToAddr(struct sockSwiftaddr ssa)
        return lsa;
 }
 
+// Function to receive a message
+ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
+                                         struct sockSwiftaddr *from, socklen_t fromlen)
+{
+       struct sockaddr s_other;
+       socklen_t slen=sizeof(s_other); 
+       ssize_t rec = -1, send;
+       char *command = "test";
+       struct listsockaddr lsa =  transformFromSwiftToAddr(*from);
+       int i, channel;
+       
+       Dprintf("create recv channel\n");       
+       // TODO make pool
+       if (s->usedChannels < s->maxChannels) 
+       {
+               channel = s->usedChannels++;
+               CHECK(s->recvChannel[channel] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
+
+               for ( i = 0 ; i < lsa.N ; i++) 
+               {
+                       Dprintf("send information to %s\n", inet_ntoa(lsa.sa[0].sin_addr));                     
+                       send = sendto(s->recvChannel[channel], command, strlen(command), 0, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
+               }
+               
+               Dprintf("receive data\n");
+               // I'm waiting for response
+               rec = recvfrom(s->recvChannel[channel], buf, len, flags,  (struct sockaddr * __restrict__)&s_other, &slen);             
+       
+               close(s->recvChannel[channel]);
+               s->usedChannels--;
+       }
+       return rec;
+}
+
+// Function to send a message
+ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags, 
+                                       const struct sockSwiftaddr *to, socklen_t tolen) 
+{
+       struct listsockaddr lsa =  transformFromSwiftToAddr(*to);
+       int i;
+       ssize_t send = -1;
+       
+       Dprintf("send data\n");
+       for ( i = 0 ; i < lsa.N ; i++) 
+       {
+               send = sendto(s->sendChannel, buf, len, flags, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
+       }
+       
+       return send;
+}
+// Function to listen to a port
 int listenfromSwift (Swift s, void *buf, size_t len, int flags,
-                 struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen)
+                                        struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen) 
 {
        struct sockaddr s_other;
        struct listsockaddr lsa;
-       socklen_t slen=sizeof(s_other);
-
-       Dprintf("wait to receive messages");
+       socklen_t slen=sizeof(s_other); 
+       ssize_t rec;
        
-       int rec = recvfrom(s->socketListener, buf, len, flags,
-               (struct sockaddr * __restrict__)&s_other, &slen);
+       Dprintf("wait to receive messages\n");
+       
+       rec = recvfrom(s->socketListener, buf, len, flags,  (struct sockaddr * __restrict__)&s_other, &slen);
        
        // fill listsockaddr
        memcpy(&lsa.sa[0], &s_other, sizeof(s_other));
@@ -60,38 +112,37 @@ int listenfromSwift (Swift s, void *buf, size_t len, int flags,
        return rec;
 }
 
+// Function to bind a port for swift socket
 int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen)
 {
-       Dprintf("bind swift socket");
-       return bind(s->socketListener, 
-               (const struct sockaddr *)&s->socketListenerAddr,
-               sizeof(s->socketListenerAddr));
+       Dprintf("bind swift socket\n");
+       struct listsockaddr lsa = transformFromSwiftToAddr(*my_addr);
+               
+       return bind(s->socketListener, (const struct sockaddr *)&lsa.sa[0], sizeof(lsa.sa[0]));
 }
 
-Swift socketSwift()
+// Function to create a Swift socket
+Swift socketSwift(int maxChannels)
 {
-       Dprintf("create swift socket"); 
+       Dprintf("create struct swift\n");       
        Swift s = calloc(1,sizeof(*s));
 
-       Dprintf("create swift socket listener");        
+       Dprintf("create swift socket listener\n");      
        CHECK(s->socketListener = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
 
-       Dprintf("create swift socket data");    
-       CHECK(s->socketData = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
+       Dprintf("create swift send channel\n"); 
+       CHECK(s->sendChannel = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
 
-       memset((char *) &s->socketListenerAddr, 0, sizeof(s->socketListenerAddr));
-       
-       s->socketListenerAddr.sin_family = AF_INET;
-       s->socketListenerAddr.sin_port = htons(SWIFT_PORT);
-       s->socketListenerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
-       
        return s;
 }
 
+
 void closeSwift(Swift s)
 {
-       Dprintf("close swift socket");
+       Dprintf("close swift socket\n");
        close(s->socketListener);
+       close(s->sendChannel);
+       
        
        free(s);
 }
index c735752..3062195 100644 (file)
        } while (0) \
 
 #ifdef DEBUG
-#define Dprintf(msg,...) printf("[%s]:%d" msg, __FILE__, __LINE__, ##__VA_ARGS__)
+#define Dprintf(msg,...) printf("[%s]:%d " msg, __FILE__, __LINE__, ##__VA_ARGS__)
 #else
 #define Dprintf(msg,...)                /* do nothing */
 #endif
 
 // swift interface
 typedef struct swift {
-       int socketListener, socketData;
-       struct sockaddr_in socketListenerAddr;
+       int socketListener;
+       int sendChannel;
+       int *recvChannel;
+       
+       int usedChannels;
+       int maxChannels;
 } *Swift;
 
 // swift_addr structure similar with in_addr 
@@ -53,7 +57,7 @@ struct listsockaddr {
 };     
 
 // Function to create a Swift socket
-Swift socketSwift();
+Swift socketSwift(int maxChannels);
 
 // Function to close a Swift socket
 void closeSwift(Swift);
@@ -66,11 +70,16 @@ int listenfromSwift (Swift s, void *buf, size_t len, int flags,
 int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen);
 
 // Function to receive a message
-ssize_t recvfrom(Swift s, void *buf, size_t len, int flags,
-                 struct sockSwiftaddr *from, socklen_t *fromlen);
+ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
+                 struct sockSwiftaddr *from, socklen_t fromlen);
                  
 // Function to send a message
-ssize_t sendto(Swift s, const void *buf, size_t len, int flags, 
-                               const struct sockSwiftaddr *to, socklen_t tolen);
+ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags, 
+                                       const struct sockSwiftaddr *to, socklen_t tolen);
+
+
+// test function -- don't commit
+struct sockSwiftaddr transformFromAddrToSwift(struct listsockaddr lsa);
+struct listsockaddr transformFromSwiftToAddr(struct sockSwiftaddr ssa);
 
 #endif
diff --git a/src/main.c b/src/main.c
deleted file mode 100644 (file)
index 7276047..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-#include "lib_swift.h"
-
-int main()
-{
-       Swift s = socketSwift();
-       struct sockaddr_in si_other;
-       socklen_t slen = sizeof(si_other);
-       
-       char buf[100];
-       
-       recvfromSwift(s,buf,100,0,(struct sockaddr *)&si_other,&slen);
-       
-       printf("Received packet from %s:%d\nData: %s\n\n", 
-               inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
-       
-       return 0;
-}
diff --git a/src/server.c b/src/server.c
new file mode 100644 (file)
index 0000000..2001f8a
--- /dev/null
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "lib_swift.h"
+
+int main()
+{
+       Swift s = socketSwift(1);
+       struct sockSwiftaddr my_addr, from;
+       char buf[100];
+       socklen_t slen;
+       struct listsockaddr lsa;
+       
+       // populate sockSwiftaddr
+       my_addr.sin_family = AF_INET;
+       my_addr.sin_port = htons(SWIFT_PORT);
+       my_addr.sin_addr.N = 1; 
+       my_addr.sin_addr.s_addr[0] = htonl(INADDR_ANY); 
+       
+       //recvfromSwift(s, buf, 100, 0, (struct sockaddr *)&si_other, &slen);
+       
+       bindSwift(s, &my_addr, sizeof(my_addr));
+       listenfromSwift(s, buf, 100, 0, &from, &slen);
+       
+       lsa =  transformFromSwiftToAddr(from);
+       printf("Received packet from %s:%d with data: %s\n", inet_ntoa(lsa.sa[0].sin_addr), ntohs(lsa.sa[0].sin_port), buf);
+       
+       
+       closeSwift(s);
+       return 0;
+}