Statistics
| Branch: | Tag: | Revision:

sssimulator / peer_event.c @ 659ba19d

History | View | Annotate | Download (3.83 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);
52
        po->eds = eds;
53
        po->src = src;
54
        ((struct action_event *)&(po->ae))->destroy = peer_offer_event_destroy;
55
        return (struct action_event *) po;
56
}
57

    
58
/********************PEER RECEPTION******************************/
59
struct peer_reception_event{
60
        struct action_event ae;
61
        struct ed_simulator * eds;
62
        struct peer * dst;
63
        int chunk;
64
};
65

    
66
void peer_reception_event_destroy(struct action_event ** ae)
67
{
68
        struct peer_reception_event * po;
69
        po = (struct peer_reception_event *) *ae;
70
        free(po);
71
        *ae = NULL;
72
}
73

    
74
int peer_reception_event_trigger(struct action_event * ae)
75
{
76
        struct peer_reception_event * po;
77

    
78
        po = (struct peer_reception_event *) ae;
79
//        fprintf(stderr,"[DEBUG] peer %d is receiving\n", (po->dst)->id);
80
        ed_simulator_receive_chunk(po->eds, po->dst, po->chunk);
81
        peer_reception_event_destroy(&ae);
82
        return 0;
83
}
84

    
85
struct action_event * peer_reception_event_new(struct ed_simulator * eds, struct peer* dst,int chunk)
86
{
87
        struct peer_reception_event * po;
88
        po = (struct peer_reception_event *) malloc (sizeof(struct peer_reception_event));
89
        action_event_init((struct action_event *)&(po->ae), peer_reception_event_trigger);
90
        po->eds = eds;
91
        po->dst = dst;
92
        po->chunk = chunk;
93
        ((struct action_event *)&(po->ae))->destroy = peer_reception_event_destroy;
94
        return (struct action_event *) po;
95
}
96

    
97
/********************SOURCE SEED******************************/
98
struct source_event{
99
        struct action_event ae;
100
        struct ed_simulator * eds;
101
};
102

    
103
void source_event_destroy(struct action_event ** ae)
104
{
105
        struct source_event * po;
106
        po = (struct source_event *) *ae;
107
        free(po);
108
        *ae = NULL;
109
}
110

    
111
int source_event_trigger(struct action_event * ae)
112
{
113
        struct peer * dst;
114
        int t;
115
        struct source_event * po;
116

    
117
        po = (struct source_event *) ae;
118
        ed_simulator_status_print(po->eds);
119
        t = (int) ed_simulator_time(po->eds);
120
        source_send(t, &dst);
121
        if (dst && t < ed_simulator_num_chunks(po->eds))
122
        {
123
                //fprintf(stderr,"[DEBUG] source seeding %d to %d\n", t, dst->id);
124
                ed_simulator_send_chunk(po->eds, t, dst, t + ed_simulator_server_period(po->eds));
125
        }
126
        ed_simulator_schedule(po->eds, ae, 1);
127
        return dst ? 0 : -1;
128
}
129

    
130
struct action_event * source_event_new(struct ed_simulator * eds)
131
{
132
        struct source_event * po;
133
        po = (struct source_event *) malloc (sizeof(struct source_event));
134
        action_event_init((struct action_event *)&(po->ae), source_event_trigger);
135
        po->eds = eds;
136
        ((struct action_event *)&(po->ae))->destroy = source_event_destroy;
137
        return (struct action_event *) po;
138
}
139