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

nasa / trick / 25456501308

06 May 2026 07:29PM UTC coverage: 55.935% (-0.8%) from 56.7%
25456501308

Pull #2011

github

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

14612 of 26123 relevant lines covered (55.94%)

462107.16 hits per line

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

76.74
/trick_source/sim_services/Message/MessageTCDevice.cpp
1

2
#include <string.h>
3
#include <stdlib.h>
4

5
#include "trick/MessageTCDevice.hh"
6
#include "trick/message_type.h"
7
#include "trick/montecarlo_c_intf.h"
8
#include "trick/tc_proto.h"
9

10
Trick::MessageTCDeviceListenThread::MessageTCDeviceListenThread(MessageTCDevice * in_mtcd) :
187✔
11
 Trick::SysThread("MessageListen"),
12
 mtcd(in_mtcd) ,
187✔
13
 listen_dev() {
374✔
14
    /* And a TCDevice for message server @e listen_device is configured. */
15
    listen_dev.disabled = TC_COMM_FALSE ;
187✔
16
    listen_dev.disable_handshaking = TC_COMM_TRUE ;
187✔
17
    listen_dev.blockio_limit = 0.0 ;
187✔
18
    listen_dev.blockio_type = TC_COMM_BLOCKIO ;
187✔
19
    listen_dev.client_id = 0 ;
187✔
20
    listen_dev.port = 0 ;
187✔
21
    strcpy(listen_dev.client_tag, "") ;
187✔
22
    listen_dev.error_handler = (TrickErrorHndlr *) calloc(1, (int)sizeof(TrickErrorHndlr));
187✔
23
    listen_dev.error_handler->report_level = TRICK_ERROR_CAUTION;
187✔
24
}
187✔
25

26
Trick::MessageTCDeviceListenThread::~MessageTCDeviceListenThread() {
187✔
27
    free(listen_dev.error_handler) ;
187✔
28
    listen_dev.error_handler = NULL;
187✔
29
    if ( listen_dev.hostname ) {
187✔
30
       free((char*)listen_dev.hostname) ;
187✔
31
    }
32
    close(listen_dev.socket) ;
187✔
33
}
187✔
34

35
int Trick::MessageTCDeviceListenThread::get_port() {
198✔
36
    return listen_dev.port ;
198✔
37
}
38

39
int Trick::MessageTCDeviceListenThread::init_listen_device() {
187✔
40
    return tc_init(&listen_dev);
187✔
41
}
42

43
int Trick::MessageTCDeviceListenThread::restart() {
11✔
44
    struct sockaddr_in s_in;
45
    int s_in_size =  sizeof(s_in) ;
11✔
46
    getsockname( listen_dev.socket , (struct sockaddr *)&s_in, (socklen_t *)&s_in_size) ;
11✔
47
    printf("restart message port = %d\n" , ntohs(s_in.sin_port)) ;
11✔
48
    return 0;
11✔
49
}
50

51
void * Trick::MessageTCDeviceListenThread::thread_body() {
156✔
52

53
        /** @par Design Details: */
54
    int status ;
55
    fd_set rfds;
56
    struct timeval timeout_time = { 2, 0 };
156✔
57
    TCDevice * new_connection ;
58

59
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
156✔
60

61
    /** @li If a proper port is found, make a new connection to the message server that is listening if possible.
62
     *      Once the connection is established, the new connection is added to the subscriber connection list.
63
     */
64
    while (1) {
65
        FD_ZERO(&rfds);
3,026✔
66
        FD_SET(listen_dev.socket, &rfds);
178✔
67
        timeout_time.tv_sec = 2 ;
178✔
68
        select(listen_dev.socket + 1, &rfds, NULL, NULL, &timeout_time);
178✔
69

70
        if (FD_ISSET(listen_dev.socket, &rfds)) {
22✔
71
            new_connection = new TCDevice() ;
×
72
            new_connection->disabled = TC_COMM_FALSE ;
×
73
            new_connection->disable_handshaking = TC_COMM_TRUE ;
×
74
            new_connection->blockio_limit = 0.0 ;
×
75
            new_connection->blockio_type = TC_COMM_BLOCKIO ;
×
76
            new_connection->client_id = 0 ;
×
77
            strcpy(new_connection->client_tag, "") ;
×
78
            new_connection->error_handler = (TrickErrorHndlr *) calloc(1, (int)sizeof(TrickErrorHndlr));
×
79
            new_connection->error_handler->report_level = TRICK_ERROR_CAUTION;
×
80

81
            status = tc_accept(&listen_dev, new_connection);
×
82

83
            if (status == TC_SUCCESS) {
×
84
                mtcd->add_connection(new_connection) ;
×
85
            } 
86
        }
87
    }
22✔
88

89
    return NULL ;
90

91
}
92

93
void Trick::MessageTCDeviceListenThread::dump( std::ostream & oss ) {
×
94
    oss << "Trick::MessageTCDeviceListenThread (" << name << ")\n";
×
95
    oss << "    port = " << get_port() << std::endl ;
×
96
    Trick::ThreadBase::dump(oss) ;
×
97
}
×
98

99
/* --------------------------------------------------------------------------- */
100

101
Trick::MessageTCDevice::MessageTCDevice() :
187✔
102
 listen_thread(this)
187✔
103
{
104
    combined_message = new char[20480] ;
187✔
105
    name = "tcdevice" ;
187✔
106
    return ;
187✔
107
}
×
108

109
// DESTRUCTOR
110
Trick::MessageTCDevice::~MessageTCDevice() {
187✔
111
    delete[] combined_message ;
187✔
112
}
187✔
113

114
Trick::MessageTCDeviceListenThread & Trick::MessageTCDevice::get_listen_thread() {
187✔
115
    return listen_thread ;
187✔
116
}
117

118
void Trick::MessageTCDevice::add_connection( TCDevice * new_conn ) {
×
119
    connections.push_back(new_conn) ;
×
120
}
×
121

122
int Trick::MessageTCDevice::shutdown() {
155✔
123
    listen_thread.cancel_thread() ;
155✔
124
    return 0 ;
155✔
125
}
126

127
int Trick::MessageTCDevice::default_data() {
187✔
128

129
    int ret ;
130

131
    if ( mc_is_slave() ) {
187✔
132
        enabled = false ;
×
133
    }
134
    /** @par Design Details: */
135
    /** @li Reserve a listen socket */
136
    if ( enabled ) {
187✔
137
        ret = listen_thread.init_listen_device() ;
187✔
138
        if ( ret == TC_SUCCESS ) {
187✔
139
            port = listen_thread.get_port() ;
187✔
140
        } else {
141
            fprintf(stderr, "ERROR: Could not create listen port for message server.  Aborting.\n");
×
142
            return (-1);
×
143
        }
144
    }
145

146
    return(0) ;
187✔
147
}
148

149
/**
150
@details
151
-# If this subscriber is enabled, creates a thread for a new socket connection
152
*/
153
int Trick::MessageTCDevice::init() {
155✔
154
    if ( enabled ) {
155✔
155
        listen_thread.create_thread() ;
155✔
156
    }
157
    return(0) ;
155✔
158
}
159

160
/**
161
@details
162
-# Call the listen thread restart routine
163
-# Get the port number from the listen thread
164
-# If the listen thread has not been created, call the listen thread create thread method
165
*/
166
int Trick::MessageTCDevice::restart() {
11✔
167
    if ( enabled ) {
11✔
168
        listen_thread.restart() ;
11✔
169
        port = listen_thread.get_port() ;
11✔
170
        if ( listen_thread.get_pthread_id() == 0 ) {
11✔
171
            listen_thread.create_thread() ;
1✔
172
        }
173
    }
174
    return 0 ;
11✔
175
}
176

177

178
// MEMBER FUNCTION
179
void Trick::MessageTCDevice::update( unsigned int level , std::string header , std::string message ) {
34,117✔
180

181
    std::vector < TCDevice *>::iterator it ;
34,117✔
182
    int nbytes ;
183
    int length ;
184
    std::string color_code ;
34,117✔
185

186
    /** @par Design Details: */
187

188
    switch (level) {
34,117✔
189
        case MSG_NORMAL :
2,351✔
190
            color_code = "\033[00m" ; // normal
2,351✔
191
            break ;
2,351✔
192
        case MSG_INFO :
83✔
193
            color_code = "\033[32m" ; // green
83✔
194
            break ;
83✔
195
        case MSG_WARNING :
28✔
196
            color_code = "\033[33m" ; // yellow
28✔
197
            break ;
28✔
198
        case MSG_ERROR :
34✔
199
            color_code = "\033[31m" ; // red
34✔
200
            break ;
34✔
201
        case MSG_DEBUG :
357✔
202
            color_code = "\033[36m" ; // cyan
357✔
203
            break ;
357✔
204
        default :
31,264✔
205
            color_code = "\033[00m" ; // normal
31,264✔
206
            break ;
31,264✔
207
    }
208

209
    /** @li If this subscriber is not enabled, do nothing. Otherwise, it gets the update. */
210
    if ( enabled && level < 100 ) {
34,117✔
211

212
             /** @li The message is composed as message header + message text */
213
        if ( color ) {
34,117✔
214
            snprintf( combined_message , 20480 , "%s%s%s\033[00m" , header.c_str() , color_code.c_str() , message.c_str()) ;
34,117✔
215
        } else {
216
            snprintf( combined_message , 20480 , "%s%s" , header.c_str() , message.c_str()) ;
×
217
        }
218
        length = (int)strlen(combined_message) ;
34,117✔
219

220
        /** @li The message is broadcast to all socket connection this subscriber has. */
221
        it = connections.begin() ;
34,117✔
222
        while ( it != connections.end() ) {
34,117✔
223
            nbytes = tc_write( *it, combined_message, length) ;
×
224

225
            /** @li All those connections that can not receive the message get disconnected.  */
226
            if ( nbytes != length ) {
×
227
                tc_disconnect(*it) ;
×
228
                it = connections.erase(it) ;
×
229
            } else {
230
                it++ ;
×
231
            }
232
        }
233
    }
234

235
    return ;
68,234✔
236
}
34,117✔
237

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