Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid...
[simgrid.git] / examples / msg / bittorrent / connection.c
1   /* Copyright (c) 2012-2013. The SimGrid Team.
2    * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "connection.h"
7 #include "bittorrent.h"
8 #include <xbt/sysdep.h>
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_peers);
10
11 connection_t connection_new(int id)
12 {
13   connection_t connection = xbt_new(s_connection_t, 1);
14
15   connection->id = id;
16   connection->mailbox = bprintf("%d", id);
17   connection->bitfield = NULL;
18   connection->current_piece = -1;
19   connection->interested = 0;
20   connection->am_interested = 0;
21   connection->choked_upload = 1;
22   connection->choked_download = 1;
23   connection->peer_speed = 0;
24   connection->last_unchoke = 0;
25
26   return connection;
27 }
28
29 void connection_add_speed_value(connection_t connection, double speed)
30 {
31   connection->peer_speed = connection->peer_speed * 0.6 + speed * 0.4;
32 }
33
34 void connection_free(void *data)
35 {
36   connection_t co = (connection_t) data;
37   xbt_free(co->bitfield);
38   xbt_free(co->mailbox);
39   xbt_free(co);
40
41 }