• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 25459968639

06 May 2026 08:42PM UTC coverage: 55.916% (-0.8%) from 56.7%
25459968639

Pull #2011

github

web-flow
Merge f11412d5f into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14607 of 26123 relevant lines covered (55.92%)

466416.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/trick_source/sim_services/MasterSlave/MSSocket.cpp
1

2
#include <iostream>
3
#include <sstream>
4
#include <string.h>
5
#include <unistd.h>
6

7
#include "trick/MSSocket.hh"
8
#include "trick/Master.hh"
9
#include "trick/tc_proto.h"
10
#include "trick/exec_proto.h"
11
#include "trick/command_line_protos.h"
12

13
Trick::MSSocket::MSSocket() : tc_dev() {
×
14

15
    tc_dev.disabled = TC_COMM_FALSE ;
×
16
    tc_dev.error_handler = NULL ;
×
17
    tc_dev.blockio_type = TC_COMM_TIMED_BLOCKIO ;
×
18
    tc_dev.blockio_type = TC_COMM_BLOCKIO ;
×
19
    tc_dev.port = 0 ; // tc_multiconnect needs this so that it will send broadcast msg !
×
20

21
    // default is a non-zero sync wait limit; helpful when slave reading initial data from master
22
    sync_wait_limit = 5.0 ;
×
23
}
×
24

25
int Trick::MSSocket::set_sync_wait_limit(double in_limit) {
×
26
    /** @par Detailed Design */
27
    sync_wait_limit = in_limit ;
×
28
    if ( in_limit > 0.0 ) {
×
29
        /** @li if the incoming limit time is greater than zero, set the socket
30
                to TC_COMM_TIMED_BLOCKIO with the time limit of in_limit */
31
        tc_blockio(&tc_dev, TC_COMM_TIMED_BLOCKIO);
×
32
        tc_set_blockio_timeout_limit(&tc_dev, sync_wait_limit);
×
33
    } else {
34
        /** @li if the incoming limit time is less than or equal to zero, set the socket
35
                to TC_COMM_BLOCKIO (infinite block) */
36
        tc_blockio(&tc_dev, TC_COMM_BLOCKIO);
×
37
    }
38
    return(0) ;
×
39
}
40

41
std::string Trick::MSSocket::add_sim_args( std::string slave_type ) {
×
42

43
    char master_host[80];
44
    std::stringstream temp_stream ;
×
45

46
    /** @par Detailed Design */
47

48
    /** @li create a unique identifier based on the <machine>_<current pid>. */
49
    gethostname(master_host, (size_t) 80);
×
50

51
    temp_stream << master_host << "_" << getpid() ;
×
52

53
    sync_port_tag = temp_stream.str() ;
×
54

55
    /** @li return the unique identifier with the "-p" argument that is
56
            specific to master/slave communications. */
57
    return("-p " + temp_stream.str()) ;
×
58
}
×
59

60
int Trick::MSSocket::process_sim_args() {
×
61

62
    int ii ;
63
    int argc ;
64
    char ** argv ;
65

66
    argc = command_line_args_get_argc() ;
×
67
    argv = command_line_args_get_argv() ;
×
68

69
    /** @par Detailed Design */
70

71
    /** @li search for the "-p" argument.  If found get the master identifier as the next argument */
72
    for (ii = 1; ii < argc; ii++) {
×
73
        if (!strncmp("-p", argv[ii], (size_t) 2)) {
×
74
            if (argc >= ii + 1) {
×
75
                sync_port_tag = argv[ii+1] ;
×
76
                return(1) ;
×
77
            }
78
        }
79
    }
80

81
    return(0) ;
×
82
}
83

84
int Trick::MSSocket::accept() {
×
85

86
    int ret ;
87
    /** @par Detailed Design */
88
    /** @li Call tc_multiconnect to connect this master.  Use "master" as the my_tag argument */
89
    ret = tc_multiconnect(&tc_dev, (char *)"master", (char *)sync_port_tag.c_str(), NULL);
×
90

91
    return(ret) ;
×
92
}
93

94
int Trick::MSSocket::connect() {
×
95
    int ret ;
96
    /** @par Detailed Design */
97
    /** @li Call tc_multiconnect to connect this slave.  Use "slave" as the my_tag argument */
98
    ret = tc_multiconnect(&tc_dev, (char *)"slave", (char *)sync_port_tag.c_str(), NULL);
×
99

100
    return(ret) ;
×
101
}
102

103
int Trick::MSSocket::disconnect() {
×
104
    int ret;
105
    /** @par Detailed Design */
106
    /** @li Call tc_disconnect to disconnect this device. */
107
    ret = tc_disconnect( &tc_dev );
×
108

109
    tc_dev.disabled = TC_COMM_FALSE ;
×
110
    tc_dev.error_handler = NULL ;
×
111
    tc_dev.blockio_type = TC_COMM_TIMED_BLOCKIO ;
×
112
    tc_dev.blockio_type = TC_COMM_BLOCKIO ;
×
113
    tc_dev.port = 0 ;
×
114

115
    return(ret);
×
116
}
117

118
long long Trick::MSSocket::read_time() {
×
119

120
    long long in_time = 0 ;
×
121
    int ret ;
122

123
    /** @par Detailed Design */
124
    /** @li Call tc_read to get the time */
125
    ret = tc_read(&tc_dev , (char *)&in_time, sizeof(long long));
×
126

127
    /** @li If the read call returned the correct number of bytes return the read in time */
128
    if ( ret == sizeof(long long)) {
×
129
        return(in_time) ;
×
130
    }
131
    /** @li Else return the special "error" time */
132
    return (MS_ERROR_TIME) ;
×
133
}
134

135
MS_SIM_COMMAND Trick::MSSocket::read_command() {
×
136

137
    MS_SIM_COMMAND command ;
138
    int ret ;
139

140
    /** @par Detailed Design */
141
    /** @li Call tc_read to get the command */
142
    ret = tc_read(&tc_dev , (char *)&command, sizeof(MS_SIM_COMMAND));
×
143

144
    /** @li If the read call returned the correct number of bytes return the read in command */
145
    if ( ret == sizeof(MS_SIM_COMMAND)) {
×
146
        return(command) ;
×
147
    }
148

149
    /** @li Else return ErrorCmd  */
150
    return(MS_ErrorCmd) ;
×
151
}
152

153
int Trick::MSSocket::read_port() {
×
154

155
    int in_port = 0 ;
×
156
    int ret ;
157

158
    /** @par Detailed Design */
159
    /** @li Call tc_read to get the port number */
160
    ret = tc_read(&tc_dev , (char *)&in_port, sizeof(int));
×
161

162
    /** @li If the read call returned the correct number of bytes return the read in port */
163
    if ( ret == sizeof(int)) {
×
164
        return(in_port) ;
×
165
    }
166
    /** @li Else return the special "error" port */
167
    return (MS_ERROR_PORT) ;
×
168
}
169

170
char Trick::MSSocket::read_name(char * read_data, size_t size) {
×
171

172
    int ret ;
173

174
    /** @par Detailed Design */
175
    /** @li Call tc_read to get the name (character array) */
176
    ret = tc_read(&tc_dev , (char *)read_data, (int)size);
×
177

178
    /** @li If the read call returned the correct number of bytes return 1st character read */
179
    if ( ret == int(size)) {
×
180
        return(read_data[0]) ;
×
181
    }
182
    /** @li Else return the special "error" name character */
183
    return (MS_ERROR_NAME) ;
×
184
}
185

186
int Trick::MSSocket::write_time(long long in_time) {
×
187

188
    int ret ;
189

190
    /** @par Detailed Design */
191
    /** @li Call tc_write to write the time */
192
    ret = tc_write(&tc_dev , (char *)&in_time, sizeof(long long));
×
193

194
    /** @li Return the number of bytes written */
195
    return(ret) ;
×
196
}
197

198
int Trick::MSSocket::write_command(MS_SIM_COMMAND command) {
×
199

200
    int ret ;
201

202
    /** @par Detailed Design */
203
    /** @li Call tc_write to write the command */
204
    ret = tc_write(&tc_dev , (char *)&command, sizeof(unsigned int));
×
205

206
    /** @li Return the number of bytes written */
207
    return(ret) ;
×
208
}
209

210
int Trick::MSSocket::write_port(int in_port) {
×
211

212
    int ret ;
213

214
    /** @par Detailed Design */
215
    /** @li Call tc_write to write the port number */
216
    ret = tc_write(&tc_dev , (char *)&in_port, sizeof(int));
×
217

218
    /** @li Return the number of bytes written */
219
    return(ret) ;
×
220
}
221

222
int Trick::MSSocket::write_name(char * in_data, size_t size) {
×
223

224
    /** @par Detailed Design */
225
    /** @li Call tc_write to write the name (character array) */
226
    tc_write(&tc_dev , (char *)in_data, size);
×
227

228
    /** @li Return the number of bytes written */
229
    return(size) ;
×
230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc