/
channeltable.c
72 lines (64 loc) · 1.61 KB
/
channeltable.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
#include <stdlib.h>
#include "channeltable.h"
#include "knot_channel_state_uip.h"
typedef struct knot_channel{
ChannelState state;
struct knot_channel *nextChannel;
int active;
}Channel;
static Channel channelTable[CHANNEL_NUM];
static Channel *nextFree;
int size;
/*
* initialise the channel table
*/
void init_table(){
printf("Initialising table\n");
int i;
size = 0;
nextFree = channelTable;
for (i = 0; i < CHANNEL_NUM; i++){
channelTable[i].active = 0;
channelTable[i].nextChannel = (struct knot_channel *)&(channelTable[(i+1) % CHANNEL_NUM]);
init_state((&channelTable[i].state), i+ 1);
}
channelTable[CHANNEL_NUM-1].nextChannel = NULL;
}
/*
* create a new channel if space available
* return channel num if successful, 0 otherwise
*/
ChannelState * new_channel(){
if (size >= CHANNEL_NUM) return NULL;
Channel *temp = nextFree;
temp->active = 1;
nextFree = temp->nextChannel;
temp->nextChannel = NULL;
size++;
printf("New channel created\n");
return &(temp->state);
}
/*
* get the channel state for the given channel number
* return 1 if successful, 0 otherwise
*/
ChannelState * get_channel_state(int channel){
if (channelTable[channel-1].active){
return &(channelTable[channel-1].state);
} else return NULL;
}
/*
* remove specified channel state from table
* (scrubs and frees space in table for a new channel)
*/
void remove_channel(int channel){
channelTable[channel-1].nextChannel = nextFree;
init_state(&(channelTable[channel-1].state),channel);
channelTable[channel-1].active = 0;
nextFree = &channelTable[channel-1];
size--;
}
/*
* destroys table
*/
void destroy_table();