-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiamondapparatus.h
175 lines (134 loc) · 4.12 KB
/
diamondapparatus.h
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
/**
* @file diamondapparatus.h
* @brief Message format and API
*
*/
#ifndef __DIAMONDAPPARATUS_H
#define __DIAMONDAPPARATUS_H
#include <errno.h>
#include <string.h>
#include <stdio.h>
////////////// wait flags for get()
// never wait
#define GET_WAITNONE 0
// wait for NEW data
#define GET_WAITNEW 1
// wait for ANY data
#define GET_WAITANY 2
/////////////// topic states
#define TOPIC_NODATA 0
#define TOPIC_UNCHANGED 1
#define TOPIC_CHANGED 2
#define TOPIC_NOTFOUND 3
#define TOPIC_NOTCONNECTED 4
/////////////// data types
#define DT_FLOAT 0
#define DT_STRING 1
#define VERSION "1.1.2"
#define VERSIONNAME "YELLOW ACKNOWLEDGEMENT"
#ifdef __cplusplus
#include "data.h"
namespace diamondapparatus {
struct DiamondException {
int err;
char str[256];
DiamondException(const char *s){
strncpy(str,s,256);str[255]=0;
err=errno;
}
DiamondException(){
str[0]=0;
err=-1;
}
DiamondException(const DiamondException& e){
strcpy(str,e.str);
err = e.err;
}
const char *what(){
static char buf[256];
if(err)
sprintf(buf,"%s: %s",str,strerror(err));
else
sprintf(buf,"%s",str);
return buf;
}
};
// start the server and never exit. Don't run this
// unless you really know what you're up do; typically
// the diamond app will do this. The port is either
// DEFAULT_PORT in tcp.h, or DIAMOND_PORT in the environment
// if it is set.
void server();
// client calls
/// initialise the system and connect to the server. The host
/// and port to connect to are the environment variables
/// DIAMOND_HOST and DIAMOND_PORT, or localhost and DEFAULT_PORT
/// (see tcp.h) if these are not set.
void init();
/// close down the client politely - returns >0 if we haven't
/// yet done the same number of destroys as inits, and 0 once
/// we have the same number and the actual shutdown was done.
/// Also returns zero if we do too many destroys!
int destroy();
/// subscribe to a topic - we will receive msgs from the server
/// when it changes. Calling gettopic will get the latest value.
void subscribe(const char *n);
/// publish a topic
void publish(const char *name,Topic& d);
/// get the a copy of a topic as it currently is.
Topic get(const char *n,int wait=GET_WAITNONE);
/// wait for a message on any topic we are subscribed to
void waitForAny();
/// returns false when the client loop has quit (i.e. the server
/// has died)
bool isRunning();
/// kill the server
void killServer();
/// destroy all data on the server
void clearServer();
}
#endif
// now the C wrappers
#ifdef __cplusplus
extern "C" {
#endif
// return error if a function returned -1
const char *diamondapparatus_error();
// 0 if OK, -1 on error
int diamondapparatus_init();
// 0 if OK, -1 on error
int diamondapparatus_server();
// will return -1 in error, >0 if not last destroy, and 0 if did actually
// shutdown
int diamondapparatus_isrunning();
int diamondapparatus_destroy();
int diamondapparatus_killserver();
int diamondapparatus_clearserver();
/// publish the topic built up by diamondapparatus_add..()
int diamondapparatus_publish(const char *n);
/// clear the topic to publish
void diamondapparatus_newtopic();
/// add a float to the topic to publish
void diamondapparatus_addfloat(float f);
/// add a string to the topic to publish
void diamondapparatus_addstring(const char *s);
int diamondapparatus_subscribe(const char *n);
/// read a topic, returning its state or -1. The topic can be accessed
/// with diamondapparatus_fetch...
int diamondapparatus_get(const char *n,int wait);
/// is the last topic got a valid topic to fetch data from?
int diamondapparatus_isfetchvalid();
/// wait for a message on any topic we are subscribed to
int diamondapparatus_waitforany();
/// read a string from the topic got
const char *diamondapparatus_fetchstring(int n);
/// read a float from the topic got
float diamondapparatus_fetchfloat(int n);
/// get the type of a datum in the topic got
uint32_t diamondapparatus_fetchtype(int n);
/// get the number of data in the topic got
int diamondapparatus_fetchsize();
#ifdef __cplusplus
}
#endif
#endif /* __DIAMONDAPPARATUS_H */