Statistics
| Branch: | Revision:

grapes / som / Tests / chunk_sending_test.c @ 8ab58ec7

History | View | Annotate | Download (2.57 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.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 = net_helper_init(my_addr, port);
50
  if (myID == NULL) {
51
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
52

    
53
    return NULL;
54
  }
55
  chunkInit(myID);
56

    
57
  return myID;
58
}
59

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

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

    
81
        exit(-1);
82
    }
83
  }
84
}
85

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

    
91
  cmdline_parse(argc, argv);
92

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

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

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

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

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

    
123
  return 0;
124
}
125