CPN
Computational Process Networks
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CPN::ConnectionServer Class Reference

#include <ConnectionServer.h>

+ Collaboration diagram for CPN::ConnectionServer:

Classes

class  PendingConnection
 

Public Member Functions

 ConnectionServer (SockAddrList addrs, shared_ptr< Context > ctx)
 
void Poll ()
 
void Wakeup ()
 
void Close ()
 
shared_ptr< Sync::Future< int > > ConnectWriter (Key_t writerkey)
 
shared_ptr< Sync::Future< int > > ConnectReader (Key_t readerkey)
 
SocketAddress GetAddress ()
 
void Disable ()
 
void Enable ()
 
void LogState ()
 

Private Types

typedef std::multimap< Key_t,
shared_ptr< PendingConnection > > 
PendingMap
 

Private Member Functions

void PendingDone (Key_t key, PendingConnection *conn)
 

Private Attributes

PthreadMutex lock
 
shared_ptr< Contextcontext
 
Logger logger
 
ServerSocketHandle server
 
WakeupHandle wakeup
 
PendingMap pendingconnections
 
bool enabled
 

Detailed Description

The connnection server takes ownership of the listening socket and accepts new connections. It also has all the logic for creating new connections. When remote is active in the Kernel, the Kernel creates a connection server and all RemoteQueues receive a reference to it as well.

Definition at line 41 of file ConnectionServer.h.

Member Typedef Documentation

typedef std::multimap<Key_t, shared_ptr<PendingConnection> > CPN::ConnectionServer::PendingMap
private

Definition at line 106 of file ConnectionServer.h.

Constructor & Destructor Documentation

CPN::ConnectionServer::ConnectionServer ( SockAddrList  addrs,
shared_ptr< Context ctx 
)
Parameters
addrslist of socket addresses to use to listen on
ctxthe context which contains all the data connection data.

Definition at line 37 of file ConnectionServer.cc.

References ServerSocketHandle::Listen(), and server.

38  : context(ctx), logger(ctx.get(), Logger::INFO), enabled(true)
39  {
40  server.Listen(addrs);
41  }
void Listen(const SocketAddress &addr, int queuelength=256)
ServerSocketHandle server
shared_ptr< Context > context

+ Here is the call graph for this function:

Member Function Documentation

void CPN::ConnectionServer::Close ( )

Close the listening socket and shutdown the connection server.

Definition at line 92 of file ConnectionServer.cc.

References FileHandle::Close(), lock, pendingconnections, and server.

92  {
93  AutoPLock al(lock);
94  server.Close();
95  for (PendingMap::iterator itr = pendingconnections.begin();
96  itr != pendingconnections.end(); ++itr)
97  {
98  itr->second->Cancel();
99  }
100  }
ServerSocketHandle server
AutoLock< PthreadMutex > AutoPLock
void Close()
Close the file and reset the internal state.
Definition: FileHandle.cc:146

+ Here is the call graph for this function:

shared_ptr< Sync::Future< int > > CPN::ConnectionServer::ConnectReader ( Key_t  readerkey)

Request the connection server connect the RemoteQueue which has key readerkey to its other endpoint.

Parameters
readerkeythe key for the reader of this queue
Returns
A Sync::Future which will have the file descriptor.

Definition at line 134 of file ConnectionServer.cc.

References FileHandle::Closed(), lock, pendingconnections, and server.

Referenced by CPN::RemoteQueue::FileThreadEntryPoint().

134  {
135  if (server.Closed()) {
136  return shared_ptr<Sync::Future<int> >();
137  }
138  AutoPLock al(lock);
139  shared_ptr<PendingConnection> conn;
140  PendingMap::iterator entry = pendingconnections.find(readerkey);
141  if (entry == pendingconnections.end()) {
142  conn = shared_ptr<PendingConnection>(new PendingConnection(readerkey, this));
143  pendingconnections.insert(std::make_pair(readerkey, conn));
144  } else {
145  conn = entry->second;
146  }
147  return conn;
148  }
ServerSocketHandle server
AutoLock< PthreadMutex > AutoPLock
bool Closed() const
Definition: FileHandle.h:128

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

shared_ptr< Sync::Future< int > > CPN::ConnectionServer::ConnectWriter ( Key_t  writerkey)

Request the connectino server to connect the RemoteQueue which has key writekey to its other endpoint.

Parameters
writerkeythe writekey of the endpoint.
Returns
A Sync::Future which will contain the file descriptor when finished

Definition at line 102 of file ConnectionServer.cc.

References FileHandle::Closed(), SocketHandle::Connect(), context, SocketAddress::CreateIP(), enabled, FileHandle::FD(), CPN::Packet::header, lock, CPN::PACKET_ID_WRITER, pendingconnections, FileHandle::Reset(), server, CPN::Packet::SourceKey(), AutoLock< Lockable >::Unlock(), and FileHandle::Write().

Referenced by CPN::RemoteQueue::FileThreadEntryPoint().

102  {
103  if (server.Closed()) {
104  return shared_ptr<Sync::Future<int> >();
105  }
106  AutoPLock al(lock);
107  shared_ptr<PendingConnection> conn;
108  if (enabled) {
109  al.Unlock();
110  conn = shared_ptr<PendingConnection>(new PendingConnection(writerkey, this));
111  Key_t readerkey = context->GetWritersReader(writerkey);
112  Key_t kernelkey = context->GetReaderKernel(readerkey);
113  std::string hostname;
114  std::string servname;
115  context->GetKernelConnectionInfo(kernelkey, hostname, servname);
116  SocketHandle sock;
117  sock.Connect(SocketAddress::CreateIP(hostname, servname));
118  Packet packet(PACKET_ID_WRITER);
119  packet.SourceKey(writerkey).DestinationKey(readerkey);
120  unsigned num = sock.Write(&packet.header, sizeof(packet.header));
121  if (num != sizeof(packet.header)) {
122  conn->Cancel();
123  } else {
124  conn->Set(sock.FD());
125  sock.Reset();
126  }
127  } else {
128  conn = shared_ptr<PendingConnection>(new PendingConnection(writerkey, this));
129  pendingconnections.insert(std::make_pair(writerkey, conn));
130  }
131  return conn;
132  }
A FileHandle customized with some socket specific functionality and functions.
Definition: SocketHandle.h:34
ServerSocketHandle server
shared_ptr< Context > context
uint64_t Key_t
Definition: common.h:79
AutoLock< PthreadMutex > AutoPLock
bool Closed() const
Definition: FileHandle.h:128
void Reset()
Clear all internal state including the file descriptor! WARNING does not close the file! ...
Definition: FileHandle.cc:138
int FD() const
Definition: FileHandle.h:106
static SockAddrList CreateIP(unsigned serv)
Return a list of valid socket address for the given service number or port number.
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.
Definition: SocketHandle.cc:48
unsigned Write(const void *ptr, unsigned len)
Write data to the file descriptor.
Definition: FileHandle.cc:225

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CPN::ConnectionServer::Disable ( )

These functions are for testing and debugging. They should only be called from the unit tests or from the debugger.

Definition at line 156 of file ConnectionServer.cc.

References enabled, and lock.

156  {
157  AutoPLock al(lock);
158  enabled = false;
159  }
AutoLock< PthreadMutex > AutoPLock
void CPN::ConnectionServer::Enable ( )

Definition at line 161 of file ConnectionServer.cc.

References enabled, lock, and pendingconnections.

161  {
162  AutoPLock al(lock);
163  enabled = true;
164  PendingMap::iterator entry = pendingconnections.begin();
165  while (entry != pendingconnections.end()) {
166  entry->second->Cancel();
167  ++entry;
168  }
169  }
AutoLock< PthreadMutex > AutoPLock
SocketAddress CPN::ConnectionServer::GetAddress ( )
Returns
the address this connection server is listening on

Definition at line 150 of file ConnectionServer.cc.

References FileHandle::FD(), server, and SocketAddress::SetFromSockName().

150  {
151  SocketAddress addr;
152  addr.SetFromSockName(server.FD());
153  return addr;
154  }
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
ServerSocketHandle server
void SetFromSockName(int fd)
Fill this SocketAddress with data from this side of the connection represented by fd...
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void CPN::ConnectionServer::LogState ( )

Definition at line 171 of file ConnectionServer.cc.

References FileHandle::Closed(), enabled, Logger::Error(), logger, pendingconnections, and server.

171  {
172  if (server.Closed()) {
173  logger.Error("Server socket closed");
174  }
175  if (!enabled) {
176  logger.Error("Connection handler disabled??");
177  }
178  logger.Error("%u pending connections", pendingconnections.size());
179  }
ServerSocketHandle server
void Error(const char *fmt,...)
Definition: Logger.cc:159
bool Closed() const
Definition: FileHandle.h:128

+ Here is the call graph for this function:

void CPN::ConnectionServer::PendingDone ( Key_t  key,
PendingConnection conn 
)
private

Definition at line 181 of file ConnectionServer.cc.

References lock, and pendingconnections.

181  {
182  AutoPLock al(lock);
183  std::pair<PendingMap::iterator, PendingMap::iterator> range;
184  range = pendingconnections.equal_range(key);
185  PendingMap::iterator entry = range.first;
186  PendingMap::iterator end = range.second;
187  while (entry != end) {
188  if (entry->second.get() == conn) {
189  pendingconnections.erase(entry);
190  break;
191  }
192  ++entry;
193  }
194  }
AutoLock< PthreadMutex > AutoPLock
void CPN::ConnectionServer::Poll ( void  )

Poll is periodically called by the Kernel to accept new connections.

Definition at line 43 of file ConnectionServer.cc.

References ServerSocketHandle::Accept(), ErrnoException::Error(), Logger::Error(), lock, logger, CPN::PACKET_ID_READER, CPN::PACKET_ID_WRITER, pendingconnections, FileHandle::Poll(), WakeupHandle::Read(), FileHandle::Readable(), server, wakeup, and ErrnoException::what().

43  {
44  std::deque<FileHandle*> files;
45  files.push_back(&server);
46  files.push_back(&wakeup);
47  FileHandle::Poll(files.begin(), files.end(), -1);
48  wakeup.Read();
49  if (server.Readable()) {
50  server.Readable(false);
51  try {
52  SocketHandle sock(server.Accept());
53  Packet packet;
54  unsigned num = sock.Read(&packet.header, sizeof(packet.header));
55  if (num != sizeof(packet.header)) {
56  return;
57  }
58  if (!packet.Valid()) {
59  return;
60  }
61  if ((packet.Type() == PACKET_ID_READER)
62  || (packet.Type() == PACKET_ID_WRITER)) {
63  AutoPLock al(lock);
64  Key_t key = packet.DestinationKey();
65  shared_ptr<PendingConnection> conn;
66  std::pair<PendingMap::iterator, PendingMap::iterator> range;
67  range = pendingconnections.equal_range(key);
68  PendingMap::iterator entry = range.first;
69  PendingMap::iterator end = range.second;
70  while (entry != end) {
71  if (!entry->second->Done()) {
72  conn = entry->second;
73  break;
74  }
75  ++entry;
76  }
77  if (!conn) {
78  conn = shared_ptr<PendingConnection>(new PendingConnection(key, this));
79  pendingconnections.insert(std::make_pair(key, conn));
80  }
81  conn->Set(sock.FD());
82  sock.Reset();
83  }
84  } catch (const ErrnoException &e) {
85  // Ignore, if we had an error we closed the socket when we
86  // left the scope, will try again later
87  logger.Error("Exception in ConnectionServer main loop (e: %d): %s", e.Error(), e.what());
88  }
89  }
90  }
int Accept(SocketAddress &addr)
A FileHandle customized with some socket specific functionality and functions.
Definition: SocketHandle.h:34
ServerSocketHandle server
void Error(const char *fmt,...)
Definition: Logger.cc:159
uint64_t Key_t
Definition: common.h:79
void Read()
Read until not readable anymore.
Definition: WakeupHandle.cc:70
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
Definition: FileHandle.cc:34
AutoLock< PthreadMutex > AutoPLock
bool Readable(bool r)
Set that the file is currently readable or not.
Definition: FileHandle.h:86
virtual const char * what() const
unsigned Read(void *ptr, unsigned len)
Read data from the file descriptor.
Definition: FileHandle.cc:159
int Error() const

+ Here is the call graph for this function:

void CPN::ConnectionServer::Wakeup ( )
inline

A call to Wakeup will cause Poll to return even if no connections are accepted.

Definition at line 56 of file ConnectionServer.h.

Referenced by CPN::RemoteQueue::FileThreadEntryPoint().

56 { wakeup.SendWakeup(); }
void SendWakeup()
Causes this handler to become readable any Poll on this FileHandler will then return.
Definition: WakeupHandle.cc:50

+ Here is the caller graph for this function:

Member Data Documentation

shared_ptr<Context> CPN::ConnectionServer::context
private

Definition at line 108 of file ConnectionServer.h.

Referenced by ConnectWriter().

bool CPN::ConnectionServer::enabled
private

Definition at line 113 of file ConnectionServer.h.

Referenced by ConnectWriter(), Disable(), Enable(), and LogState().

PthreadMutex CPN::ConnectionServer::lock
private

Definition at line 107 of file ConnectionServer.h.

Referenced by Close(), ConnectReader(), ConnectWriter(), Disable(), Enable(), PendingDone(), and Poll().

Logger CPN::ConnectionServer::logger
private

Definition at line 109 of file ConnectionServer.h.

Referenced by LogState(), and Poll().

PendingMap CPN::ConnectionServer::pendingconnections
private
ServerSocketHandle CPN::ConnectionServer::server
private
WakeupHandle CPN::ConnectionServer::wakeup
private

Definition at line 111 of file ConnectionServer.h.

Referenced by Poll().


The documentation for this class was generated from the following files: