/
bang-com.h
153 lines (136 loc) · 3.39 KB
/
bang-com.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* \file bang-com.h
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief Functions to communicate between peers using a master slave system.
* */
#ifndef __BANG_COM_H
#define __BANG_COM_H
/**
* \page Peer Implementation
* Outgoing requests are serviced by iterating through the peers and sending out requests using the
* peers list and corresponding structure. There are two threads for each peer. One to manage
* incoming requests, and one to manage outgoing requests.
*/
#include<poll.h>
#include<pthread.h>
#include<semaphore.h>
/**
* A linked list of requests of peer send threads.
*/
typedef struct _request_node {
/**
* The next node in the request list.
*/
struct _request_node *next;
} request_node;
/**
* \return The head of the request node list starting at head.
*
* \brief Pops off the head of the linked list.
*/
request_node* pop_request(request_node **head);
/**
* Holds requests of the peers in a linked list.
*/
typedef struct {
/**
* The lock on adding or removing requests.
*/
pthread_mutex_t lock;
/**
* Signals the thread that a new requests has been added.
*/
sem_t num_requests;
/**
* A linked list of requests
*/
request_node *head;
} BANG_requests;
/**
* Represents one of our peers.
*/
typedef struct {
int peer_id;
char *peername;
int socket;
pthread_t receive_thread;
BANG_requests *requests;
pthread_t send_thread;
struct pollfd pfd;
} peer;
/**
* \brief Initializes the bang-com part of BANG.
*/
void BANG_com_init();
/**
* \brief Closes down all the peer threads, destroy the semaphores, and
* frees all the used memory.
*/
void BANG_com_close();
/**
* \param self_info Information about the peer.
*
* \brief A peer connection thread.
*/
void* BANG_read_peer_thread(void *self_info);
/**
* \param self_info Information about the peer.
*
* \brief A peer connection thread.
*/
void* BANG_write_peer_thread(void *self_info);
/**
* \param socket The slave client socket.
*
* \brief Creates a slave thread using the socket gotten from the signal.
*/
void BANG_peer_added(int signal, int sig_id, void *socket);
/**
* \param peer_id The peer to be remove
*
* \brief Catches BANG_PEER_TO_BE_REMOVED, and remove that peer, and emits
*/
void BANG_peer_removed(int signal, int sig_id, void *peer_id);
/**
*
* \return A new unique peer id.
*/
int BANG_new_peer_id();
/**
* \param peer_id The peer you want to add a request.
* \param request The request you want to add to the peer.
*
* \brief Adds a request to the peer_id peer.
*/
///TODO: Figure out the structure for the request
void BANG_add_request_to_peer(int peer_id, void *request);
/**
* \param request The request to add.
*
* \brief Adds request to all peers.
*/
void BANG_add_request_all(void *request);
/**
* \param peer_id Peer to remove.
*
* \brief Removes peer_id, and it associated threads from existence, and emits
* a BANG_PEER_REMOVED. If you want the peer to removed in a different thread, send
* out a BANG_PEER_TO_BE_REMOVED
*/
void BANG_remove_peer(int peer_id);
/**
* \param socket
*
* \brief Adds a peer with socket, creates its threads. If you want other functions to
* be able to catch this, remember to use a signal.
*/
void BANG_add_peer(int socket);
/**
* \param peer_id The id of the peer thread.
*
* \brief Gets the current key of the peer id (the place it is located in the keys array).
*/
int BANG_get_key_with_peer_id(int peer_id);
#endif