Subversion Repositories HomeAutomation

Rev

Rev 1657 | Rev 1665 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1657 Rev 1664
Line 115... Line 115...
115
   
115
   
116
    void SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
116
    void SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
117
    {
117
    {
118
        if (client_state != net::CLIENT_STATE_CONNECTED)
118
        if (client_state != net::CLIENT_STATE_CONNECTED)
119
        {
119
        {
120
            std::cout << "Disconnected from server" << std::endl;
120
            std::cout << "Disconnected from server." << std::endl;
121
           
121
           
122
            this->client_id_ = 0;
122
            this->client_id_ = 0;
123
            finish = true;
123
            finish = true;
124
           
-
 
125
            kill(getpid(), SIGTERM);
124
            on_message_condition.notify_all();
126
        }
125
        }
127
    }
126
    }
128
   
127
   
129
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
128
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
130
    {
129
    {
Line 153... Line 152...
153
                    std::string payload = s.substr(8, payload_length - 1);
152
                    std::string payload = s.substr(8, payload_length - 1);
154
                   
153
                   
155
                    if (payload.length() > 0)
154
                    if (payload.length() > 0)
156
                    {
155
                    {
157
                        boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
156
                        boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
158
                    }
157
                    }
159
                }
158
                }
160
               
159
               
161
                on_message_condition.notify_all();
160
                on_message_condition.notify_all();
162
            }
161
            }
163
            else
162
            else
164
            {
163
            {
165
                std::cerr << "Unknown data received: " << s << std::endl;
164
                std::cerr << "Unknown data received: " << s << std::endl;
166
                finish = true;
165
                finish = true;
167
                on_message_condition.notify_all();
166
                on_message_condition.notify_all();
168
                break;
167
                break;
169
            }
168
            }
170
           
169
           
171
            if (8 + payload_length >= s.length())
170
            if (8 + payload_length >= s.length())
172
            {
171
            {
173
                break;
172
                break;
174
            }
173
            }
175
           
174
           
176
            s = s.substr(8 + payload_length);
175
            s = s.substr(8 + payload_length);
177
        }
176
        }
178
    }
177
    }
179
};
178
};
180
 
179
 
181
ConsoleClient::Pointer cc;
180
ConsoleClient::Pointer cc;
182
 
181
 
183
 
182
 
184
int main(int argc, char **argv)
183
int main(int argc, char **argv)
185
{
184
{
186
    // Signal handlers
185
    // Signal handlers
187
    signal(SIGTERM, Handler);
186
    signal(SIGTERM, Handler);
188
    signal(SIGINT, Handler);
187
    signal(SIGINT, Handler);
Line 193... Line 192...
193
    boost::mutex guard_mutex;
192
    boost::mutex guard_mutex;
194
   
193
   
195
    // Setup readline
194
    // Setup readline
196
    history_filename = GetUserHomeDirectory() + "/.atomic_history";
195
    history_filename = GetUserHomeDirectory() + "/.atomic_history";
197
    read_history(history_filename.data());
196
    read_history(history_filename.data());
198
   
197
   
199
    rl_attempted_completion_function = AutoComplete;
198
    rl_attempted_completion_function = AutoComplete;
200
   
199
   
201
    // Save command line state
200
    // Save command line state
202
    tcgetattr(fileno(stdin), &original_flags);
201
    tcgetattr(fileno(stdin), &original_flags);
203
   
202
   
204
    // Parse commandline
203
    // Parse commandline
205
    boost::program_options::options_description command_line;
204
    boost::program_options::options_description command_line;
206
    boost::program_options::variables_map variable_map;
205
    boost::program_options::variables_map variable_map;
207
   
206
   
208
    command_line.add_options()
207
    command_line.add_options()
209
    ("help,h",    "produce help message")
208
    ("help,h",    "produce help message")
-
 
209
    ("command,c", boost::program_options::value<std::string>()->default_value(""), "command")
210
    ("server,s",  boost::program_options::value<std::string>()->default_value("localhost"), "server address")
210
    ("server,s",  boost::program_options::value<std::string>()->default_value("localhost"), "server address")
211
    ("port,p",    boost::program_options::value<unsigned int>()->default_value(1202), "server port");
211
    ("port,p",    boost::program_options::value<unsigned int>()->default_value(1202), "server port");
212
   
212
   
213
    try
213
    try
214
    {
214
    {
215
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map);
215
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map);
216
    }
216
    }
217
    catch (boost::program_options::unknown_option e)
217
    catch (boost::program_options::unknown_option e)
218
    {
218
    {
219
        std::cerr << e.what() << std::endl;
219
        std::cerr << e.what() << std::endl;
220
        std::cout << command_line << std::endl;
220
        std::cout << command_line << std::endl;
221
        CleanUp();
221
        CleanUp();
222
        return EXIT_FAILURE;
222
        return EXIT_FAILURE;
223
    }
223
    }
224
    catch (boost::program_options::invalid_syntax e)
224
    catch (boost::program_options::invalid_syntax e)
225
    {
225
    {
226
        std::cerr << e.what() << std::endl;
226
        std::cerr << e.what() << std::endl;
227
        std::cout << command_line << std::endl;
227
        std::cout << command_line << std::endl;
228
        CleanUp();
228
        CleanUp();
229
        return EXIT_FAILURE;
229
        return EXIT_FAILURE;
230
    }
230
    }
231
   
231
   
232
    if (variable_map.count("help") != 0)
232
    if (variable_map.count("help") != 0)
233
    {
233
    {
234
        std::cout << command_line << std::endl;
234
        std::cout << command_line << std::endl;
235
        CleanUp();
235
        CleanUp();
236
        return EXIT_SUCCESS;
236
        return EXIT_SUCCESS;
237
    }
237
    }
238
   
238
   
-
 
239
    net::Manager::Create();
239
   
240
 
-
 
241
    if (variable_map["command"].as<std::string>() == "")
-
 
242
    {
240
    std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl;
243
        std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl;
241
    std::cout << "\033[29;1mReleased under GPL version 2.\033[0m" << std::endl;
244
        std::cout << "\033[29;1mReleased under GPL version 2.\033[0m" << std::endl;
242
    std::cout << "Written by Mattias Runge 2010." << std::endl;
245
        std::cout << "Written by Mattias Runge 2010." << std::endl;
243
   
-
 
244
   
-
 
245
    net::Manager::Create();
-
 
246
       
246
       
247
    std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl;
247
        std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl;
-
 
248
    }
248
   
249
   
249
    try
250
    try
250
    {
251
    {
251
        cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>()));
252
        cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>()));
252
    }
253
    }
253
    catch (std::runtime_error& e)
254
    catch (std::runtime_error& e)
254
    {
255
    {
255
        std::cout << "error!" << std::endl;
-
 
256
        std::cerr << e.what() << std::endl;
256
        std::cerr << "Connection error: " << e.what() << std::endl;
257
        CleanUp();
257
        CleanUp();
258
        return EXIT_FAILURE;
258
        return EXIT_FAILURE;
259
    }
259
    }
260
   
260
   
-
 
261
    if (variable_map["command"].as<std::string>() != "")
-
 
262
    {
-
 
263
        boost::mutex::scoped_lock guard(guard_mutex);
-
 
264
        on_message_condition.wait(guard);
-
 
265
       
-
 
266
        if (!finish)
-
 
267
        {
-
 
268
            cc->SendResponse(variable_map["command"].as<std::string>());
-
 
269
           
-
 
270
            if (!finish)
-
 
271
            {
-
 
272
                on_message_condition.wait(guard);
-
 
273
            }
-
 
274
        }
-
 
275
    }
-
 
276
    else
-
 
277
    {
261
    while (true)
278
        while (true)
262
    {
279
        {
263
        boost::mutex::scoped_lock guard(guard_mutex);
280
            boost::mutex::scoped_lock guard(guard_mutex);
264
        on_message_condition.wait(guard);
281
            on_message_condition.wait(guard);
265
       
282
           
Line 278... Line 295...
278
            break;
295
                break;
279
        }
296
            }
280
       
297
           
281
        cc->SendResponse(buffer);
298
            cc->SendResponse(buffer);
282
        add_history(buffer);
299
            add_history(buffer);
-
 
300
        }
283
    }
301
    }
284
   
302
   
285
    CleanUp();
303
    CleanUp();
286
   
304
   
287
    return EXIT_SUCCESS;
305
    return EXIT_SUCCESS;
Line 342... Line 360...
342
    return NULL;
360
    return NULL;
343
}
361
}
344
 
362
 
345
void CleanUp()
363
void CleanUp()
346
{
364
{
-
 
365
    std::cout << std::endl;
347
    std::cout << "Cleaning up..." << std::endl;
366
    //std::cout << "Cleaning up..." << std::endl;
348
 
367
 
349
    write_history(history_filename.data());
368
    write_history(history_filename.data());
350
   
369
   
351
    cc.reset();
370
    cc.reset();
352
   
371
   
Line 355... Line 374...
355
    if (buffer != NULL)
374
    if (buffer != NULL)
356
    {
375
    {
357
        free(buffer);
376
        free(buffer);
358
    }
377
    }
359
   
378
   
360
    std::cout << "Thank you for using Atom. Goodbye!" << std::endl;
379
    //std::cout << "Thank you for using Atom. Goodbye!" << std::endl;
361
   
380
   
362
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
381
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
363
}
382
}
364
 
383
 
365
void Handler(int status)
384
void Handler(int status)