Now you can download a copy of these docs so you can use them offline! Download now
ServerConnectionAdapter.cpp
1 /*
2  * ServerConnectionAdapter.cpp
3  *
4  * Created on: Sep 26, 2012
5  * Author: Mitchell Wills
6  */
7 
8 #include "networktables2/server/ServerConnectionAdapter.h"
9 #include <stdio.h>
10 
11 
12 void ServerConnectionAdapter::gotoState(ServerConnectionState* newState){
13  if(connectionState!=newState){
14  fprintf(stdout, "[NT] %p entered connection state: %s\n", (void*)this, newState->toString());
15  fflush(stdout);
16  connectionState = newState;
17  }
18 }
19 
21  entryStore(_entryStore), transactionReceiver(_transactionReceiver), adapterListener(_adapterListener),
22  connection(stream, typeManager), monitorThread(*this, connection), m_IsAdapterListenerClosed(false) {
25  readThread = threadManager.newBlockingPeriodicThread(&monitorThread, "Server Connection Reader Thread");
26 }
27 
28 ServerConnectionAdapter::~ServerConnectionAdapter(){
29  delete readThread;
30 }
31 
32 
34  fprintf(stdout, "[NT] Bad message: %s\n", e.what());
35  fflush(stdout);
36  gotoState(new ServerConnectionState_Error(e));
37  adapterListener.close(*this, true);
38  m_IsAdapterListenerClosed=true;
39 }
40 
42  fprintf(stdout, "[NT] IOException message: %s\n", e.what());
43  fflush(stdout);
44  if(e.isEOF())
46  else
47  gotoState(new ServerConnectionState_Error(e));
48  adapterListener.close(*this, false);
49  m_IsAdapterListenerClosed=true;
50 }
51 
52 
53 void ServerConnectionAdapter::shutdown(bool closeStream) {
54  readThread->stop();
55  if(closeStream)
56  connection.close();
57 }
58 
59 bool ServerConnectionAdapter::keepAlive() {
60  return !m_IsAdapterListenerClosed; //returns true as long as the adapter listener has not been flagged for closing
61 }
62 
63 void ServerConnectionAdapter::clientHello(ProtocolVersion protocolRevision) {
65  throw BadMessageException("A server should not receive a client hello after it has already connected/entered an error state");
66  if(protocolRevision!=NetworkTableConnection::PROTOCOL_REVISION){
67  connection.sendProtocolVersionUnsupported();
68  throw BadMessageException("Client Connected with bad protocol revision");
69  }
70  else{
71  entryStore.sendServerHello(connection);
73  }
74 }
75 
76 void ServerConnectionAdapter::protocolVersionUnsupported(ProtocolVersion protocolRevision) {
77  throw BadMessageException("A server should not receive a protocol version unsupported message");
78 }
79 
80 void ServerConnectionAdapter::serverHelloComplete() {
81  throw BadMessageException("A server should not receive a server hello complete message");
82 }
83 
84 void ServerConnectionAdapter::offerIncomingAssignment(NetworkTableEntry* entry) {
85  transactionReceiver.offerIncomingAssignment(entry);
86 }
87 
88 void ServerConnectionAdapter::offerIncomingUpdate(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value) {
89  transactionReceiver.offerIncomingUpdate(entry, sequenceNumber, value);
90 }
91 
92 NetworkTableEntry* ServerConnectionAdapter::GetEntry(EntryId id) {
93  return entryStore.GetEntry(id);
94 }
95 
96 void ServerConnectionAdapter::offerOutgoingAssignment(NetworkTableEntry* entry) {
97  try {
98  if(connectionState==&ServerConnectionState::CONNECTED_TO_CLIENT)
99  connection.sendEntryAssignment(*entry);
100  } catch (IOException& e) {
101  ioException(e);
102  }
103 }
104 void ServerConnectionAdapter::offerOutgoingUpdate(NetworkTableEntry* entry) {
105  try {
106  if(connectionState==&ServerConnectionState::CONNECTED_TO_CLIENT)
107  connection.sendEntryUpdate(*entry);
108  } catch (IOException& e) {
109  ioException(e);
110  }
111 }
112 
113 
114 void ServerConnectionAdapter::flush() {
115  try {
116  connection.flush();
117  } catch (IOException& e) {
118  ioException(e);
119  }
120 }
121 
126  return connectionState;
127 }
128 
129 void ServerConnectionAdapter::ensureAlive() {
130  try {
131  connection.sendKeepAlive();
132  } catch (IOException& e) {
133  ioException(e);
134  }
135 }
static ServerConnectionState GOT_CONNECTION_FROM_CLIENT
virtual NTThread * newBlockingPeriodicThread(PeriodicRunnable *r, const char *name)=0
void ioException(IOException &e)
const char * what()
Definition: IOException.cpp:16
NetworkTableConnection connection
ServerConnectionState * getConnectionState()
virtual void close(ServerConnectionAdapter &connectionAdapter, bool closeStream)=0
virtual void stop()=0
ServerConnectionAdapter(IOStream *stream, ServerNetworkTableEntryStore &entryStore, IncomingEntryReceiver &transactionReceiver, ServerAdapterManager &adapterListener, NetworkTableEntryTypeManager &typeManager, NTThreadManager &threadManager)
void sendServerHello(NetworkTableConnection &connection)
void badMessage(BadMessageException &e)
void shutdown(bool closeStream)
static ServerConnectionState CLIENT_DISCONNECTED
Definition: ITable.h:13
static ServerConnectionState CONNECTED_TO_CLIENT
virtual bool isEOF()
Definition: IOException.cpp:20

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6