TraVisToClient.h 4.47 KB
Newer Older
1
/**
2 3
 * \file        TraVisToClient.h
 * \date        Jul 4, 2014
4 5
 * \version     v0.7
 * \copyright   <2009-2015> Forschungszentrum Jülich GmbH. All rights reserved.
6
 *
7
 * \section License
8 9 10
 * This file is part of JuPedSim.
 *
 * JuPedSim is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Lesser General Public License as published by
12 13 14 15 16 17 18 19
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * JuPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21 22
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
23
 * \section Description
24 25
 *
 *
26 27
 **/

Ulrich Kemloh's avatar
Ulrich Kemloh committed
28 29 30 31 32 33 34 35 36
#ifndef TRAVISTOCLIENT_H_
#define TRAVISTOCLIENT_H_

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

37
// #ifdef _WIN32
Ulrich Kemloh's avatar
Ulrich Kemloh committed
38

39
// #endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
40 41 42

#define QUEUE_LENGTH       5        ///< max queue length of pending connections

43 44 45 46 47 48 49 50
// inline
// void
// _printDebugLine(const char *fileName, int lineNumber)
// {
//   fprintf(stderr, "%-15s|%03d| ", fileName, lineNumber);
// }

// #ifdef TRACE_LOGGING
Ulrich Kemloh's avatar
Ulrich Kemloh committed
51

52 53 54 55 56 57 58 59 60 61 62
/*
 #define dtrace(...)                         \
     (_printDebugLine(__FILE__, __LINE__),   \
     fprintf(stderr, __VA_ARGS__),           \
     (void) fprintf(stderr, "\n"))

 #define derror(...)                         \
     (_printDebugLine(__FILE__, __LINE__),   \
     fprintf(stderr, "ERROR: "),             \
     fprintf(stderr, __VA_ARGS__),           \
     _printErrorMessage())
63
 */
Ulrich Kemloh's avatar
Ulrich Kemloh committed
64

65 66 67 68 69 70
// // #define dtrace(...)   _printDebugLine(__FILE__, __LINE__, false, __VA_ARGS__)
// // #define derror(...)   _printDebugLine(__FILE__, __LINE__, true, __VA_ARGS__)
// #else
// #define dtrace(...)   ((void) 0)
// #define derror(...)   (fprintf(stderr, __VA_ARGS__), _printErrorMessage())
// #endif /* TRACE_LOGGING */
Ulrich Kemloh's avatar
Ulrich Kemloh committed
71 72

#ifdef _WIN32
73 74 75 76 77 78 79 80
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
81

82
#pragma comment(lib, "Ws2_32.lib")
Ulrich Kemloh's avatar
Ulrich Kemloh committed
83 84
#define WS_MAJOR_VERSION   1        ///< major version of Winsock API
#define WS_MINOR_VERSION   1        ///< minor version of Winsock API
85
#define SHUT_RDWR          2  ///< @c SHUT_RDWR is POSIX standard
Ulrich Kemloh's avatar
Ulrich Kemloh committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
typedef SOCKET socket_t;
typedef int socklen_t;
#define startSocketSession() _startWin32SocketSession()
#define stopSocketSession()  _stopWin32SocketSession()

#else
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
typedef int socket_t;
#define u_int_32_t unsigned int
#define startSocketSession() (true)
#define stopSocketSession()  ((void) 0)
101 102 103
//#define closesocket          close
#define INVALID_SOCKET       (-1)
#define SOCKET_ERROR         (-1)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
104 105 106 107 108 109 110 111 112
#endif

#ifndef IPPORT_USERRESERVED
#define IPPORT_USERRESERVED  (5000)  ///< up to this number, ports are reserved and should not be used
#endif

#define PORT 8989
#define HOST "localhost"

113 114
class TraVisToClient
{
115
public:
Ulrich Kemloh's avatar
Ulrich Kemloh committed
116

117 118
     /// create a client with specific parameters
     TraVisToClient(std::string hostname = HOST, unsigned short port = PORT);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
119

120 121
     /// Destructor
     virtual ~TraVisToClient();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
122

123 124 125 126
     /// send datablock to the server
     /// this functions is still blocking unfortunately, so it may
     /// influence the execution time of your program
     void sendData(const char* data);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
127

128 129
     /// close the client (end the connection)
     void close();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
130

131 132 133
     /// send a datagram using the unreliable
     /// udp protokoll
     void sendDatagram(char *datagram);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
134

135 136
private:
     void createConnection();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
137

138
     unsigned long lookupHostAddress(const char *hostName);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
139

140 141
     socket_t createClientSocket(const char *serverName,
               unsigned short portNumber);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
142

143
     socket_t createServerSocket(unsigned short portNumber);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
144

145
     bool shutdownAndCloseSocket(socket_t sock);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
146

147
     bool sendMessage(socket_t sock, const void *msg, int msgSize);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
148

149
     bool receiveMessage(socket_t sock, void *msg, int msgSize);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
150

151
     void _printErrorMessage(void);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
152 153

#ifdef _WIN32
154 155
     bool _startWin32SocketSession(void);
     void _stopWin32SocketSession(void);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
156
#else
Mohcine Chraibi's avatar
Mohcine Chraibi committed
157
#define closesocket          close
Ulrich Kemloh's avatar
Ulrich Kemloh committed
158 159
#endif

160 161 162 163 164 165
private:
     bool _isConnected;
     socket_t _tcpSocket;
     std::string _hostname;
     unsigned short _port;
     std::vector<const char *> _msgQueue;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
166 167 168 169

};

#endif /* TRAVISTOCLIENT_H_ */