Statistics
| Branch: | Revision:

chunker-player / event_http / chunk_sender.c @ 59c85ad1

History | View | Annotate | Download (3.24 KB)

1
/**
2
 * @file chunk_sender.c
3
 *
4
 * Chunk Sender "object".
5
 *
6
 * No threads, thus it is based on callbacks and timer events.
7
 *
8
 * Napa-Wine project 2009-2010
9
 * @author: Giuseppe Tropea <giuseppe.tropea@lightcomm.it>
10
 */
11

    
12
#include <string.h>
13

    
14
#include "chunk_external_interface.h"
15
#include "ul_commons.h"
16

    
17
//this definition should seat in a central place...
18
#define UL_ENCODED_CHUNK_HEADER_SIZE 20
19

    
20
int ulSendChunk(Chunk *c) {
21
  //they will be NULL just the first time
22
  static struct evhttp_connection *htc[UL_MAX_EXTERNAL_APPLICATIONS]; //initialized to NULL by compiler
23
  char addr[UL_IP_ADDRESS_SIZE] = "";
24
  int port = 0;
25
  char path[UL_PATH_SIZE] = "";
26
  int i = 0;
27
  //the code in this function cycles through all registered applications,
28
  //so the return code should be an "OR" of the singular return values...
29
  int ret = 0;
30

    
31
  for(i=0; i<UL_MAX_EXTERNAL_APPLICATIONS; i++) {
32
    addr[0] = '\0';
33
    port = 0;
34
    path[0] = '\0';
35

    
36
    //check whether we have a registered application at position i
37
    if(ulRegisterApplication(addr, &port, path, &i) == UL_RETURN_FAIL) {
38
      //some problem occurred
39
      error("ulSendChunk unable to check connection at position %d", i);
40
      ret = UL_RETURN_FAIL;
41
    }
42
    else {
43
      //lets check if the RegisterApplication has given back an address for the empty slot
44
      if(strlen(addr)) {
45
/*
46
        if(htc[i] == NULL) { //we have an address but not a connection yet
47
          //setup a new connection with the newly registered application and put it in position i
48
          if(ulEventHttpClientSetup(addr, port, &htc[i]) == UL_RETURN_FAIL) {
49
            error("ulSendChunk unable to setup a connection to %s:%d at position %d", addr, port, i);
50
            ret = UL_RETURN_FAIL;
51
          }
52
        }
53
*/
54
        //push the chunk
55
        if(ulPushChunkToRemoteApplication(c, htc[i], addr, port, path)) {
56
          error("ulSendChunk unable to push chunk to application %s:%d%s through connection at position %d", addr, port, path, i);
57
          ret = UL_RETURN_FAIL;
58
        }
59
        else {
60
          //debug("ulSendChunk pushed chunk to application %s:%d%s through connection at position %d", addr, port, path, i);
61
          ret = UL_RETURN_OK;
62
        }
63
      }
64
      else {
65
        debug("ulSendChunk no application seems to be registered at position %d", i);
66
      }
67
    } //application registry check ok
68
  } //cycle all positions
69
  return ret;
70
}
71

    
72
int ulPushChunkToRemoteApplication(Chunk *c, struct evhttp_connection *htc, const char *addr, const int port, const char *path) {
73
  int encoded_chunk_size = 0;
74
  uint8_t *buff = NULL;
75
  int size = 0;
76
  int ret = 0;
77

    
78
  //encode the chunk into a bitstream
79
  encoded_chunk_size = UL_ENCODED_CHUNK_HEADER_SIZE + c->size + c->attributes_size;
80
  if( (buff=(uint8_t *)malloc(encoded_chunk_size)) == NULL ) {
81
    error("memory allocation failed in encoding chunk to push via http");
82
    return UL_RETURN_FAIL;
83
  }
84
  size = encodeChunk(c, buff, encoded_chunk_size);
85

    
86
  if(size > 0) {
87
    ret = ulEventHttpClientPostData(buff, size, htc, addr, port, path);
88
    debug("Just HTTP pushed chunk %d of %d bytes encoded into %d bytes. Ret value of %d", c->id, c->size, size, ret);
89
    free(buff);
90
    return UL_RETURN_OK;
91
  }
92
  else {
93
    warn("size zero in a encode chunk!!!!");
94
    free(buff);
95
    return UL_RETURN_FAIL;
96
  }
97
}