-
Notifications
You must be signed in to change notification settings - Fork 4
/
mpsock_socket.c
116 lines (97 loc) · 2.93 KB
/
mpsock_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include "mpsock_socket.h"
#include "mpsock_connection.h"
#include "mpsock_def.h"
mpsock_socket* create_mpsocket(int sd, size_t initial_chunk_size, size_t num_conns, mpsock_buffer *buffer, int max_req_con, int max_req_serv, int max_req_mpsocket, int initial_alpha, int version, int alpha_max, int processing_skips, int use_initial_second_path, int use_random_path, int log_decisions, int log_traffic, int log_metrics, int scheduler_algo)
{
mpsock_socket *sock = (mpsock_socket*)malloc(sizeof(mpsock_socket));
sock->num_sends = 0;
sock->max_req_con = max_req_con;
sock->max_req_serv = max_req_serv;
sock->max_req_mpsocket = max_req_mpsocket;
sock->initial_alpha = initial_alpha;
sock->alpha_max = alpha_max;
sock->scheduler_version = version;
sock->processing_skips = processing_skips;
sock->use_initial_second_path = use_initial_second_path;
sock->use_random_path = use_random_path;
sock->log_decisions = log_decisions;
sock->log_traffic = log_traffic;
sock->log_metrics = log_metrics;
sock->scheduler_algo = scheduler_algo;
// set chunk size
sock->initial_chunk_size = initial_chunk_size;
// connection count...
sock->max_connections = num_conns;
// create pool
mpsock_pool *pool = create_pool(sock,buffer,sd);
sock->pool = pool;
// set main socket descriptor
sock->m_sd = sd;
if(USE_ASSERTS) assert(HASH_COUNT(pool->connection_table) == 1);
if(USE_ASSERTS) assert(get_random_free_connection(pool)->sd == sd);
// add new socket to hash table
HASH_ADD_INT(mpsock_socket_table, m_sd, sock);
LOG_INFO("%stotal number of open mpsockets: %d",RESULT_EVENT,HASH_COUNT(mpsock_socket_table));
return sock;
}
void free_socket(mpsock_socket *sock)
{
// TODO: HASH_DEL
free_pool(sock->pool);
HASH_DEL(mpsock_socket_table,sock);
}
mpsock_socket* find_mpsocket(int m_sd)
{
mpsock_socket *sock;
HASH_FIND_INT(mpsock_socket_table, &m_sd, sock);
return sock;
}
int is_mpsocket(int m_sd)
{
mpsock_socket *sock = find_mpsocket(m_sd);
if(sock == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
size_t socket_bytes_ready(mpsock_socket *sock)
{
if(!sock->pool->is_response_created)
{
// we do not give back partwise responses, because only after fully parsing response
// we can verify whether there is a server problem to react upon or not
return 0;
}
else
{
return pool_bytes_ready(sock->pool);
}
}
size_t read_data_from_socket(mpsock_socket *sock, void *buf, size_t count, int flags)
{
return read_data_from_pool(sock->pool,buf,count,flags);
}
void set_port(mpsock_socket *sock, int port)
{
LOG_INFO("%sset_port(%d)",FUN_EVENT,port);
sock->port = port;
}
void set_hostname(mpsock_socket *sock, const char *host, size_t len)
{
sock->hostname[len] = '\0';
strncpy(sock->hostname,host,len);
}
void advance_socket_save_pointer(mpsock_socket *sock)
{
advance_pool_save_pointer(sock->pool);
}
void strip_mpsock_from_socket(mpsock_socket *sock)
{
HASH_DEL(mpsock_socket_table,sock);
// TODO: free structures
}