Subversion Repositories HomeAutomation

Rev

Rev 1657 | Rev 1665 | Go to most recent revision | Show entire file | Ignore 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 205... Line 204...
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
    {
Line 234... Line 234...
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();
-
 
240
 
-
 
241
    if (variable_map["command"].as<std::string>() == "")
239
   
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
    while (true)
261
    if (variable_map["command"].as<std::string>() != "")
262
    {
262
    {
263
        boost::mutex::scoped_lock guard(guard_mutex);
263
        boost::mutex::scoped_lock guard(guard_mutex);
264
        on_message_condition.wait(guard);
264
        on_message_condition.wait(guard);
265
       
265
       
266
        if (finish)
266
        if (!finish)
267
        {
-
 
268
            break;
-
 
269
        }
-
 
270
       
-
 
271
        while ((buffer = readline(cc->GetPrompt().data())) != NULL)
-
 
272
        {
267
        {
-
 
268
            cc->SendResponse(variable_map["command"].as<std::string>());
-
 
269
           
273
            if (strlen(buffer) == 0)
270
            if (!finish)
-
 
271
            {
-
 
272
                on_message_condition.wait(guard);
-
 
273
            }
-
 
274
        }
-
 
275
    }
-
 
276
    else
-
 
277
    {
-
 
278
        while (true)
-
 
279
        {
-
 
280
            boost::mutex::scoped_lock guard(guard_mutex);
-
 
281
            on_message_condition.wait(guard);
-
 
282
           
-
 
283
            if (finish)
-
 
284
            {
-
 
285
                break;
-
 
286
            }
-
 
287
           
-
 
288
            while ((buffer = readline(cc->GetPrompt().data())) != NULL)
274
            {
289
            {
-
 
290
                if (strlen(buffer) == 0)
-
 
291
                {
275
                continue;
292
                    continue;
-
 
293
                }
-
 
294
               
-
 
295
                break;
276
            }
296
            }
277
           
297
           
-
 
298
            cc->SendResponse(buffer);
278
            break;
299
            add_history(buffer);
279
        }
300
        }
280
       
-
 
281
        cc->SendResponse(buffer);
-
 
282
        add_history(buffer);
-
 
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)