thr_example2.c 5.02 KB
Newer Older
Sven Robertz's avatar
Sven Robertz committed
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_thr_reader_writer.h>
#include "gen/simple.h"
#include "ThrottleDrv/throttle_drv.h"



static void handle_simple_TwoInts_foo(simple_TwoInts *v,void *context) {
  unsigned char *src = get_sender_addr((struct thr_chn_t *)context); 
  printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
  printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0],  src[1], src[2], src[3], src[4], src[5]);
}

static int encode(int argc, char *argv[]) {
  struct thr_chn_t *p_thr_chn = NULL;
  struct thr_chn_t *p_thr_chn2 = NULL;
  struct labcomm_encoder *encoder;
  struct labcomm_decoder *decoder;
  int i, j;
//  unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80};	/* other host MAC address, hardcoded...... :-( */
  unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0xe3, 0x81, 0xbf};	/* other host MAC address, hardcoded...... :-( */
  unsigned char chn_id = 0x01;
  unsigned short frag_size = 60;
  unsigned short freq = 1000;  /* milliseconds */

  char *ifname = argv[1];
  char *dest_mac_str = argv[2];

  if(parse_MAC_address(dest_mac_str, dest_mac)) {
	printf("failed to parse dest MAC address\n");
	return 1;
  }
  
  if (-1 == thr_init(ifname))
  {
     printf("Throttle Init failure.");
  }
  else
  {
    p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq,NULL);
    p_thr_chn2 = thr_open_chn(dest_mac, 17, frag_size, 100,(thr_msg_handler_t)labcomm_decoder_decode_one);
    encoder = labcomm_encoder_new(labcomm_thr_writer, p_thr_chn);
    labcomm_encoder_register_simple_TwoInts(encoder);
    labcomm_encoder_register_simple_IntString(encoder);
    decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn2);
    if (!decoder)
    {
      printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
      return 1;
    }
    labcomm_decoder_register_simple_TwoInts(decoder, handle_simple_TwoInts_foo, p_thr_chn2);

    int ret;
    unsigned char data[200];
    simple_IntString is;
    is.x = 24;
    is.s = "Hello, LabComm!";
    printf("Encoding IntString, x=%d, s=%s\n", is.x, is.s);
    labcomm_encode_simple_IntString(encoder, &is);

    simple_TwoInts ti;
    ti.a = 13;
    ti.b = 37;
    printf("Encoding TwoInts, a=%d, b=%d\n", ti.a, ti.b);
    labcomm_encode_simple_TwoInts(encoder, &ti);
    printf("Decoding:\n");
    thr_receive(p_thr_chn2, data, decoder);
    thr_receive(p_thr_chn2, data, decoder);

    labcomm_encoder_free(encoder);
    labcomm_decoder_free(decoder);
    thr_close_chn(p_thr_chn);
    thr_close_chn(p_thr_chn2);
    //client_exit(fd);
 }
}

static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
  unsigned char *src = get_sender_addr((struct thr_chn_t *)context); 
  printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
  printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0],  src[1], src[2], src[3], src[4], src[5]);
  struct thr_chan_t *ch = thr_open_chn(src, 17, (unsigned short)50, (unsigned short)100, NULL);
  struct labcomm_encoder *enc = labcomm_encoder_new(labcomm_thr_writer, ch);
  labcomm_encoder_register_simple_TwoInts(enc);
  v->a *= 2;
  v->b *= 3;
  labcomm_encode_simple_TwoInts(enc, v);
  labcomm_encoder_free(enc);
  thr_close_chn(ch);
}

static void handle_simple_IntString(simple_IntString *v,void *context) {
  printf("Got IntString. x=%d, s=%s\n", v->x, v->s);
  unsigned char *src = get_sender_addr((struct thr_chn_t *)context); 
  printf("... src addr: %x:%x:%x:%x:%x:%x\n", src[0],  src[1], src[2], src[3], src[4], src[5]);
}

static int decode(int argc, char *argv[]) {
  struct thr_chn_t *p_thr_chn = NULL;
  struct labcomm_decoder *decoder;
  unsigned char dest_mac[ETH_ADR_SIZE] = {0x00, 0x09, 0x6b, 0x10, 0xf3, 0x80};	/* other host MAC address, hardcoded...... :-( */
  int ret = 0;
  unsigned char chn_id = 0x01;
  unsigned short frag_size = 60;
  unsigned short freq = 1000;  /* milliseconds */
  unsigned char data[200];

  char *ifname = argv[1];
  if (-1 == thr_init(ifname))
  {
     printf("Throttle Init failure.");
  }
  else
  {
    p_thr_chn = thr_open_chn(dest_mac, chn_id, frag_size, freq, (thr_msg_handler_t)labcomm_decoder_decode_one);
    decoder = labcomm_decoder_new(labcomm_thr_reader, p_thr_chn);
    if (!decoder)
    {
      printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
      return 1;
    }
    labcomm_decoder_register_simple_TwoInts(decoder, handle_simple_TwoInts, p_thr_chn);
    labcomm_decoder_register_simple_IntString(decoder, handle_simple_IntString, p_thr_chn);

    printf("Decoding:\n");
    do {
    ret = thr_receive(p_thr_chn, data, decoder);
    } while(-1 != ret);
    {
      printf("Throttle Send receive failure.");
    }
    printf("--- End Of File ---:\n");
    labcomm_decoder_free(decoder);

    //server_exit(fd);
  }
}

int main(int argc, char *argv[]) {
	if(argc == 2) {
		return decode(argc, argv);
	} else if(argc == 3) {
		return encode(argc, argv);
	} else {
		printf("Usage: For encoding ethN xx:xx:xx:xx:xx:xx\n       For decoding ethN\n       where ethN is the ethernet interface to use\n       and xx:xx:...:xx is the destination MAC address"); 
		return 1;
	}
}