HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
main.cpp
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 #include <iostream>
18 #include <sstream>
19 
20 #include "uv.h"
21 
22 #include "common.h"
23 
24 #include "application.h"
25 #include "transport.h"
26 
32 // =============================================================================
33 // Global Variables.
34 // =============================================================================
35 
36 static HF::Application::Transport transport;
37 static uv_pipe_t stdin_pipe;
38 
39 // =============================================================================
40 // Libuv Helpers
41 // =============================================================================
42 
43 uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size);
44 
45 // =============================================================================
46 // read_stdin
47 // =============================================================================
55 // =============================================================================
56 static void read_stdin(uv_stream_t *stream, ssize_t nread, uv_buf_t buf)
57 {
58  UNUSED(stream);
59 
60  if (nread == -1)
61  {
62  if (uv_last_error(uv_default_loop()).code == UV_EOF)
63  {
64  uv_close((uv_handle_t *) &stdin_pipe, NULL);
65  }
66  }
67  else if (nread > 0)
68  {
69  std::string cmd(buf.base, nread);
70 
71  if (HF::Application::Handle(cmd))
72  {
73  transport.destroy();
74  uv_stop(uv_default_loop());
75  }
76  }
77 
78  if (buf.base)
79  {
80  free(buf.base);
81  }
82 }
83 
84 // =============================================================================
85 // Application Callbacks
86 // =============================================================================
87 
88 // =============================================================================
89 // HF::Application::Saved
90 // =============================================================================
94 // =============================================================================
96 {
97  LOG(INFO) << "Application configuration saved !" << NL;
98 }
99 
100 // =============================================================================
101 // HF::Application::Restored
102 // =============================================================================
106 // =============================================================================
108 {
109  LOG(INFO) << "Application configuration restored !" << NL;
110 }
111 
114 // =============================================================================
115 // Main
116 // =============================================================================
117 
118 int main(int argc, char *argv[])
119 {
120  UNUSED(argc);
121  UNUSED(argv);
122 
123  uv_loop_t *loop = uv_default_loop();
124 
125 #ifdef HF_BASE_APP
126  HF::UID::URI *uid = new HF::UID::URI("hf://base.example.com");
127 #endif
128 
129 #ifdef HF_NODE_APP
130 
131  if (argc < 2)
132  {
133  std::cout << "usage : " << argv[0] << " [number]" << std::endl;
134  exit(-1);
135  }
136 
137  std::stringstream ss;
138  ss << "hf://node.example.com/" << argv[1];
139 
140  HF::UID::URI *uid = new HF::UID::URI(ss.str());
141 #endif
142 
143  HF::Application::Initialize(transport);
144  transport.uid(uid);
145 
147 
148  uv_pipe_init(loop, &stdin_pipe, 0);
149  uv_pipe_open(&stdin_pipe, 0);
150 
151  uv_read_start((uv_stream_t *) &stdin_pipe, alloc_buffer, read_stdin);
152 
153  uv_run(loop, UV_RUN_DEFAULT);
154 
156 
157  return 0;
158 }
uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size)
This function is used to allocate the buffers used with libuv.
Definition: transport.cpp:212
bool Handle(std::string command)
Handle the command.
Definition: common.cpp:197
Transport Layer implementation over TCP/IP using libuv.
This file contains the definitions for the HAN-FUN example applications.
void Saved()
Callback indicating that the application configuration has been saved.
Definition: main.cpp:95
URI UID class.
Definition: uids.h:431
void Restored()
Callback indicating that the application configuration has been restored.
Definition: main.cpp:107
This file contains the declarations of the transport layer over libuv.
void destroy()
Free the system resources associated with this transport layer.
Definition: transport.cpp:576
#define NL
Helper define for new-line and stream clear.
Definition: debug.h:34
const HF::UID::UID uid() const
Return the UID of the local device on this transport layer.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
This file contains the definitions for the common functionality in the HAN-FUN example applications...
#define LOG(X)
Log messages with the level given by X.
Definition: debug.h:81
void Save()
Save application configuration.
Definition: base_app.cpp:473
void Initialize(HF::Transport::Layer &transport)
Initialize the application.
Definition: base_app.cpp:441