on master help sign in
Merge pull request #1632 from edwinb/remove-support

Remove dead code in support

commitc81ad3b633b0d8189b31092ba2c610476244dcc7 authorEdwin Brady <edwin.brady@gmail.com> dateTue 14 Oct 2014, 21:20 (12 years ago) parent74ad9d537d7ef4
7 files changed−451
support/Makefile−15 support/network.c−260 support/network.h−36 support/testidr.c−6 support/testidr.h−7 support/threads.c−116 support/threads.h−11
support/Makefiledeleted−15
@@ -1,15 +0,0 @@
1−OBJS = network.o testidr.o
2−HDRS = network.h testidr.h
3−
4−CFLAGS = `epic -includedirs`
5−
6−LIBTARGET = libidris.a
7−
8−check: $(OBJS)
9− ar -r $(LIBTARGET) $(OBJS)
10−
11−install: check
12− install $(LIBTARGET) $(TARGET)
13− install $(HDRS) $(TARGET)
14−
15−.PHONY: check install
support/network.cdeleted−260
@@ -1,260 +0,0 @@
1−#include <stdio.h>
2−#include <sys/types.h>
3−#include <sys/socket.h>
4−#include <netinet/in.h>
5−#include <arpa/inet.h>
6−#include <unistd.h>
7−#include <string.h>
8−#include <fcntl.h>
9−#include <errno.h>
10−#include <closure.h>
11−
12−#include "network.h"
13−
14−int isReadable(int sd, int timeOut) { // milliseconds
15− fd_set socketReadSet;
16− FD_ZERO(&socketReadSet);
17− FD_SET(sd,&socketReadSet);
18− struct timeval tv;
19− if (timeOut) {
20− tv.tv_sec = timeOut / 1000;
21− tv.tv_usec = (timeOut % 1000) * 1000;
22− } else {
23− tv.tv_sec = 0;
24− tv.tv_usec = 0;
25− } // if
26− if (select(sd+1,&socketReadSet,0,0,&tv) == -1) {
27− printf("READ ERROR!\n");
28− return 0;
29− } // if
30− return FD_ISSET(sd,&socketReadSet) != 0;
31−} /* isReadable */
32−
33−void* net_UDP_clientSocket(char* server, int port) {
34− srand(12345); //time(NULL));
35−
36− Connection* con = (Connection*)EMALLOC(sizeof(Connection));
37− int s;
38− struct sockaddr_in si_me, si_other;
39−
40− if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
41− printf("FAIL 1\n");
42− return NULL;
43− }
44−
45− memset((char *) &si_other, 0, sizeof(si_other));
46− si_other.sin_family = AF_INET;
47− si_other.sin_port = htons(port);
48−
49− if (inet_aton(server, &si_other.sin_addr)==0) {
50− printf("FAIL 2\n");
51− return NULL;
52− }
53−
54− // Bind, so we can receive replies.
55− si_me.sin_family = AF_INET;
56− si_me.sin_port = htons(0);
57− si_me.sin_addr.s_addr = htonl(INADDR_ANY);
58−
59− if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me))<0) {
60− return NULL;
61− }
62−
63− con->sock = s;
64− con->addr = si_other;
65−
66−// printf("Opened client socket %d\n", s);
67− return con;
68−}
69−
70−void* net_UDP_serverSocket(int port) {
71− srand(time(NULL));
72−
73− Connection* con = (Connection*)EMALLOC(sizeof(Connection));
74− int s;
75− struct sockaddr_in si_me;
76−
77− if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
78− return NULL;
79− }
80−
81− memset((char *) &si_me, 0, sizeof(si_me));
82− si_me.sin_family = AF_INET;
83− si_me.sin_port = htons(port);
84− si_me.sin_addr.s_addr = htonl(INADDR_ANY);
85− if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me))==-1) {
86− return NULL;
87− }
88−
89− con->sock = s;
90− con->addr = si_me;
91−
92−// printf("Opened server socket %d\n", s);
93− return con;
94−}
95−
96−int net_sendUDP(void* conn, char* server, int port, char* stuff) {
97−// printf("Socket is %d\n", conn);
98− if (0) { //rand()%50000 == 0) {
99− printf("DROP\n");
100− return 0;
101− }
102−
103−
104− Connection* c = (Connection*)conn;
105− int s = c->sock;
106− struct sockaddr_in other = c->addr;
107− memset((char *) &other, 0, sizeof(other));
108− other.sin_family = AF_INET;
109− other.sin_port = htons(port);
110− if (inet_aton(server, &other.sin_addr)==0) {
111− printf("FAIL 3\n");
112− return -1;
113− }
114−
115− if (sendto(s, stuff, strlen(stuff), 0, (struct sockaddr*)&other,
116− sizeof(other))==-1) {
117− printf("Send FAIL\n");
118− return -1;
119− }
120−// printf("Send WIN, to %s %d %d\n", inet_ntoa(other.sin_addr),
121−// ntohs(other.sin_port), port);
122− return 0;
123−}
124−
125−void* net_recvUDP(void* conn) {
126− struct sockaddr_in other;
127− Connection* c = (Connection*)conn;
128− int s = c->sock;
129− socklen_t slen = sizeof(other);
130− struct sockaddr_in me = c->addr;
131−
132− // TMP HACK: 512 should be enough for the purposes of this experiment...
133− // Do it properly some time.
134− char* buf = (char*)EMALLOC(512*sizeof(char));
135−
136−// printf("Waiting to receive\n");
137−
138− int error;
139−// while (!isReadable(s,100)) printf(".");
140−
141− if (!isReadable(s, 1000)) {
142− printf("Nothing\n");
143− return NULL;
144− }
145−
146− if (recvfrom(s, buf, 512, 0, (struct sockaddr *)&other, &slen)==-1) {
147− printf("Nothing\n");
148− return NULL;
149− }
150−
151−/* printf("Received %s from %s:UDP%u\n",
152− buf,inet_ntoa(other.sin_addr),
153− ntohs(other.sin_port));
154−*/
155− // return a structure with the received value, and where it came from.
156− Recv* r = (Recv*)EMALLOC(sizeof(Recv));
157− r->buf = buf;
158− r->server = inet_ntoa(other.sin_addr);
159− r->port = ntohs(other.sin_port);
160−
161− return (void*)r;
162−}
163−
164−char* get_recvBuf(void* recv) {
165− return ((Recv*)recv)->buf;
166−}
167−
168−char* get_recvServer(void* recv) {
169− return ((Recv*)recv)->server;
170−}
171−
172−int get_recvPort(void* recv) {
173− return ((Recv*)recv)->port;
174−}
175−
176−int nullPtr(void *ptr) {
177− return ptr==NULL;
178−}
179−
180−// Just to see if the idris FFI is working, and happily talking to .o files...
181−int netTest(char* strTest, int num) {
182− int i;
183− for(i=0;i<num;++i) {
184− printf("%s\n",strTest);
185− }
186−
187− return i;
188−}
189−
190−void net_sendRaw(void* conn, char* server, int port, VAL pkt)
191−{
192− int seq = GETINT(PROJECT(pkt, 0));
193− VAL ty = PROJECT(pkt, 1);
194− int pty;
195−
196− switch(TAG(ty)) {
197− case 0:
198− pty = 0;
199− break;
200− case 1:
201− pty = 1;
202− break;
203− case 2:
204− pty = TAG(PROJECT(ty,0)) + 2;
205− }
206−
207− char* buf = (char*)EMALLOC(512*sizeof(char));
208− sprintf(buf, "%d %d", seq, pty);
209−
210− net_sendUDP(conn, server, port, buf);
211−}
212−
213−VAL net_recvRaw(void* conn)
214−{
215− int seq, pty;
216−
217− void* dat = net_recvUDP(conn);
218− if (dat==NULL) {
219− return CONSTRUCTOR(1,0,NULL); // Nothing
220− }
221− else {
222− char* recvBuf = get_recvBuf(dat);
223−
224− sscanf(recvBuf, "%d %d", &seq, &pty);
225− VAL ptyc = NULL;
226− if (pty<2) {
227− ptyc = CONSTRUCTOR(pty,0,NULL);
228− } else {
229− ptyc = CONSTRUCTOR1(2, CONSTRUCTOR(pty-2,0,NULL));
230− }
231− VAL pkt = CONSTRUCTOR2(0, MKINT(seq), ptyc);
232− printf("Received %d %d\n", seq, pty);
233−
234− VAL recvpkt = CONSTRUCTOR3(0, pkt, MKSTR(get_recvServer(dat)),
235− MKINT(get_recvPort(dat)));
236−
237− return CONSTRUCTOR1(0, recvpkt); // Just pkt
238− }
239−
240−}
241−
242−void dumpPkt(VAL pkt)
243−{
244− int seq = GETINT(PROJECT(pkt, 0));
245− VAL ty = PROJECT(pkt, 1);
246− int pty;
247−
248− switch(TAG(ty)) {
249− case 0:
250− pty = 0;
251− break;
252− case 1:
253− pty = 1;
254− break;
255− case 2:
256− pty = TAG(PROJECT(ty,0)) + 2;
257− }
258−
259− printf("%d %d\n", seq, pty);
260−}
support/network.hdeleted−36
@@ -1,36 +0,0 @@
1−#include <stdio.h>
2−#include <sys/types.h>
3−#include <sys/socket.h>
4−#include <netinet/in.h>
5−#include <arpa/inet.h>
6−#include <unistd.h>
7−#include <string.h>
8−#include <closure.h>
9−
10−typedef struct {
11− int sock;
12− struct sockaddr_in addr;
13−} Connection;
14−
15−typedef struct {
16− unsigned port;
17− char* buf;
18− char* server;
19−} Recv;
20−
21−int netTest(char* strTest, int num);
22−void* net_UDP_serverSocket(int port);
23−void* net_UDP_clientSocket(char* server, int port);
24−
25−int nullPtr(void *ptr);
26−
27−int net_sendUDP(void* conn, char* server, int port, char* stuff);
28−void* net_recvUDP(void* conn);
29−
30−char* get_recvBuf(void* recv);
31−char* get_recvServer(void* recv);
32−int get_recvPort(void* recv);
33−
34−void net_sendRaw(void* conn, char* server, int port, VAL pkt);
35−VAL net_recvRaw(void* conn);
36−void dumpPkt(VAL pkt);
support/testidr.cdeleted−6
@@ -1,6 +0,0 @@
1−#include "testidr.h"
2−#include <stdio.h>
3−
4−void testIdr() {
5− printf("Hello Idris support library!\n");
6−}
support/testidr.hdeleted−7
@@ -1,7 +0,0 @@
1−#ifndef __TESTIDR_H
2−#define __TESTIDR_H
3−
4−void testIdr();
5−
6−#endif
7−
support/threads.cdeleted−116
@@ -1,116 +0,0 @@
1−#include <closure.h>
2−#include "threads.h"
3−
4−// Threads and locks
5−
6−typedef struct {
7− pthread_mutex_t m_id;
8−} IdrisMutex;
9−
10−typedef struct {
11− pthread_t t_id;
12−} IdrisThread;
13−
14−IdrisMutex** idris_ms = NULL;
15−int idris_mutexes = 0;
16−
17−int idris_newLock()
18−{
19− pthread_mutex_t m;
20−
21− pthread_mutex_init(&m, NULL);
22− IdrisMutex* newm = EMALLOC(sizeof(IdrisMutex));
23− newm->m_id = m;
24−
25− // Increase space for the mutexes
26− if (idris_ms==NULL) {
27− idris_ms = (IdrisMutex**)EMALLOC(sizeof(IdrisMutex*));
28− idris_mutexes=1;
29− } else {
30− idris_ms = (IdrisMutex**)(EREALLOC(idris_ms, sizeof(IdrisMutex*)*(idris_mutexes+1)));
31− idris_mutexes++;
32− }
33−
34− idris_ms[idris_mutexes-1] = newm;
35− return idris_mutexes-1;
36−}
37−
38−void idris_doLock(int lock)
39−{
40− pthread_mutex_lock(&(idris_ms[lock]->m_id));
41−}
42−
43−void idris_doUnlock(int lock)
44−{
45− pthread_mutex_unlock(&(idris_ms[lock]->m_id));
46−}
47−
48−struct idris_threadinfo {
49− void* proc;
50− void* result;
51−};
52−
53−void* idris_runThread(void* th_in) {
54− printf("IN THREAD\n");
55− struct idris_threadinfo* th = (struct idris_threadinfo*)th_in;
56− printf("using %d\n", th_in);
57− void* v = DO_EVAL(th_in, 1);
58− th->result = v;
59− return v;
60−}
61−
62−void idris_doFork(void* proc)
63−{
64− printf("FORKING!\n");
65− pthread_t* t = malloc(sizeof(pthread_t));
66− struct idris_threadinfo th;
67− printf("in %d\n", proc);
68− th.proc = proc;
69− th.result = NULL;
70− pthread_create(t, NULL, idris_runThread, proc);
71− printf("FORKED!\n");
72−}
73−
74−void* idris_doWithin(int limit, void* proc, void* doOnFail)
75−{
76− pthread_t* t = EMALLOC(sizeof(pthread_t));
77−// printf("CREATING THREAD %d\n", t);
78− struct idris_threadinfo th;
79− th.proc = proc;
80− th.result = NULL;
81−
82− struct timeval tv;
83− gettimeofday(&tv, NULL);
84− int tnow, tthen = do_utime();
85−
86− pthread_create(t, NULL, idris_runThread, &th);
87−// printf("tthen %d\n", tthen);
88−
89− void* ans;
90−
91− do
92− {
93− // If the answer has been updated, we're done.
94− if (th.result!=NULL) {
95− pthread_join(*t, &ans);
96− return ans;
97− }
98− gettimeofday(&tv, NULL);
99− tnow = idris_utime();
100− usleep(100);
101−// printf("tnow %d\n", tnow);
102− }
103− while(tnow<(tthen+(limit*1000)));
104− pthread_cancel(*t);
105− return DO_EVAL(doOnFail,1);
106−}
107−
108−int idris_utime() {
109− struct timeval tv;
110− gettimeofday(&tv, NULL);
111−
112− static int start=0;
113− if (start==0) { start = tv.tv_sec; }
114−
115− return 1000000*(tv.tv_sec - start)+tv.tv_usec;
116−}
support/threads.hdeleted−11
@@ -1,11 +0,0 @@
1−#ifndef _THREADS_H
2−#define _THREADS_H
3−
4−int idris_newLock();
5−void idris_doLock(int lock);
6−void idris_doUnlock(int lock);
7−void idris_doFork(void* proc);
8−void* idris_doWithin(int limit, void* proc, void* doOnFail);
9−int idris_utime();
10−
11−#endif