- /// Initialize MeshLink's configuration directory.
- /** This function causes MeshLink to initialize its configuration directory,
- * if it hasn't already been initialized.
- * It only has to be run the first time the application starts,
- * but it is not a problem if it is run more than once, as long as
- * the arguments given are the same.
- *
- * This function does not start any network I/O yet. The application should
- * first set callbacks, and then call meshlink_start().
- *
- * @param confbase The directory in which MeshLink will store its configuration files.
- * @param name The name which this instance of the application will use in the mesh.
- * @param appname The application name which will be used in the mesh.
- * @param dclass The device class which will be used in the mesh.
- *
- * @return This function will return a pointer to a meshlink::mesh if MeshLink has succesfully set up its configuration files, NULL otherwise.
- */
- bool open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
- handle = meshlink_open(confbase, name, appname, devclass);
- if(handle)
- handle->priv = this;
-
- return isOpen();
- }
-
- mesh(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
- open(confbase, name, appname, devclass);
- }
-
- /// Close the MeshLink handle.
- /** This function calls meshlink_stop() if necessary,
- * and frees all memory allocated by MeshLink.
- * Afterwards, the handle and any pointers to a struct meshlink_node are invalid.
- */
- void close() {
- if(handle)
- {
- handle->priv = 0;
- meshlink_close(handle);
- }
- handle=0;
- }
-
- /** instead of registerin callbacks you derive your own class and overwrite the following abstract member functions.
- * These functions are run in MeshLink's own thread.
- * It is therefore important that these functions use apprioriate methods (queues, pipes, locking, etc.)
- * to hand the data over to the application's thread.
- * These functions should also not block itself and return as quickly as possible.
- * The default member functions are no-ops, so you are not required to overwrite all these member functions
- */
-
- /// This function is called whenever another node sends data to the local node.
- virtual void receive(node* source, const void* data, size_t length) { /* do nothing */ }
-
- /// This functions is called whenever another node's status changed.
- virtual void node_status(node* peer, bool reachable) { /* do nothing */ }
-
- /// This functions is called whenever MeshLink has some information to log.
- virtual void log(log_level_t level, const char* message) { /* do nothing */ }
-
- /// This functions is called whenever another node attemps to open a channel to the local node.
- /**
- * If the channel is accepted, the poll_callback will be set to channel_poll and can be
- * changed using set_channel_poll_cb(). Likewise, the receive callback is set to
- * channel_receive().
- *
- * The function is run in MeshLink's own thread.
- * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
- * to pass data to or from the application's thread.
- * The callback should also not block itself and return as quickly as possible.
- *
- * @param channel A handle for the incoming channel.
- * @param port The port number the peer wishes to connect to.
- * @param data A pointer to a buffer containing data already received. (Not yet used.)
- * @param len The length of the data. (Not yet used.)
- *
- * @return This function should return true if the application accepts the incoming channel, false otherwise.
- * If returning false, the channel is invalid and may not be used anymore.
- */
- virtual bool channel_accept(channel *channel, uint16_t port, const void *data, size_t len)
- {
- /* by default reject all channels */
- return false;
- }
+ /// Start MeshLink.
+ /** This function causes MeshLink to open network sockets, make outgoing connections, and
+ * create a new thread, which will handle all network I/O.
+ *
+ * @return This function will return true if MeshLink has succesfully started its thread, false otherwise.
+ */
+ bool start() {
+ meshlink_set_receive_cb(handle, &receive_trampoline);
+ meshlink_set_node_status_cb(handle, &node_status_trampoline);
+ meshlink_set_node_duplicate_cb(handle, &node_duplicate_trampoline);
+ meshlink_set_log_cb(handle, MESHLINK_DEBUG, &log_trampoline);
+ meshlink_set_channel_accept_cb(handle, &channel_accept_trampoline);
+ return meshlink_start(handle);
+ }