Building Controls Virtual Test Bed
util/utilSocket.h
Go to the documentation of this file.
1 // Methods for interfacing clients using BSD sockets.
2 
3 /*
4 ********************************************************************
5 Copyright Notice
6 ----------------
7 
8 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
9 Regents of the University of California, through Lawrence Berkeley
10 National Laboratory (subject to receipt of any required approvals from
11 the U.S. Dept. of Energy). All rights reserved.
12 
13 If you have questions about your rights to use or distribute this
14 software, please contact Berkeley Lab's Technology Transfer Department
15 at TTD@lbl.gov
16 
17 NOTICE. This software was developed under partial funding from the U.S.
18 Department of Energy. As such, the U.S. Government has been granted for
19 itself and others acting on its behalf a paid-up, nonexclusive,
20 irrevocable, worldwide license in the Software to reproduce, prepare
21 derivative works, and perform publicly and display publicly. Beginning
22 five (5) years after the date permission to assert copyright is obtained
23 from the U.S. Department of Energy, and subject to any subsequent five
24 (5) year renewals, the U.S. Government is granted for itself and others
25 acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
26 license in the Software to reproduce, prepare derivative works,
27 distribute copies to the public, perform publicly and display publicly,
28 and to permit others to do so.
29 
30 
31 Modified BSD License agreement
32 ------------------------------
33 
34 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
35 Regents of the University of California, through Lawrence Berkeley
36 National Laboratory (subject to receipt of any required approvals from
37 the U.S. Dept. of Energy). All rights reserved.
38 
39 Redistribution and use in source and binary forms, with or without
40 modification, are permitted provided that the following conditions are met:
41 
42  1. Redistributions of source code must retain the above copyright
43  notice, this list of conditions and the following disclaimer.
44  2. Redistributions in binary form must reproduce the above copyright
45  notice, this list of conditions and the following disclaimer in
46  the documentation and/or other materials provided with the
47  distribution.
48  3. Neither the name of the University of California, Lawrence
49  Berkeley National Laboratory, U.S. Dept. of Energy nor the names
50  of its contributors may be used to endorse or promote products
51  derived from this software without specific prior written permission.
52 
53 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
54 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
57 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 
65 You are under no obligation whatsoever to provide any bug fixes,
66 patches, or upgrades to the features, functionality or performance of
67 the source code ("Enhancements") to anyone; however, if you choose to
68 make your Enhancements available either publicly, or directly to
69 Lawrence Berkeley National Laboratory, without imposing a separate
70 written license agreement for such Enhancements, then you hereby grant
71 the following license: a non-exclusive, royalty-free perpetual license
72 to install, use, modify, prepare derivative works, incorporate into
73 other computer software, distribute, and sublicense such enhancements or
74 derivative works thereof, in binary and source code form.
75 
76 ********************************************************************
77 */
78 
109 #ifndef _UTILSOCKET_H_
110 #define _UTILSOCKET_H_
111 #ifdef _MSC_VER // Microsoft compiler
112 #include <windows.h>
113 #include <winsock.h>
114 
115 //#include <winsock2.h>
116 //#include <ws2tcpip.h> // this gives compile error due to bug in .h file
117 #else
118 // Include arpa/inet.h so that inet_ntoa is defined for 64bit linux
119 // and on Mac OS X 10.6.1 Snow Leopard
120 #include <arpa/inet.h>
121 #include <sys/socket.h>
122 //#include <netinet/in.h>
123 #include <netdb.h>
124 #endif
125 
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <sys/types.h>
129 #include <string.h>
130 #include <limits.h>
131 #include <math.h>
132 #include <errno.h>
133 
134 #include "defines.h"
135 
136 FILE *f1 = NULL;
137 #define HEADER_LENGTH 54 // =10 + 4*(10+1);
140 
143 int SERVER_VERSION = 0;
144 
158 int save_append(char* *buffer, const char *toAdd, int *bufLen);
159 
173 int assembleBuffer(int flag,
174  int nDbl, int nInt, int nBoo,
175  double curSimTim,
176  double dblVal[], int intVal[], int booVal[],
177  char* *buffer, int *bufLen);
178 
188 int getIntCheckError(const char *nptr, char **endptr, const int base,
189  int* val);
190 
199 int getDoubleCheckError(const char *nptr, char **endptr,
200  double* val);
201 
202 
216 int disassembleHeaderBuffer(const char* buffer,
217  char **endptr, const int base,
218  int *fla, int *nDbl, int *nInt, int *nBoo);
219 
232 int disassembleBuffer(const char* buffer,
233  int *fla,
234  int *nDbl, int *nInt, int *nBoo,
235  double *curSimTim,
236  double dblVal[], int intVal[], int booVal[]);
237 
244 int getsocketportnumber(const char *const docname);
245 
253 int getsockethost(const char *const docname, char *const hostname);
254 
264 
272 int establishclientsocket(const char *const docname);
273 
289 int writetosocket(const int *sockfd,
290  const int *flaWri,
291  const int *nDblWri, const int *nIntWri, const int *nBooWri,
292  double *curSimTim,
293  double dblValWri[], int intValWri[], int booValWri[]);
294 
295 
310 int sendclientmessage(const int *sockfd, const int *flaWri);
311 
322 int sendclienterror(const int *sockfd, const int *flaWri){
323  return sendclientmessage(sockfd, flaWri);
324 }
325 
333 int getRequiredReadBufferLength(const int *sockfd);
334 
341 int getrequiredbufferlength(const int nDbl, const int nInt, const int nBoo);
342 
345 //
358 int readfromsocket(const int *sockfd, int *flaRea,
359  int *nDblRea, int *nIntRea, int *nBooRea,
360  double *curSimTim,
361  double dblValRea[], int intValRea[], int booValRea[]);
362 
372 int readbufferfromsocket(const int *sockfd,
373  char *buffer, int *bufLen);
374 
398 int exchangewithsocket(const int *sockfd,
399  const int *flaWri, int *flaRea,
400  const int *nDblWri, const int *nIntWri, const int *nBooWri,
401  int *nDblRea, int *nIntRea, int *nBooRea,
402  double *simTimWri,
403  double dblValWri[], int intValWri[], int booValWri[],
404  double *simTimRea,
405  double dblValRea[], int intValRea[], int booValRea[]);
406 
422 int exchangedoubleswithsocket(const int *sockfd,
423  const int *flaWri, int *flaRea,
424  const int *nDblWri,
425  int *nDblRea,
426  double *simTimWri,
427  double dblValWri[],
428  double *simTimRea,
429  double dblValRea[]);
430 
449 int exchangedoubleswithsocketFMU(const int *sockfd,
450  const int *flaWri, int *flaRea,
451  const int *nDblWri,
452  int *nDblRea,
453  double *simTimWri,
454  double dblValWri[],
455  double *simTimRea,
456  double dblValRea[],
457  const int *flaExport);
458 
459 
465 int closeipc(int* sockfd);
466 
467 #endif /* _UTILSOCKET_H_ */
468 
function sockfd
int assembleBuffer(int flag, int nDbl, int nInt, int nBoo, double curSimTim, double dblVal[], int intVal[], int booVal[], char **buffer, int *bufLen)
Assembles the buffer that will be exchanged through the IPC.
int disassembleHeaderBuffer(const char *buffer, char **endptr, const int base, int *fla, int *nDbl, int *nInt, int *nBoo)
Disassembles the header of the buffer that has been received through the IPC.
int getrequiredbufferlength(const int nDbl, const int nInt, const int nBoo)
Returns the required socket buffer length.
int REQUIRED_WRITE_LENGTH
int readbufferfromsocket(const int *sockfd, char *buffer, int *bufLen)
Reads a character buffer from the socket.
int REQUIRED_READ_LENGTH
int exchangedoubleswithsocket(const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, int *nDblRea, double *simTimWri, double dblValWri[], double *simTimRea, double dblValRea[])
Exchanges data with the socket.
int save_append(char **buffer, const char *toAdd, int *bufLen)
Appends a character array to another character array.
int exchangewithsocket(const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, const int *nIntWri, const int *nBooWri, int *nDblRea, int *nIntRea, int *nBooRea, double *simTimWri, double dblValWri[], int intValWri[], int booValWri[], double *simTimRea, double dblValRea[], int intValRea[], int booValRea[])
Exchanges data with the socket.
int closeipc(int *sockfd)
Closes the inter process communication socket.
int SERVER_VERSION
This will be overwritten to contain the version number of the server.
int disassembleBuffer(const char *buffer, int *fla, int *nDbl, int *nInt, int *nBoo, double *curSimTim, double dblVal[], int intVal[], int booVal[])
Disassembles the buffer that has been received through the IPC.
FILE * f1
Define statements used by the BCVTB C methods.
int writetosocket(const int *sockfd, const int *flaWri, const int *nDblWri, const int *nIntWri, const int *nBooWri, double *curSimTim, double dblValWri[], int intValWri[], int booValWri[])
Writes data to the socket.
end Get return values from pointers flaRea
int sendclienterror(const int *sockfd, const int *flaWri)
Writes an error flag to the socket stream.
int getIntCheckError(const char *nptr, char **endptr, const int base, int *val)
Gets an integer and does the required error checking.
int sendclientmessage(const int *sockfd, const int *flaWri)
Writes a message flag to the socket stream.
int getsockethost(const char *const docname, char *const hostname)
Gets the hostname for the BSD socket communication.
int getsocketportnumber(const char *const docname)
Gets the port number for the BSD socket communication.
int getDoubleCheckError(const char *nptr, char **endptr, double *val)
Gets a double and does the required error checking.
int readfromsocket(const int *sockfd, int *flaRea, int *nDblRea, int *nIntRea, int *nBooRea, double *curSimTim, double dblValRea[], int intValRea[], int booValRea[])
Reads data from the socket.
int exchangedoubleswithsocketFMU(const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, int *nDblRea, double *simTimWri, double dblValWri[], double *simTimRea, double dblValRea[], const int *flaExport)
Exchanges data with the socket.
int getmainversionnumber()
Returns the main version number of the client.
int getRequiredReadBufferLength(const int *sockfd)
Returns the required socket buffer length by reading from the socket how many data it contains...
int establishclientsocket(const char *const docname)
Establishes a connection to the socket.