-
Notifications
You must be signed in to change notification settings - Fork 12
/
info.go
201 lines (160 loc) · 6.22 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*****************************************************************************
* (c) 2020 Copyright, Real-Time Innovations. All rights reserved. *
* *
* No duplications, whole or partial, manual or electronic, may be made *
* without express written permission. Any such copies, or revisions thereof,*
* must display this notice unaltered. *
* This code contains trade secrets of Real-Time Innovations, Inc. *
* *
*****************************************************************************/
// Package rti implements functions of RTI Connector for Connext DDS in Go
package rti
// #cgo windows CFLAGS: -I${SRCDIR}/include -I${SRCDIR}/rticonnextdds-connector/include -DRTI_WIN32 -DNDDS_DLL_VARIABLE
// #cgo linux,arm CFLAGS: -I${SRCDIR}/include -I${SRCDIR}/rticonnextdds-connector/include -DRTI_UNIX -DRTI_LINUX
// #cgo windows LDFLAGS: -L${SRCDIR}/rticonnextdds-connector/lib/x64Win64VS2013 -lrtiddsconnector
// #cgo linux,arm LDFLAGS: -L${SRCDIR}/rticonnextdds-connector/lib/armv6vfphLinux3.xgcc4.7.2 -lrtiddsconnector -ldl -lnsl -lm -lpthread -lrt
// #include "rticonnextdds-connector.h"
// #include <stdlib.h>
import "C"
import (
"encoding/json"
"errors"
"strconv"
"unsafe"
)
/********
* Types *
*********/
// Infos is a sequence of info samples used by an input to read DDS meta data
type Infos struct {
input *Input
}
// Identity is the structure for identifying
type Identity struct {
WriterGUID [16]byte `json:"writer_guid"`
SequenceNumber int `json:"sequence_number"`
}
/*******************
* Public Functions *
*******************/
// IsValid is a function to check validity of the element and return a boolean
func (infos *Infos) IsValid(index int) (bool, error) {
memberNameCStr := C.CString("valid_data")
defer C.free(unsafe.Pointer(memberNameCStr))
var retVal C.int
retcode := int(C.RTI_Connector_get_boolean_from_infos(unsafe.Pointer(infos.input.connector.native), &retVal, infos.input.nameCStr, C.int(index+1), memberNameCStr))
err := checkRetcode(retcode)
return (retVal != 0), err
}
// GetSourceTimestamp is a function to get the source timestamp of a sample
func (infos *Infos) GetSourceTimestamp(index int) (int64, error) {
tsStr, err := infos.getJSONMember(index, "source_timestamp")
if err != nil {
return 0, err
}
ts, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil {
return 0, err
}
return ts, nil
}
// GetReceptionTimestamp is a function to get the reception timestamp of a sample
func (infos *Infos) GetReceptionTimestamp(index int) (int64, error) {
tsStr, err := infos.getJSONMember(index, "reception_timestamp")
if err != nil {
return 0, err
}
ts, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil {
return 0, err
}
return ts, nil
}
// GetIdentity is a function to get the identity of a writer that sent the sample
func (infos *Infos) GetIdentity(index int) (Identity, error) {
var writerID Identity
identityStr, err := infos.getJSONMember(index, "sample_identity")
if err != nil {
return writerID, err
}
jsonByte := []byte(identityStr)
err = json.Unmarshal(jsonByte, &writerID)
if err != nil {
return writerID, errors.New("JSON Unmarshal failed: " + err.Error())
}
return writerID, nil
}
// GetIdentityJSON is a function to get the identity of a writer in JSON
func (infos *Infos) GetIdentityJSON(index int) (string, error) {
identityStr, err := infos.getJSONMember(index, "sample_identity")
if err != nil {
return "", err
}
return identityStr, nil
}
// GetRelatedIdentity is a function used for request-reply communications.
func (infos *Infos) GetRelatedIdentity(index int) (Identity, error) {
var writerID Identity
identityStr, err := infos.getJSONMember(index, "related_sample_identity")
if err != nil {
return writerID, err
}
jsonByte := []byte(identityStr)
err = json.Unmarshal(jsonByte, &writerID)
if err != nil {
return writerID, errors.New("JSON Unmarshal failed: " + err.Error())
}
return writerID, nil
}
// GetRelatedIdentityJSON is a function used for get related identity in JSON.
func (infos *Infos) GetRelatedIdentityJSON(index int) (string, error) {
identityStr, err := infos.getJSONMember(index, "related_sample_identity")
if err != nil {
return "", err
}
return identityStr, nil
}
// GetViewState is a function used to get a view state in string (either "NEW" or "NOT NEW").
func (infos *Infos) GetViewState(index int) (string, error) {
viewStateStr, err := infos.getJSONMember(index, "view_state")
if err != nil {
return "", err
}
return viewStateStr, nil
}
// GetInstanceState is a function used to get a instance state in string (one of "ALIVE", "NOT_ALIVE_DISPOSED" or "NOT_ALIVE_NO_WRITERS").
func (infos *Infos) GetInstanceState(index int) (string, error) {
instanceStateStr, err := infos.getJSONMember(index, "instance_state")
if err != nil {
return "", err
}
return instanceStateStr, nil
}
// GetSampleState is a function used to get a sample state in string (either "READ" or "NOT_READ").
func (infos *Infos) GetSampleState(index int) (string, error) {
sampleStateStr, err := infos.getJSONMember(index, "sample_state")
if err != nil {
return "", err
}
return sampleStateStr, nil
}
// GetLength is a function to return the length of the
func (infos *Infos) GetLength() (int, error) {
var retVal C.double
retcode := int(C.RTI_Connector_get_sample_count(unsafe.Pointer(infos.input.connector.native), infos.input.nameCStr, &retVal))
err := checkRetcode(retcode)
return int(retVal), err
}
func (infos *Infos) getJSONMember(index int, memberName string) (string, error) {
memberNameCStr := C.CString(memberName)
defer C.free(unsafe.Pointer(memberNameCStr))
var retValCStr *C.char
retcode := int(C.RTI_Connector_get_json_from_infos(unsafe.Pointer(infos.input.connector.native), infos.input.nameCStr, C.int(index+1), memberNameCStr, &retValCStr))
err := checkRetcode(retcode)
if err != nil {
return "", err
}
retValGoStr := C.GoString(retValCStr)
C.RTI_Connector_free_string(retValCStr)
return retValGoStr, nil
}