-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ccnl-ext-nfn.c
155 lines (133 loc) · 4.77 KB
/
ccnl-ext-nfn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* @f ccn-lite-relay.c
* @b user space CCN relay
*
* Copyright (C) 2011-14, Christian Tschudin, University of Basel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* File history:
* 2014-02-06 <christopher.scherb@unibas.ch>created
*/
#ifndef CCNL_EXT_NFN_H
#define CCNL_EXT_NFN_H
#include "ccnl-core.h"
#include "krivine.c"
#include "krivine-common.c"
#include "ccnl-includes.h"
static int
ccnl_nfn_count_required_thunks(char *str)
{
int num = 0;
char *tok;
tok = str;
while((tok = strstr(tok, "call")) != NULL ){
tok += 4;
++num;
}
DEBUGMSG(99, "Number of required Thunks is: %d\n", num);
return num;
}
void *
ccnl_nfn_thread(void *arg)
{
struct thread_parameter_s *t = ((struct thread_parameter_s*)arg);
struct ccnl_relay_s *ccnl = t->ccnl;
struct ccnl_buf_s *orig = t->orig;
struct ccnl_prefix_s *prefix = t->prefix;
struct ccnl_face_s *from = t->from;
struct thread_s *thread = t->thread;
int thunk_request = 0;
int num_of_thunks = 0;
struct ccnl_prefix_s *original_prefix;
DEBUGMSG(49, "ccnl_nfn(%p, %p, %p, %p)\n", ccnl, orig, prefix, from);
if(!memcmp(prefix->comp[prefix->compcnt-2], "THUNK", 5))
{
ccnl_nfn_copy_prefix(prefix, &original_prefix);
thunk_request = 1;
//FIXME: WARUM COPY VON PREFIX NÖTIG?
DEBUGMSG(99, " Thunk-request, currently not implementd\n");
//TODO: search for num of required thunks???
}
else{
original_prefix = prefix;
}
char str[CCNL_MAX_PACKET_SIZE];
int i, len = 0;
//put packet together
len = sprintf(str, "%s", prefix->comp[prefix->compcnt-2-thunk_request]);
if(prefix->compcnt > 2 + thunk_request){
len += sprintf(str + len, " ");
}
for(i = 0; i < prefix->compcnt-2-thunk_request; ++i){
len += sprintf(str + len, "/%s", prefix->comp[i]);
}
DEBUGMSG(99, "%s\n", str);
//search for result here... if found return...
if(thunk_request){
num_of_thunks = ccnl_nfn_count_required_thunks(str);
}
char *res = Krivine_reduction(ccnl, str, thunk_request, &num_of_thunks,
original_prefix, thread);
//stores result if computed
if(res){
DEBUGMSG(2,"Computation finshed: %s\n", res);
if(thunk_request){
original_prefix->comp[original_prefix->compcnt-2] = original_prefix->comp[original_prefix->compcnt-1];
original_prefix->complen[original_prefix->compcnt-2] = original_prefix->complen[original_prefix->compcnt-1];
--original_prefix->compcnt;
}
struct ccnl_content_s *c = create_content_object(ccnl, original_prefix, res, strlen(res));
c->flags = CCNL_CONTENT_FLAGS_STATIC;
if(!thunk_request)ccnl_content_serve_pending(ccnl,c);
ccnl_content_add2cache(ccnl, c);
}
//TODO: check if really necessary
/*if(thunk_request)
{
ccnl_nfn_delete_prefix(prefix);
}*/
struct thread_s *thread1 = main_thread;
pthread_cond_broadcast(&thread1->cond);
pthread_exit ((void *) 0);
return 0;
}
void
ccnl_nfn_send_signal(int threadid){
struct thread_s *thread = threads[threadid];
pthread_cond_broadcast(&thread->cond);
struct thread_s *thread1 = main_thread;
pthread_cond_wait(&(thread1->cond), &thread1->mutex);
}
int
ccnl_nfn(struct ccnl_relay_s *ccnl, struct ccnl_buf_s *orig,
struct ccnl_prefix_s *prefix, struct ccnl_face_s *from)
{
struct thread_s *thread = new_thread(threadid);
struct thread_parameter_s *arg = malloc(sizeof(struct thread_parameter_s *));
char *h = malloc(10);
arg->ccnl = ccnl;
arg->orig = orig;
arg->prefix = prefix;
arg->from = from;
arg->thread = thread;
int threadpos = -threadid;
threads[threadpos] = thread;
--threadid;
DEBUGMSG(99, "New Thread with threadid %d\n", -arg->thread->id);
pthread_create(&thread->thread, NULL, ccnl_nfn_thread, (void *)arg);
struct thread_s *thread1 = main_thread;
pthread_cond_wait(&(thread1->cond), &thread1->mutex);
return 0;
}
#endif //CCNL_EXT_NFN_H