summaryrefslogtreecommitdiffstats
path: root/kenolaba/Network.cpp
blob: 4079feae0ea4f7c1e100c905e256dc6627360f61 (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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <config.h>

#include "Network.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

Listener::Listener(const char* h, int p, struct sockaddr_in s,bool r)
{
  if (h==0)
    host[0]=0;
  else {
    int l = strlen(h);
    if (l>99) l=99;
    strncpy(host, h, l);
    host[l] = 0;
  }
  port = p;
  sin = s;
  reachable = r;
}

Network::Network(int port)
{
  struct sockaddr_in name;
  int i,j;

  listeners.setAutoDelete(TRUE);

  fd = ::socket (PF_INET, SOCK_STREAM, 0);
  if (fd<0) return;
 
  for(i = 0; i<5;i++) {
    name.sin_family = AF_INET;
    name.sin_port = htons (port+i);
    name.sin_addr.s_addr = htonl (INADDR_ANY);
    if (bind (fd, (struct sockaddr *) &name, sizeof (name)) >= 0)
      break;
    //    printf("...Port %d in use\n", port+i);
  }
  mySin = name;
  //  printf("I'm using Port %d\n", port+i);
  if (i==5) {
    printf("Error in bind to port %d\n", port);
    close(fd);
    fd = -1;
    return;
  }     
  for(j = 0; j<i;j++)
    addListener("127.0.0.1", port+j);

  if (::listen(fd,5)<0) {
    printf("Error in listen\n");
    close(fd);
    fd = -1;
    return;
  }

  sn = new TQSocketNotifier( fd, TQSocketNotifier::Read );
  TQObject::connect( sn, TQ_SIGNAL(activated(int)),
		    this, TQ_SLOT(gotConnection()) );
}

Network::~Network()
{
  if (fd<0) return;
  close(fd);

  char tmp[50];
  int len = sprintf(tmp, "unreg %d", ntohs(mySin.sin_port));
  
  Listener* l;
  for(l=listeners.first(); l!=0; l=listeners.next()) {
    if (l->reachable)
      sendString( l->sin, tmp, len);
  }
  listeners.clear();

  delete sn;
}

void Network::gotConnection()
{
  static char tmp[1024];
  int len=0;
  struct sockaddr_in sin;
  socklen_t sz = sizeof (sin);

  //  printf("GotConnection: ");
  int s = accept(fd,(struct sockaddr *)&sin, &sz);
  if (s<0) {
    printf("Error in accept\n");
    return;
  }
  while(read(s, tmp+len, 1)==1) len++;
  close(s);
  tmp[len]=0; len++;
  //  printf("Got: '%s'\n",tmp);
  if (strncmp(tmp,"reg ",4)==0) {
    int port = atoi(tmp+4);
    sin.sin_port = htons( port );
    Listener *l = new Listener(0,0,sin);
    //    printf("Reg of 0x%x:%d\n", 
    //	   ntohl(sin.sin_addr.s_addr ), ntohs(sin.sin_port));
    listeners.append(l);
    return;
  }

  if (strncmp(tmp,"unreg ",6)==0) {
    int port = atoi(tmp+6);
    sin.sin_port = htons( port );
    Listener* l;
    for(l=listeners.first(); l!=0; l=listeners.next())
      if (l->sin.sin_addr.s_addr == sin.sin_addr.s_addr &&
	  l->sin.sin_port == sin.sin_port) break;
    if (l==0) {
      printf("Error: UnReg of 0x%x:%d. Not Found\n",
	     ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
      return;
    }
    listeners.remove(l);
    //    printf("UnReg of 0x%x:%d\n",
    //	   ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
    return;
  }

  if (strncmp(tmp,"pos ",4)==0) {
    emit gotPosition(tmp+4);
  }
}

void Network::addListener(const char* host, int port)
{
  struct hostent *hostinfo;
  struct sockaddr_in name;

  memset(&name, 0, sizeof(struct sockaddr_in));
  name.sin_family = AF_INET;
  name.sin_port = htons (port);
  hostinfo = gethostbyname (host);
  if (hostinfo == NULL) {
    printf ("Error in addListener: Unknown host %s.\n", host);
    return;
  }
  name.sin_addr = *(struct in_addr *) hostinfo->h_addr;

  Listener *l = new Listener(host,port,name);
//  printf("Added Listener %s, 0x%x:%d\n", 
//	 host, ntohl(name.sin_addr.s_addr), ntohs(name.sin_port));
  listeners.append(l);

  char tmp[50];
  int len = sprintf(tmp, "reg %d", ntohs(mySin.sin_port));

  if (!sendString( name, tmp, len))
    listeners.remove(l);
}

void Network::broadcast(const char* pos)
{
  char tmp[1024];
  int len = sprintf(tmp,"pos %s", pos);
  
  for(Listener* l=listeners.first(); l!=0; l=listeners.next())
    if (l->reachable)
      l->reachable = sendString(l->sin, tmp, len);
}

bool Network::sendString(struct sockaddr_in sin, char* str, int len)
{
  int s = ::socket (PF_INET, SOCK_STREAM, 0);
  if (s<0) {
    printf("Error in sendString/socket ??\n");
    return false;
  }
  if (::connect (s, (struct sockaddr *)&sin, sizeof (sin)) <0) {
    printf("Error in sendString/connect to socket 0x%x:%d\n", 
	   ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port) );
    return false;
  }
  write(s, str, len);
  close(s);
  //  printf("Send '%s' to 0x%x:%d\n", str, 
  //	 ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port) );
  return true;
}

#include "Network.moc"