Statistics
| Branch: | Revision:

grapes / som / Tests / chunk_sending_test.c @ 026a7e5d

History | View | Annotate | Download (2.54 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see GPL.txt
5
 */
6
#include <sys/select.h>
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include <getopt.h>
12

    
13
#include "net_helper.h"
14
#include "chunk.h"
15
#include "trade_msg_la.h"
16
#include "trade_msg_ha.h"
17
#include "net_helpers.h"
18
#include "msg_types.h"
19

    
20
static const char *my_addr = "127.0.0.1";
21
static int port = 6666;
22
static int dst_port;
23
static const char *dst_ip;
24

    
25
#define BUFFSIZE 1024
26

    
27
static void chunk_print(FILE *f, const struct chunk *c)
28
{
29
  const uint8_t *p;
30

    
31
  fprintf(f, "Chunk %d:\n", c->id);
32
  fprintf(f, "\tTS: %llu\n", c->timestamp);
33
  fprintf(f, "\tPayload size: %d\n", c->size);
34
  fprintf(f, "\tAttributes size: %d\n", c->attributes_size);
35
  p = c->data;
36
  fprintf(f, "\tPayload:\n");
37
  fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
38
  if (c->attributes_size > 0) {
39
    p = c->attributes;
40
    fprintf(f, "\tAttributes:\n");
41
    fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
42
  }
43
}
44

    
45
static struct nodeID *init(void)
46
{
47
  struct nodeID *myID;
48

    
49
  myID = create_socket(my_addr, port);
50
  if (myID == NULL) {
51
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
52
  }
53
  chunkInit(myID);
54

    
55
  return myID;
56
}
57

    
58
static void cmdline_parse(int argc, char *argv[])
59
{
60
  int o;
61

    
62
  while ((o = getopt(argc, argv, "p:i:P:I:")) != -1) {
63
    switch(o) {
64
      case 'p':
65
        dst_port = atoi(optarg);
66
        break;
67
      case 'i':
68
        dst_ip = strdup(optarg);
69
        break;
70
      case 'P':
71
        port =  atoi(optarg);
72
        break;
73
      case 'I':
74
        my_addr = iface_addr(optarg);
75
        break;
76
      default:
77
        fprintf(stderr, "Error: unknown option %c\n", o);
78

    
79
        exit(-1);
80
    }
81
  }
82
}
83

    
84
int main(int argc, char *argv[])
85
{
86
  struct nodeID *my_sock;
87
  struct chunk c;
88

    
89
  cmdline_parse(argc, argv);
90

    
91
  my_sock = init();
92
  if (dst_port != 0) {
93
    struct nodeID *dst;
94

    
95
    c.id = 666;
96
    c.timestamp = 1000000000ULL;
97
    c.size = strlen("ciao") + 1;
98
    c.data = (uint8_t *)"ciao";
99
    c.attributes_size = 0;
100

    
101
    dst = create_socket(dst_ip, dst_port);
102
    sendChunk(dst, &c);
103
  } else {
104
    /* Receive a chunk and print it! */
105
    int res;
106
    struct nodeID *remote;
107
    static uint8_t buff[BUFFSIZE];
108

    
109
    res = recv_data(my_sock, &remote, buff, BUFFSIZE);
110
    /* TODO: Error check! */
111
    if (buff[0] != MSG_TYPE_CHUNK) {
112
      fprintf(stderr, "Wrong message type!!!\n");
113

    
114
      return -1;
115
    }
116
    res = decodeChunk(&c, buff + 1, res);
117
    fprintf(stdout, "Decoding: %d\n", res);
118
    chunk_print(stdout, &c);
119
  }
120

    
121
  return 0;
122
}
123