aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libraries/SledjHamr.c
blob: 5e1d387dd4dbcd54a091d9b6750511720adb42ab (plain)
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
// This might become nails, since at the moment it's only got comms stuff in it.

#include <unistd.h>
#include "SledjHamr.h"


struct _conct
{
  char *address;
  int port;
  void *pointer;
  Ecore_Event_Handler_Cb addCb, dataCb, delCb;
  Ecore_Event_Handler *add, *data, *del;
};


static Eina_Bool _add(void *data, int type, Ecore_Con_Event_Server_Del *ev)
{
  struct _conct *this = data;

  if (this->addCb)
    this->addCb(this->pointer, type, ev);
  if (this->dataCb)
    ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, this->dataCb, this->pointer);

  return ECORE_CALLBACK_RENEW;
}

static Eina_Bool _delTimer(void *data)
{
  struct _conct *this = data;

  reachOut(this->address, this->port, this->pointer, this->addCb, this->dataCb, this->delCb);
  return ECORE_CALLBACK_CANCEL;
}

static Eina_Bool _del(void *data, int type, Ecore_Con_Event_Server_Del *ev)
{
  struct _conct *this = data;

  printf("FAILED connection to server %s:%d, trying again in a second!\n", this->address, this->port);
  ecore_event_handler_del(this->add);
  ecore_event_handler_del(this->del);

  if (this->delCb)
  {
    if (ECORE_CALLBACK_RENEW == this->delCb(this->pointer, type, ev))
      ecore_timer_add(1.0, _delTimer, this);
  }

  if (ev->server)  ecore_con_server_del(ev->server);
  // TODO - Hmm, I think this is where this should be freed, but it causes a seggie in reachOut's while loop.
  //        Which is odd, so leave it commented for now and investigate later.
//  free(this);

  return ECORE_CALLBACK_CANCEL;
}

Ecore_Con_Server *reachOut(char *address, int port, void *data, Ecore_Event_Handler_Cb _addCb, Ecore_Event_Handler_Cb _dataCb, Ecore_Event_Handler_Cb _delCb)
{
  Ecore_Con_Server *server = NULL;
  struct _conct *this = malloc(sizeof(struct _conct));
  int count = 0;

  this->address = address;
  this->port = port;
  this->pointer = data;
  this->addCb = _addCb;
  this->dataCb = _dataCb;
  this->delCb = _delCb;
  // This loop is overkill I think.
  while ((!server) && (10 > count))
  {
    if ((server = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, address, port, this->pointer)))
    {
      this->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) _add, this);
      this->del = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb) _del, this);
    }
    count++;
  }

  if (!server)
    printf("Failed to connect to server %s:%d!\n", this->address, this->port);

  return server;
}

void sendBack(Ecore_Con_Client *client, const char *SID, const char *message, ...)
{
    va_list args;
    char buf[PATH_MAX];
    int length = strlen(SID);

    strncpy(buf, SID, length);
    buf[length++] = '.';
    va_start(args, message);
    length += vsprintf(&buf[length], message, args);
    va_end(args);
    buf[length++] = '\n';
    buf[length++] = '\0';
    ecore_con_client_send(client, buf, strlen(buf));
    ecore_con_client_flush(client);
}

void sendForth(Ecore_Con_Server	*server, const char *SID, const char *message, ...)
{
    va_list args;
    char buf[PATH_MAX];
    int length = strlen(SID);

    strncpy(buf, SID, length);
    buf[length++] = '.';
    va_start(args, message);
    length += vsprintf(&buf[length], message, args);
    va_end(args);
    buf[length++] = '\n';
    buf[length++] = '\0';
    ecore_con_server_send(server, buf, strlen(buf));
    ecore_con_server_flush(server);
}