Statistics
| Branch: | Tag: | Revision:

sssimulator / peer_event.c @ 4123e0f7

History | View | Annotate | Download (5.4 KB)

1
/*
2
 * this is sssim: the simple & stupid simulator
3
 *
4
 *  copyright (c) 2015 luca baldesi
5
 *
6
 *  this is free software; see gpl-3.0.txt
7
 */
8

    
9
#include<action_event_struct.h>
10
#include "peer_event.h"
11

    
12
/********************PEER OFFER******************************/
13
struct peer_offer_event{
14
        struct action_event ae;
15
        struct ed_simulator * eds;
16
        struct peer * src;
17
};
18

    
19
void peer_offer_event_destroy(struct action_event ** ae)
20
{
21
        struct peer_offer_event * po;
22
        po = (struct peer_offer_event *) *ae;
23
        free(po);
24
        *ae = NULL;
25
}
26

    
27
int peer_offer_event_trigger(struct action_event * ae)
28
{
29
        struct peer * dst;
30
        int chunk, t;
31
        struct peer_offer_event * po;
32

    
33
        po = (struct peer_offer_event *) ae;
34
//        fprintf(stderr,"[DEBUG] peer %d is offering\n", (po->src)->id);
35
        t = (int) ed_simulator_time(po->eds);
36
        peer_send(po->src, t, &chunk, &dst);
37
        if (dst != NULL)
38
        {
39
                //fprintf(stderr,"[DEBUG] peer %d send to %d\n", (po->src)->id, dst->id);
40
                (po->src)->chunks[win_idx(po->src, chunk)]->dl += ed_simulator_server_period(po->eds);
41
                ed_simulator_send_chunk(po->eds, chunk, dst, (po->src)->chunks[win_idx(po->src, chunk)]->dl);
42
        }
43
        ed_simulator_schedule(po->eds, ae, (po->src)->offer_rate);
44
        return dst ? 0 : -1;
45
}
46

    
47
struct action_event * peer_offer_event_new(struct ed_simulator * eds, struct peer *src)
48
{
49
        struct peer_offer_event * po;
50
        po = (struct peer_offer_event *) malloc (sizeof(struct peer_offer_event));
51
        action_event_init((struct action_event *)&(po->ae), peer_offer_event_trigger, peer_offer_event_destroy);
52
        po->eds = eds;
53
        po->src = src;
54
        return (struct action_event *) po;
55
}
56

    
57
/********************LINK OFFER******************************/
58
struct link_offer_event{
59
        struct action_event ae;
60
        struct ed_simulator * eds;
61
        struct peer * src;
62
        int dst_idx;
63
};
64

    
65
void link_offer_event_destroy(struct action_event ** ae)
66
{
67
        struct link_offer_event * po;
68
        po = (struct link_offer_event *) *ae;
69
        free(po);
70
        *ae = NULL;
71
}
72

    
73
int link_offer_event_trigger(struct action_event * ae)
74
{
75
        int chunk, t;
76
        struct peer * dst;
77
        struct link_offer_event * po;
78

    
79
        po = (struct link_offer_event *) ae;
80
//        fprintf(stderr,"[DEBUG] peer %d is offering\n", (po->src)->id);
81
        t = (int) ed_simulator_time(po->eds);
82
        dst = (po->src)->neighbour[po->dst_idx];
83
        peer_send_chunk(po->src, t, &chunk, dst);
84
        if (chunk >= 0)
85
        {
86
                //fprintf(stderr,"[DEBUG] %d: peer %d send chunk %d to %d\n", t, (po->src)->id, chunk, dst->id);
87
                (po->src)->chunks[win_idx(po->src, chunk)]->dl += ed_simulator_server_period(po->eds);
88
                ed_simulator_send_chunk(po->eds, chunk, dst, (po->src)->chunks[win_idx(po->src, chunk)]->dl);
89
        }
90
        //fprintf(stderr,"[DEBUG] offer rate: %f, prob: %f, interval: %f\n", (po->src)->offer_rate,  (po->src)->neighbour_prob[po->dst_idx], (po->src)->offer_rate * 1/((po->src)->neighbour_prob[po->dst_idx]));
91
        ed_simulator_schedule(po->eds, ae, (po->src)->offer_rate * 1/((po->src)->neighbour_prob[po->dst_idx]));
92
        return chunk >= 0 ? 0 : -1;
93
}
94

    
95
struct action_event * link_offer_event_new(struct ed_simulator * eds, struct peer *src, int dst_idx)
96
{
97
        struct link_offer_event * po;
98
        po = (struct link_offer_event *) malloc (sizeof(struct link_offer_event));
99
        action_event_init((struct action_event *)&(po->ae), link_offer_event_trigger, link_offer_event_destroy);
100
        po->eds = eds;
101
        po->src = src;
102
        po->dst_idx = dst_idx;
103
        return (struct action_event *) po;
104
}
105
/********************PEER RECEPTION******************************/
106
struct peer_reception_event{
107
        struct action_event ae;
108
        struct ed_simulator * eds;
109
        struct peer * dst;
110
        int chunk;
111
};
112

    
113
void peer_reception_event_destroy(struct action_event ** ae)
114
{
115
        struct peer_reception_event * po;
116
        po = (struct peer_reception_event *) *ae;
117
        free(po);
118
        *ae = NULL;
119
}
120

    
121
int peer_reception_event_trigger(struct action_event * ae)
122
{
123
        struct peer_reception_event * po;
124

    
125
        po = (struct peer_reception_event *) ae;
126
//        fprintf(stderr,"[DEBUG] peer %d is receiving\n", (po->dst)->id);
127
        ed_simulator_receive_chunk(po->eds, po->dst, po->chunk);
128
        peer_reception_event_destroy(&ae);
129
        return 0;
130
}
131

    
132
struct action_event * peer_reception_event_new(struct ed_simulator * eds, struct peer* dst,int chunk)
133
{
134
        struct peer_reception_event * po;
135
        po = (struct peer_reception_event *) malloc (sizeof(struct peer_reception_event));
136
        action_event_init((struct action_event *)&(po->ae), peer_reception_event_trigger, peer_reception_event_destroy);
137
        po->eds = eds;
138
        po->dst = dst;
139
        po->chunk = chunk;
140
        return (struct action_event *) po;
141
}
142

    
143
/********************SOURCE SEED******************************/
144
struct source_event{
145
        struct action_event ae;
146
        struct ed_simulator * eds;
147
};
148

    
149
void source_event_destroy(struct action_event ** ae)
150
{
151
        struct source_event * po;
152
        po = (struct source_event *) *ae;
153
        free(po);
154
        *ae = NULL;
155
}
156

    
157
int source_event_trigger(struct action_event * ae)
158
{
159
        struct peer * dst;
160
        int t;
161
        struct source_event * po;
162

    
163
        po = (struct source_event *) ae;
164
        ed_simulator_status_print(po->eds);
165
        t = (int) ed_simulator_time(po->eds);
166
        source_send(t, &dst);
167
        if (dst && t < ed_simulator_num_chunks(po->eds))
168
        {
169
                //fprintf(stderr,"[DEBUG] source seeding %d to %d\n", t, dst->id);
170
                ed_simulator_send_chunk(po->eds, t, dst, t + ed_simulator_server_period(po->eds));
171
        }
172
        ed_simulator_schedule(po->eds, ae, 1);
173
        return dst ? 0 : -1;
174
}
175

    
176
struct action_event * source_event_new(struct ed_simulator * eds)
177
{
178
        struct source_event * po;
179
        po = (struct source_event *) malloc (sizeof(struct source_event));
180
        action_event_init((struct action_event *)&(po->ae), source_event_trigger, source_event_destroy);
181
        po->eds = eds;
182
        return (struct action_event *) po;
183
}
184