1 /* Copyright (c) 2012-2020. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "connection.h"
8 #include "bittorrent.h"
9 #include <xbt/sysdep.h>
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_peers);
12 connection_t connection_new(int id)
14 connection_t connection = xbt_new(s_connection_t, 1);
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;
30 void connection_add_speed_value(connection_t connection, double speed)
32 connection->peer_speed = connection->peer_speed * 0.6 + speed * 0.4;
35 void connection_free(void* data)
37 connection_t co = (connection_t)data;
38 xbt_free(co->mailbox);
42 int connection_has_piece(const s_connection_t* connection, unsigned int piece)
44 return (connection->bitfield & 1U << piece);