GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
networkClient.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 // POSIX includes
9 #ifdef _WIN32
10  #include <winsock2.h>
11 #else
12  #include <arpa/inet.h>
13  #include <netinet/in.h>
14  #include <netinet/tcp.h>
15  #include <sys/socket.h>
16  #include <sys/types.h>
17  #include <unistd.h>
18 #endif
19 
20 // SpineML common includes
21 #include "spineMLLogging.h"
22 //----------------------------------------------------------------------------
23 // SpineMLSimulator::NetworkClient
24 //----------------------------------------------------------------------------
25 namespace SpineMLSimulator
26 {
28 {
29 public:
30  //------------------------------------------------------------------------
31  // Enumerations
32  //------------------------------------------------------------------------
33  enum class Mode : char
34  {
35  Source = 45,
36  Target = 46,
37  };
38 
39  enum class DataType : char
40  {
41  Analogue = 31,
42  Events = 32,
43  Impulses = 33,
44  };
45 
46  NetworkClient();
47  NetworkClient(const std::string &hostname, unsigned int port, unsigned int size, DataType dataType, Mode mode, const std::string &connectionName);
49 
50  //----------------------------------------------------------------------------
51  // Public API
52  //----------------------------------------------------------------------------
53  bool connect(const std::string &hostname, unsigned int port, unsigned int size, DataType dataType, Mode mode, const std::string &connectionName);
54 
55  bool receive(std::vector<double> &buffer);
56  bool send(const std::vector<double> &buffer);
57 
58 private:
59  //------------------------------------------------------------------------
60  // Enumerations
61  //------------------------------------------------------------------------
62  enum class Response : char
63  {
64  Hello = 41,
65  Received = 42,
66  Abort = 43,
67  Finished = 44,
68  };
69 
70  //----------------------------------------------------------------------------
71  // Private API
72  //----------------------------------------------------------------------------
73  int startNonBlockingSend()
74  {
75 #ifdef _WIN32
76  // Turn on non-blocking mode
77  u_long nonBlockingMode = 1;
78  ioctlsocket(m_Socket, FIONBIO, &nonBlockingMode);
79 
80  // Don't use flags
81  return 0;
82 #else
83  // Use flags
84  return MSG_DONTWAIT;
85 #endif
86  }
87 
88  void endNonBlockingSend()
89  {
90 #ifdef _WIN32
91  // Turn off non-blocking mode
92  u_long nonBlockingMode = 0;
93  ioctlsocket(m_Socket, FIONBIO, &nonBlockingMode);
94 #endif
95  }
96 
97  template<typename Type>
98  bool sendRequestReadResponse(Type data, Response &response)
99  {
100  // Start non-blocking send mode and get flags for send (if any)
101  const int sendFlags = startNonBlockingSend();
102 
103  // Send request
104  if(::send(m_Socket, reinterpret_cast<const char*>(&data), sizeof(Type), sendFlags) < 0) {
105  LOGE_SPINEML << "Unable to send request";
106  return false;
107  }
108 
109  // End non-blocking send mode
110  endNonBlockingSend();
111 
112  // Receive handshake response
113  if(::recv(m_Socket, reinterpret_cast<char*>(&response), sizeof(Response), MSG_WAITALL) < 1) {
114  LOGE_SPINEML << "Unable to receive response";
115  return false;
116  }
117 
118  return true;
119  }
120 
121  bool sendRequestReadResponse(const std::string &data, Response &response);
122 
123  //----------------------------------------------------------------------------
124  // Private members
125  //----------------------------------------------------------------------------
126  int m_Socket;
127 };
128 
129 } // namespace SpineMLSimulator
Definition: networkClient.h:27
bool receive(std::vector< double > &buffer)
Definition: networkClient.cc:116
~NetworkClient()
Definition: networkClient.cc:26
bool connect(const std::string &hostname, unsigned int port, unsigned int size, DataType dataType, Mode mode, const std::string &connectionName)
Definition: networkClient.cc:38
Definition: connectors.h:25
NetworkClient()
Definition: networkClient.cc:15
bool send(const std::vector< double > &buffer)
Definition: networkClient.cc:150
Mode
Definition: networkClient.h:33
DataType
Definition: networkClient.h:39