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

nasa / trick / 15004044467

13 May 2025 06:29PM UTC coverage: 55.958% (+0.03%) from 55.933%
15004044467

Pull #1877

github

web-flow
Merge d70b5de90 into ff58ed109
Pull Request #1877: Resolved comments being parsed inside strings

12332 of 22038 relevant lines covered (55.96%)

264644.69 hits per line

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

21.23
/trick_source/sim_services/VariableServer/VariableServerSession_commands.cpp
1

2
#include <string.h>
3
#include <iostream>
4
#include <sstream>
5
#include <stdlib.h>
6
#include <udunits2.h>
7
#include "trick/VariableServerSession.hh"
8
#include "trick/variable_server_message_types.h"
9
#include "trick/memorymanager_c_intf.h"
10
#include "trick/exec_proto.h"
11
#include "trick/command_line_protos.h"
12
#include "trick/message_proto.h"
13
#include "trick/message_type.h"
14
#include "trick/TrickConstant.hh"
15
#include "trick/sie_c_intf.h"
16
#include "trick/UdUnits.hh"
17
#include "trick/map_trick_units_to_udunits.hh"
18

19

20
int Trick::VariableServerSession::var_add(std::string in_name) {
3✔
21
    VariableReference * new_var;
22
    if (in_name == "time") {
3✔
23
        new_var = new VariableReference(in_name, &_time);
×
24
    } else {
25
        new_var = new VariableReference(in_name);
3✔
26
    }
27

28
    _session_variables.push_back(new_var) ;
3✔
29

30
    return(0) ;
3✔
31
}
32

33
int Trick::VariableServerSession::var_add(std::string var_name, std::string units_name) {
×
34
    var_add(var_name) ;
×
35
    var_units(var_name, units_name) ;
×
36
    return(0) ;
×
37
}
38

39
// Helper function for var_send_once
40
std::vector<std::string> split (const std::string& str, const char delim) {
×
41
    std::stringstream ss(str);
×
42
    std::string s;
×
43
    std::vector<std::string> ret;
×
44
    while (std::getline(ss, s, delim)) {
×
45
        ret.push_back(s);
×
46
    }
47
    return ret;
×
48
}
49

50
int Trick::VariableServerSession::var_send_once(std::string in_name, int num_vars) {
×
51
    std::vector<std::string> var_names = split(in_name, ',');
×
52

53
    if (var_names.size() != num_vars) {
×
54
        message_publish(MSG_ERROR, "Number of variables sent to var_send_once (%d) does not match num_vars (%d).\n", var_names.size(), num_vars);
×
55
        return -1;
×
56
    }
57

58
    std::vector<VariableReference *> given_vars;
×
59
    for (auto& varName : var_names) {
×
60
        VariableReference * new_var;
61
        if (varName == "time") {
×
62
            new_var = new VariableReference(varName, &_time);
×
63
        } else {
64
            new_var = new VariableReference(varName);
×
65
        }
66
        given_vars.push_back(new_var);
×
67
    }
68
    copy_sim_data(given_vars, false);
×
69
    write_data(given_vars, VS_SEND_ONCE);
×
70

71
    return(0) ;
×
72
}
73

74

75
int Trick::VariableServerSession::var_remove(std::string in_name) {
×
76

77
    for (unsigned int ii = 0 ; ii < _session_variables.size() ; ii++ ) {
×
78
        std::string var_name = _session_variables[ii]->getName();
×
79
        if ( ! var_name.compare(in_name) ) {
×
80
            delete _session_variables[ii];
×
81
            _session_variables.erase(_session_variables.begin() + ii) ;
×
82
            break ;
×
83
        }
84
    }
85

86
    return(0) ;
×
87

88
}
89

90
int Trick::VariableServerSession::var_units(std::string var_name, std::string units_name) {    
×
91
    VariableReference * variable = find_session_variable(var_name);
×
92

93
    if (variable == NULL) {
×
94
        // TODO: ERROR LOGGER HERE
95
        return -1;
×
96
    }
97

98
    return variable->setRequestedUnits(units_name);
×
99
}
100

101
int Trick::VariableServerSession::var_exists(std::string in_name) {
×
102
    char buf1[5] ;
103
    bool error = false ;
×
104

105
    unsigned int msg_type ;
106
    REF2* var_ref = ref_attributes(in_name.c_str());
×
107

108
    if ( var_ref == (REF2*)NULL ) {
×
109
        error = true;
×
110
    }
111

112
    if (_binary_data) {
×
113
        /* send binary 1 or 0 */
114
        msg_type = VS_VAR_EXISTS ;
×
115
        memcpy(buf1, &msg_type , sizeof(msg_type)) ;
×
116

117
        buf1[4] = (error==false);
×
118

119
        if (_debug >= 2) {
×
120
            message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending 1 binary byte\n", _connection, _connection->getClientTag().c_str());
×
121
        }
122

123
        _connection->write(buf1, 5);
×
124
    } else {
125
        /* send ascii "1" or "0" */
126
        sprintf(buf1, "%d\t%d\n", VS_VAR_EXISTS, (error==false));
×
127
        if (_debug >= 2) {
×
128
            message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending:\n%s\n", _connection, _connection->getClientTag().c_str(), buf1) ;
×
129
        }
130
        std::string write_string(buf1);
×
131
        if (write_string.length() != strlen(buf1)) {
×
132
            std::cout << "PROBLEM WITH STRING LENGTH: VAR_EXISTS ASCII" << std::endl;
×
133
        }
134
        _connection->write(write_string);
×
135
    }
136

137
    return(0) ;
×
138
}
139

140
int Trick::VariableServerSession::var_clear() {
×
141

142
    while( !_session_variables.empty() ) {
×
143
        delete _session_variables.back();
×
144
        _session_variables.pop_back();
×
145
    }
146

147
    return(0) ;
×
148
}
149

150
int Trick::VariableServerSession::var_send() {
×
151
    copy_sim_data();
×
152
    write_data();
×
153
    return(0) ;
×
154
}
155

156
int Trick::VariableServerSession::var_cycle(double in_rate) {
×
157
    _update_rate = in_rate ;
×
158
    _cycle_tics = (long long)(_update_rate * exec_get_time_tic_value()) ;
×
159
    return(0) ;
×
160
}
161

162
int Trick::VariableServerSession::var_exit() {
×
163
    _exit_cmd = true ;
×
164
    return(0) ;
×
165
}
166

167
int Trick::VariableServerSession::var_validate_address(bool on_off) {
×
168
    _validate_address = on_off ;
×
169
    return(0) ;
×
170
}
171

172
int Trick::VariableServerSession::var_debug(int level) {
1✔
173
    _debug = level ;
1✔
174
    return(0) ;
1✔
175
}
176

177
int Trick::VariableServerSession::var_ascii() {
1✔
178
    _binary_data = 0 ;
1✔
179
    return(0) ;
1✔
180
}
181

182
int Trick::VariableServerSession::var_binary() {
2✔
183
    _binary_data = 1 ;
2✔
184
    return(0) ;
2✔
185
}
186

187
int Trick::VariableServerSession::var_binary_nonames() {
×
188
    _binary_data = 1 ;
×
189
    _binary_data_nonames = 1 ;
×
190
    return(0) ;
×
191
}
192

193
int Trick::VariableServerSession::var_set_copy_mode(int mode) {
3✔
194
    if ( mode >= VS_COPY_ASYNC and mode <= VS_COPY_TOP_OF_FRAME ) {
3✔
195
        _copy_mode = (VS_COPY_MODE)mode ;
3✔
196
        if ( _copy_mode == VS_COPY_SCHEDULED ) {
3✔
197
            long long sim_time_tics ;
198
            sim_time_tics = exec_get_time_tics() ;
2✔
199
            // round the next call time to a multiple of the cycle
200
            sim_time_tics -= sim_time_tics % _cycle_tics ;
2✔
201
            _next_tics = sim_time_tics + _cycle_tics ;
2✔
202

203
            sim_time_tics = exec_get_freeze_time_tics() ;
2✔
204
            // round the next call time to a multiple of the cycle
205
            sim_time_tics -= sim_time_tics % _cycle_tics ;
2✔
206
            _freeze_next_tics = sim_time_tics + _cycle_tics ;
2✔
207

208
        } else {
209
            _next_tics = TRICK_MAX_LONG_LONG ;
1✔
210
        }
211
        return 0 ;
3✔
212
    }
213
    return -1 ;
×
214
}
215

216
int Trick::VariableServerSession::var_set_write_mode(int mode) {
3✔
217
    if ( mode >= VS_WRITE_ASYNC and mode <= VS_WRITE_WHEN_COPIED ) {
3✔
218
        _write_mode = (VS_WRITE_MODE)mode ;
3✔
219
        return 0 ;
3✔
220
    }
221
    return -1 ;
×
222
}
223

224
int Trick::VariableServerSession::var_sync(int mode) {
3✔
225

226
    switch (mode) {
3✔
227
        case 1:
1✔
228
            var_set_copy_mode(VS_COPY_SCHEDULED) ;
1✔
229
            var_set_write_mode(VS_WRITE_ASYNC) ;
1✔
230
            break ;
1✔
231
        case 2:
1✔
232
            var_set_copy_mode(VS_COPY_SCHEDULED) ;
1✔
233
            var_set_write_mode(VS_WRITE_WHEN_COPIED) ;
1✔
234
            break ;
1✔
235
        case 0:
1✔
236
        default:
237
            var_set_copy_mode(VS_COPY_ASYNC) ;
1✔
238
            var_set_write_mode(VS_WRITE_ASYNC) ;
1✔
239
            break ;
1✔
240
    }
241

242
    return 0 ;
3✔
243
}
244

245
int Trick::VariableServerSession::var_set_frame_multiple(unsigned int mult) {
×
246
    _frame_multiple = mult ;
×
247
    return 0 ;
×
248
}
249

250
int Trick::VariableServerSession::var_set_frame_offset(unsigned int offset) {
×
251
    _frame_offset = offset ;
×
252
    return 0 ;
×
253
}
254

255
int Trick::VariableServerSession::var_set_freeze_frame_multiple(unsigned int mult) {
×
256
    _freeze_frame_multiple = mult ;
×
257
    return 0 ;
×
258
}
259

260
int Trick::VariableServerSession::var_set_freeze_frame_offset(unsigned int offset) {
×
261
    _freeze_frame_offset = offset ;
×
262
    return 0 ;
×
263
}
264

265
int Trick::VariableServerSession::var_byteswap(bool on_off) {
×
266
    _byteswap = on_off ;
×
267
    return(0) ;
×
268
}
269

270
bool Trick::VariableServerSession::get_send_stdio() {
×
271
    return _send_stdio ;
×
272
}
273

274
int Trick::VariableServerSession::set_send_stdio(bool on_off) {
×
275
    _send_stdio = on_off ;
×
276
    return(0) ;
×
277
}
278

279
int Trick::VariableServerSession::send_list_size() {
×
280
    
281
    unsigned int msg_type = VS_LIST_SIZE;
×
282
    int var_count = _session_variables.size();
×
283

284
    // send number of variables
285
    if (_binary_data) {
×
286
        // send in the binary message header format:
287
        // <message_indicator><message_size><number_of_variables>
288
        char buf1[12] ;
289

290
        unsigned int msg_type = VS_LIST_SIZE;
×
291
        memcpy(buf1, &msg_type , sizeof(msg_type)) ;
×
292

293
        memset(&(buf1[4]), 0, sizeof(int)); // message size = 0
×
294
        memcpy(&(buf1[8]), &var_count, sizeof(var_count));
×
295

296
        if (_debug >= 2) {
×
297
            message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending %d event variables\n", _connection, _connection->getClientTag().c_str(), var_count);
×
298
        }
299

300
        _connection->write(buf1, sizeof (buf1));
×
301
    } else {
302
        std::stringstream write_string;
×
303
        write_string << VS_LIST_SIZE << "\t" << var_count << "\n";
×
304
        // ascii
305
        if (_debug >= 2) {
×
306
            message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending number of event variables:\n%s\n", _connection, _connection->getClientTag().c_str(), write_string.str().c_str()) ;
×
307
        }
308

309
        _connection->write(write_string.str());
×
310
    }
311

312
    return 0 ;
×
313
}
314

315
int Trick::VariableServerSession::transmit_file(std::string filename) {
×
316
    const unsigned int packet_size = 4095 ;
×
317
    FILE * fp ;
318
    unsigned int file_size ;
319
    unsigned int current_size = 0 ;
×
320
    unsigned int bytes_read ;
321
    char buffer[packet_size+1] ;
322
    int ret ;
323

324
    if (_debug >= 2) {
×
325
        message_publish(MSG_DEBUG,"%p tag=<%s> var_server opening %s.\n", _connection, _connection->getClientTag().c_str(), filename.c_str()) ;
×
326
    }
327

328
    if ((fp = fopen(filename.c_str() , "r")) == NULL ) {
×
329
        message_publish(MSG_ERROR,"Variable Server Error: Cannot open %s.\n", filename.c_str()) ;
×
330
        sprintf(buffer, "%d\t-1\n", VS_SIE_RESOURCE) ;
×
331
        std::string message(buffer);
×
332
        _connection->write(message);
×
333
        return(-1) ;
×
334
    }
335

336
    fseek(fp , 0L, SEEK_END) ;
×
337
    file_size = ftell(fp) ;
×
338

339
    sprintf(buffer, "%d\t%u\n\0" , VS_SIE_RESOURCE, file_size) ;
×
340
    std::string message(buffer);
×
341
    _connection->write(message);
×
342
    rewind(fp) ;
×
343

344
    // Switch to blocking writes since this could be a large transfer.
345
    if (_connection->setBlockMode(true)) {
×
346
        message_publish(MSG_DEBUG,"Variable Server Error: Failed to set socket to blocking mode.\n");
×
347
    }
348

349
    while ( current_size < file_size ) {
×
350
        bytes_read = fread(buffer , 1 , packet_size , fp) ;
×
351
        message = std::string(buffer);
×
352
        message.resize(bytes_read);
×
353
        ret = _connection->write(message);
×
354
        if (ret != (int)bytes_read) {
×
355
            message_publish(MSG_ERROR,"Variable Server Error: Failed to send file. Bytes read: %d Bytes sent: %d\n", bytes_read, ret) ;
×
356
            return(-1);
×
357
        }
358
        current_size += bytes_read ;
×
359
    }
360

361
    // Switch back to non-blocking writes.
362
    if (_connection->setBlockMode(false)) {
×
363
        message_publish(MSG_DEBUG,"Variable Server Error: Failed to set socket to non-blocking mode.\n");
×
364
        return(-1);
×
365
    }
366

367
    return(0) ;
×
368
}
369

370
int Trick::VariableServerSession::send_file(std::string file_name) {
×
371
    return transmit_file(file_name) ;
×
372
}
373

374
int Trick::VariableServerSession::send_sie_resource() {
×
375
    sie_append_runtime_objs() ;
×
376
    //return transmit_file(std::string(command_line_args_get_default_dir()) + "/S_sie.resource") ;
377
    // Use the runtime sie dir instead of the default dir as sie_append_runtime_objs() 
378
    // may have moved the sie resource file and also always uses the runtime sie dir.
379
    return transmit_file(std::string(sie_get_runtime_sie_dir()) + "/S_sie.resource") ;
×
380
}
381

382
int Trick::VariableServerSession::send_sie_class() {
×
383
    sie_class_attr_map_print_xml() ;
×
384
    //return transmit_file(std::string(command_line_args_get_default_dir()) + "/" + "S_sie_class.xml") ;
385
    // Use the runtime sie dir instead of the default dir as sie_class_attr_map_print_xml()
386
    // always uses the runtime sie dir.
387
    return transmit_file(std::string(sie_get_runtime_sie_dir()) + "/" + "S_sie_class.xml") ;
×
388
}
389

390
int Trick::VariableServerSession::send_sie_enum() {
×
391
    sie_enum_attr_map_print_xml() ;
×
392
    //return transmit_file(std::string(command_line_args_get_default_dir()) + "/" + "S_sie_enum.xml") ;
393
    // Use the runtime sie dir instead of the default dir as sie_enum_attr_map_print_xml()
394
    // always uses the runtime sie dir.
395
    return transmit_file(std::string(sie_get_runtime_sie_dir()) + "/" + "S_sie_enum.xml") ;
×
396
}
397

398
int Trick::VariableServerSession::send_sie_top_level_objects() {
×
399
    sie_top_level_objects_print_xml() ;
×
400
    //return transmit_file(std::string(command_line_args_get_default_dir()) + "/" + "S_sie_top_level_objects.xml") ;
401
    // Use the runtime sie dir instead of the default dir as sie_top_level_objects_print_xml()
402
    // always uses the runtime sie dir.
403
    return transmit_file(std::string(sie_get_runtime_sie_dir()) + "/" + "S_sie_top_level_objects.xml") ;
×
404
}
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