Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/ConnectionManager.cc @ 348

Last change on this file since 348 was 337, checked in by bknecht, 17 years ago

merged merger to FICN branch

File size: 5.1 KB
Line 
1//
2// C++ Interface: ConnectionManager
3//
4// Description: The Class ConnectionManager manages the servers conenctions to the clients.
5// each connection is provided by a new process. communication between master process and
6// connection processes is provided by ...
7//
8//
9// Author:  Oliver Scheuss
10//
11
12#include "ConnectionManager.h"
13
14namespace network{
15
16  boost::thread_group network_threads;
17
18  void test(){
19    return;
20  }
21
22  ConnectionManager::ConnectionManager(){
23    quit=false;
24    client=NULL;
25    bindAddress.host = ENET_HOST_ANY;
26    bindAddress.port = NETWORK_PORT;
27  }
28
29  ConnectionManager::ConnectionManager(int port, std::string address){
30    quit=false;
31    client=NULL;
32    enet_address_set_host (& bindAddress, address.c_str());
33    bindAddress.port = NETWORK_PORT;
34  }
35
36  ConnectionManager::ConnectionManager(int port, const char *address){
37    quit=false;
38    client=NULL;
39    enet_address_set_host (& bindAddress, address);
40    bindAddress.port = NETWORK_PORT;
41  }
42
43  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
44    if(!buffer.isEmpty())
45      return buffer.pop(address);
46    else
47        return NULL;
48  }
49
50  bool ConnectionManager::queueEmpty(){
51    return buffer.isEmpty();
52  }
53
54  void ConnectionManager::createListener(){
55    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
56//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
57    return;
58  }
59
60  bool ConnectionManager::quitListener(){
61    quit=true;
62    network_threads.join_all();
63    return true;
64  }
65
66  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
67    if(client=NULL)
68      return false;
69    ClientList *temp=client;
70    while(peer->host != temp->event->peer->host){
71      temp=temp->next;
72      if(temp==NULL)
73        return false;
74    }
75    if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
76      return false;
77    return true;
78  }
79
80  bool ConnectionManager::addPacket(ENetPacket *packet, int ID){
81    if(client==NULL)
82      return false;
83    ClientList *temp=client;
84    while(ID != temp->ID){
85      temp=temp->next;
86      if(temp==NULL)
87        return false;
88    }
89    if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
90      return false;
91    else
92      return true;
93  }
94
95  bool ConnectionManager::addPacketAll(ENetPacket *packet){
96    ClientList *temp=client;
97    while(temp!=NULL){
98      if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
99         return false;
100    }
101    return true;
102  }
103
104  bool ConnectionManager::sendPackets(ENetEvent *event){
105    if(server==NULL)
106      return false;
107    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
108      return true;
109    else
110      return false;
111  }
112
113  void ConnectionManager::receiverThread(){
114    // what about some error-handling here ?
115    enet_initialize();
116    atexit(enet_deinitialize);
117    ENetEvent event;
118    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
119    if(server==NULL)
120      // add some error handling here ==========================
121      quit=true;
122
123    while(!quit){
124      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
125        // we should never reach this point
126        quit=true;
127        // add some error handling here ========================
128      }
129      switch(event.type){
130        // log handling ================
131        case ENET_EVENT_TYPE_CONNECT:
132        addClient(&event);
133        break;
134      case ENET_EVENT_TYPE_RECEIVE:
135        processData(&event);
136        break;
137      case ENET_EVENT_TYPE_DISCONNECT:
138        // add some error/log handling here
139        clientDisconnect(event.peer);
140        break;
141      }
142    }
143    // if we're finishied, destroy server
144    enet_host_destroy(server);
145  }
146
147  bool ConnectionManager::processData(ENetEvent *event){
148    // just add packet to the buffer
149    // this can be extended with some preprocessing
150    return buffer.push(event);
151  }
152
153  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
154    ClientList *temp=client;
155    // do we have to remove the first item ?
156    if(temp->event->peer->host==peer->host){
157      if(temp->next==NULL){
158        client=NULL;
159      } else{
160        client=temp->next;
161      }
162      delete temp;
163      return true;
164    } else {
165      while(temp->next!=NULL){
166        if(temp->next->event->peer->host==peer->host){
167          // do a correct relink and delete the disconnected client
168          ClientList *temp2=temp->next;
169          temp->next=temp2->next;
170          delete temp2;
171          return true;
172        } else
173          //otherwise keep on searching ;)
174          temp=temp->next;
175      }
176    }
177    return false;
178  }
179
180  bool ConnectionManager::addClient(ENetEvent *event){
181
182    // first client?
183    if(client==NULL){
184      client =new ClientList;
185      client->ID=1;
186      client->next=NULL;
187      client->event = event;
188    } else {
189      // otherwise add a new element to clientlist
190      ClientList *temp = client;
191      int i=1;
192      while(temp->next != NULL){
193        temp=temp->next;
194        i++;
195      }
196      temp->next=new ClientList;
197      temp=temp->next;
198      temp->ID=i+1;
199      temp->event=event;
200      temp->next=NULL;
201    }
202    return true;
203  }
204
205
206}
Note: See TracBrowser for help on using the repository browser.