Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv MSG version of bittorrent to teshsuite
[simgrid.git] / teshsuite / msg / app-bittorrent / connection.c
1 /* Copyright (c) 2012-2014. 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
7 #include "connection.h"
8 #include "bittorrent.h"
9 #include <xbt/sysdep.h>
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_peers);
11
12 connection_t connection_new(int id)
13 {
14   connection_t connection = xbt_new(s_connection_t, 1);
15
16   connection->id              = id;
17   connection->mailbox         = bprintf("%d", id);
18   connection->bitfield        = 0;
19   connection->current_piece   = -1;
20   connection->interested      = 0;
21   connection->am_interested   = 0;
22   connection->choked_upload   = 1;
23   connection->choked_download = 1;
24   connection->peer_speed      = 0;
25   connection->last_unchoke    = 0;
26
27   return connection;
28 }
29
30 void connection_add_speed_value(connection_t connection, double speed)
31 {
32   connection->peer_speed = connection->peer_speed * 0.6 + speed * 0.4;
33 }
34
35 void connection_free(void* data)
36 {
37   connection_t co = (connection_t)data;
38   xbt_free(co->mailbox);
39   xbt_free(co);
40 }
41
42 int connection_has_piece(connection_t connection, unsigned int piece)
43 {
44   return (connection->bitfield & 1U << piece);
45 }