Subversion Repositories HomeAutomation

Rev

Rev 1609 | Rev 1642 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1609 Rev 1620
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include <string>
21
#include <string>
22
#include <vector>
22
#include <vector>
23
 
23
 
24
#include <signal.h>
24
#include <signal.h>
25
#include <stdio.h>
25
#include <stdio.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
#include <pwd.h>
27
#include <pwd.h>
28
 
28
 
29
#include <boost/lexical_cast.hpp>
29
#include <boost/lexical_cast.hpp>
30
#include <boost/thread/mutex.hpp>
30
#include <boost/thread/mutex.hpp>
31
#include <boost/thread/condition.hpp>
31
#include <boost/thread/condition.hpp>
32
#include <boost/thread/locks.hpp>
32
#include <boost/thread/locks.hpp>
33
#include <boost/algorithm/string.hpp>
33
#include <boost/algorithm/string.hpp>
34
 
34
 
35
#include "net/Manager.h"
35
#include "net/Manager.h"
36
#include "net/Subscriber.h"
36
#include "net/Subscriber.h"
37
#include "net/types.h"
37
#include "net/types.h"
38
 
38
 
39
#include "type/common.h"
39
#include "type/common.h"
40
 
40
 
41
#include <readline/readline.h>
41
#include <readline/readline.h>
42
#include <readline/history.h>
42
#include <readline/history.h>
43
 
43
 
44
using namespace atom;
44
using namespace atom;
45
 
45
 
46
 
46
 
47
void Handler(int status);
47
void Handler(int status);
48
void CleanUp();
48
void CleanUp();
49
 
49
 
50
char* AutoCompleteGet(const char* text, int state);
50
char* AutoCompleteGet(const char* text, int state);
51
static char** AutoComplete(const char* text, int start, int end);
51
static char** AutoComplete(const char* text, int start, int end);
52
 
52
 
53
type::StringList autocomplete_list;
53
type::StringList autocomplete_list;
54
char* buffer = NULL;
54
char* buffer = NULL;
55
bool waiting_for_autocomplete = false;
55
bool waiting_for_autocomplete = false;
56
boost::condition on_message_condition;
56
boost::condition on_message_condition;
57
boost::mutex guard_mutex;
57
boost::mutex guard_mutex;
-
 
58
std::string prompt;
-
 
59
int prompt_id = -1;
58
 
60
 
59
class ConsoleClient : public net::Subscriber
61
class ConsoleClient : public net::Subscriber
60
{
62
{
61
public:
63
public:
62
    typedef boost::shared_ptr<ConsoleClient> Pointer;
64
    typedef boost::shared_ptr<ConsoleClient> Pointer;
63
   
65
   
64
    ConsoleClient(std::string address, unsigned int port)
66
    ConsoleClient(std::string address, unsigned int port)
65
    {
67
    {
-
 
68
        this->identifier_ = address + ":" + boost::lexical_cast<std::string>(port);
66
        this->client_id_ = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, address, port);
69
        this->client_id_ = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, address, port);
67
    }
70
    }
68
   
71
   
69
    virtual ~ConsoleClient()
72
    virtual ~ConsoleClient()
70
    {
73
    {
71
        net::Manager::Instance()->Disconnect(this->client_id_);
74
        net::Manager::Instance()->Disconnect(this->client_id_);
72
 
75
 
73
        this->io_service_.stop();
76
        this->io_service_.stop();
74
    }
77
    }
75
   
78
   
76
    void Send(std::string data)
79
    void Send(std::string data)
77
    {
80
    {
78
        net::Manager::Instance()->SendTo(this->client_id_, data);
81
        net::Manager::Instance()->SendTo(this->client_id_, data);
79
    }
82
    }
80
   
83
   
81
    bool IsConnected()
84
    bool IsConnected()
82
    {
85
    {
83
        return this->client_id_ != 0;
86
        return this->client_id_ != 0;
-
 
87
    }
-
 
88
   
-
 
89
    std::string GetIdentifier()
-
 
90
    {
-
 
91
        return this->identifier_;
84
    }
92
    }
85
   
93
   
86
private:
94
private:
87
    net::ClientId client_id_;
95
    net::ClientId client_id_;
-
 
96
    std::string identifier_;
-
 
97
   
88
   
98
   
89
    void SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
99
    void SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
90
    {
100
    {
91
        if (client_state == net::CLIENT_STATE_CONNECTED)
101
        if (client_state == net::CLIENT_STATE_CONNECTED)
92
        {
102
        {
93
            printf("Connected to Atom Daemon!\n");
103
            printf("Connected to Atom Daemon!\n");
94
        }
104
        }
95
        else
105
        else
96
        {
106
        {
97
            printf("\nDisconnected!\n");
107
            printf("\nDisconnected!\n");
98
            this->client_id_ = 0;
108
            this->client_id_ = 0;
99
           
109
           
100
            kill(getpid(), SIGTERM);
110
            kill(getpid(), SIGTERM);
101
        }
111
        }
102
    }
112
    }
103
   
113
   
104
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
114
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
105
    {
115
    {
-
 
116
        prompt_id = -1;
-
 
117
       
106
        std::string s(data.ToCharString());
118
        std::string s(data.ToCharString());
107
       
119
       
108
        if (waiting_for_autocomplete)
120
        if (waiting_for_autocomplete)
109
        {
121
        {
110
            autocomplete_list.clear();
122
            autocomplete_list.clear();
111
           
123
           
112
            boost::algorithm::trim_if(s, boost::is_any_of("\n"));
124
            boost::algorithm::trim_if(s, boost::is_any_of("\n"));
113
            boost::algorithm::trim_if(s, boost::is_any_of(" "));
125
            boost::algorithm::trim_if(s, boost::is_any_of(" "));
114
           
126
           
115
            if (s != "")
127
            if (s != "")
116
            {
128
            {
117
                boost::algorithm::split(autocomplete_list, s, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
129
                boost::algorithm::split(autocomplete_list, s, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
118
            }
130
            }
119
        }
131
        }
120
        else
132
        else
121
        {
133
        {
122
            if (s[s.length()-1] != '\n')
134
            type::StringList lines;
-
 
135
           
-
 
136
            boost::algorithm::split(lines, s, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
-
 
137
           
-
 
138
            for (unsigned int n = 0; n < lines.size(); n++)
123
            {
139
            {
-
 
140
                type::StringList parts;
-
 
141
               
-
 
142
                boost::algorithm::split(parts, lines[n], boost::is_any_of(";"), boost::algorithm::token_compress_off);
-
 
143
               
-
 
144
                if (parts[0].length() == 1 && parts[0] == "P")
-
 
145
                {
-
 
146
                    prompt_id = boost::lexical_cast<unsigned int>(parts[1]);
-
 
147
                   
-
 
148
                    prompt = "";
-
 
149
                   
-
 
150
                    for (unsigned int c = 2; c < parts.size(); c++)
-
 
151
                    {
-
 
152
                        prompt += parts[c] + " ";
124
                s += "\n";
153
                    }
125
            }
154
                }
-
 
155
                else
126
           
156
                {
-
 
157
                    boost::algorithm::trim_if(lines[n], boost::is_any_of(" "));
-
 
158
                   
-
 
159
                    if (lines[n] != "")
-
 
160
                    {
127
            printf("%s", s.data());
161
                        printf("%s\n", lines[n].data());
-
 
162
                    }
-
 
163
                }
-
 
164
            }
128
        }
165
        }
129
         
166
         
130
        on_message_condition.notify_all();
167
        on_message_condition.notify_all();
131
    }
168
    }
132
};
169
};
133
 
170
 
134
ConsoleClient::Pointer cc;
171
ConsoleClient::Pointer cc;
135
struct termios original_flags;
172
struct termios original_flags;
136
std::string history_filename;
173
std::string history_filename;
137
 
174
 
138
int main(int argc, char **argv)
175
int main(int argc, char **argv)
139
{
176
{
140
    signal(SIGTERM, Handler);
177
    signal(SIGTERM, Handler);
141
    signal(SIGINT, Handler);
178
    signal(SIGINT, Handler);
142
    signal(SIGQUIT, Handler);
179
    signal(SIGQUIT, Handler);
143
    signal(SIGABRT, Handler);
180
    signal(SIGABRT, Handler);
144
    signal(SIGPIPE, Handler);
181
    signal(SIGPIPE, Handler);
145
   
182
   
146
    tcgetattr(fileno(stdin), &original_flags);
183
    tcgetattr(fileno(stdin), &original_flags);
147
       
184
       
148
    printf("Atom Interactive Console, version 1.5.0 starting...\n");
185
    printf("Atom Interactive Console, version 1.5.0 starting...\n");
149
    printf("Written by Mattias Runge 2010.\n");
186
    printf("Written by Mattias Runge 2010.\n");
150
    printf("Released under GPL version 2.\n");
187
    printf("Released under GPL version 2.\n");
151
   
188
   
152
    net::Manager::Create();
189
    net::Manager::Create();
153
   
190
   
154
    std::string address = "localhost";
191
    std::string address = "localhost";
155
    unsigned int port = 1202;
192
    unsigned int port = 1202;
156
   
193
   
157
    printf("Connecting to %s:%d...", address.data(), port);
194
    printf("Connecting to %s:%d...", address.data(), port);
158
   
195
   
159
    try
196
    try
160
    {
197
    {
161
        cc = ConsoleClient::Pointer(new ConsoleClient(address, port));
198
        cc = ConsoleClient::Pointer(new ConsoleClient(address, port));
162
    }
199
    }
163
    catch (std::runtime_error& e)
200
    catch (std::runtime_error& e)
164
    {
201
    {
165
        printf("%s\n", e.what());
202
        printf("%s\n", e.what());
166
        CleanUp();
203
        CleanUp();
167
       
204
       
168
        return EXIT_FAILURE;
205
        return EXIT_FAILURE;
169
    }
206
    }
170
   
207
   
171
    printf("success!\n");
208
    printf("success!\n");
172
   
-
 
173
    std::string prompt = address + ":" + boost::lexical_cast<std::string>(port) + "] ";
-
 
174
   
-
 
175
   
209
   
176
    passwd* user_struct = getpwuid(getuid());
210
    prompt = cc->GetIdentifier() + "] ";
177
   
211
   
178
    history_filename = std::string(user_struct->pw_dir) + "/.atomic_history";
212
    passwd* user_struct = getpwuid(getuid());
179
   
213
   
-
 
214
    history_filename = std::string(user_struct->pw_dir) + "/.atomic_history";
-
 
215
   
180
    read_history(history_filename.data());
216
    read_history(history_filename.data());
181
   
217
   
182
    rl_attempted_completion_function = AutoComplete;
218
    rl_attempted_completion_function = AutoComplete;
183
   
219
   
184
    while ((buffer = readline(prompt.data())) != NULL)
220
    while ((buffer = readline(prompt.data())) != NULL)
185
    {
221
    {
186
        if (strlen(buffer) == 0)
222
        if (strlen(buffer) == 0)
187
        {
223
        {
188
            continue;
224
            continue;
189
        }
225
        }
190
       
226
       
191
        if (strcmp(buffer, "quit") == 0)
227
        if (strcmp(buffer, "quit") == 0)
192
        {
228
        {
193
            break;
229
            break;
194
        }
230
        }
195
       
231
       
196
        waiting_for_autocomplete = false;
232
        waiting_for_autocomplete = false;
-
 
233
        prompt = cc->GetIdentifier() + "] ";
197
       
234
       
-
 
235
        if (prompt_id == -1)
-
 
236
        {
198
        cc->Send("E;" + std::string(buffer));
237
            cc->Send("E;" + std::string(buffer));
-
 
238
            add_history(buffer);
-
 
239
        }
-
 
240
        else
-
 
241
        {
-
 
242
            cc->Send("R;" + boost::lexical_cast<std::string>(prompt_id) + ";" + std::string(buffer));
-
 
243
            prompt_id = -1;
-
 
244
        }
199
       
245
       
200
        boost::mutex::scoped_lock guard(guard_mutex);
246
        boost::mutex::scoped_lock guard(guard_mutex);
201
        on_message_condition.wait(guard);    
247
        on_message_condition.wait(guard);    
202
       
-
 
203
        add_history(buffer);
-
 
204
    }
248
    }
205
   
249
   
206
    CleanUp();
250
    CleanUp();
207
   
251
   
208
    return EXIT_SUCCESS;
252
    return EXIT_SUCCESS;
209
}
253
}
210
 
254
 
211
static char** AutoComplete(const char* text, int start, int end)
255
static char** AutoComplete(const char* text, int start, int end)
212
{
256
{
213
    unsigned int count = 0;
257
    unsigned int count = 0;
214
   
258
   
215
    //printf("\nAutoComplete:start=%d, end=%d\n", start, end);
259
    //printf("\nAutoComplete:start=%d, end=%d\n", start, end);
216
   
260
   
217
    for (unsigned int n = 0; n < start; n++)
261
    for (unsigned int n = 0; n < start; n++)
218
    {
262
    {
219
        if (rl_line_buffer[n] == ' ')
263
        if (rl_line_buffer[n] == ' ')
220
        {
264
        {
221
            count++;
265
            count++;
222
        }
266
        }
223
    }
267
    }
224
   
268
   
225
    waiting_for_autocomplete = true;
269
    waiting_for_autocomplete = true;
226
   
270
   
227
 
271
 
228
    std::string request = "A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(rl_line_buffer);
272
    std::string request = "A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(rl_line_buffer);
229
    //printf("\nSending request:%s\n", request.data());
273
    //printf("\nSending request:%s\n", request.data());
230
   
274
   
231
    cc->Send(request);
275
    cc->Send(request);
232
   
276
   
233
    boost::mutex::scoped_lock guard(guard_mutex);
277
    boost::mutex::scoped_lock guard(guard_mutex);
234
    on_message_condition.wait(guard);    
278
    on_message_condition.wait(guard);    
235
   
279
   
236
    if (autocomplete_list.size() == 0)
280
    if (autocomplete_list.size() == 0)
237
    {
281
    {
238
        return NULL;
282
        return NULL;
239
    }
283
    }
240
   
284
   
241
    //printf("processing response\n");
285
    //printf("processing response\n");
242
   
286
   
243
    return rl_completion_matches(text, &AutoCompleteGet);
287
    return rl_completion_matches(text, &AutoCompleteGet);
244
}
288
}
245
 
289
 
246
char* AutoCompleteGet(const char* text, int state)
290
char* AutoCompleteGet(const char* text, int state)
247
{
291
{
248
    static int index = 0;
292
    static int index = 0;
249
    int length = strlen(text);
293
    int length = strlen(text);
250
    std::string name;
294
    std::string name;
251
   
295
   
252
    //printf("run, %s, %d, %d\n", text, state, autocomplete_list.size());
296
    //printf("run, %s, %d, %d\n", text, state, autocomplete_list.size());
253
   
297
   
254
    if (!state) // First run
298
    if (!state) // First run
255
    {
299
    {
256
        index = 0;
300
        index = 0;
257
    }
301
    }
258
   
302
   
259
    while (autocomplete_list.size() > index)
303
    while (autocomplete_list.size() > index)
260
    {
304
    {
261
        name = autocomplete_list[index];
305
        name = autocomplete_list[index];
262
        //printf("testing name = %s\n", name.data());
306
        //printf("testing name = %s\n", name.data());
263
       
307
       
264
        index++;
308
        index++;
265
       
309
       
266
        if (strncmp(name.data(), text, length) == 0)
310
        if (strncmp(name.data(), text, length) == 0)
267
        {
311
        {
268
            char *result = (char*)malloc(name.size() + 1);
312
            char *result = (char*)malloc(name.size() + 1);
269
            strcpy(result, name.data());
313
            strcpy(result, name.data());
270
           
314
           
271
            return result;
315
            return result;
272
        }
316
        }
273
    }
317
    }
274
   
318
   
275
    return NULL;
319
    return NULL;
276
}
320
}
277
 
321
 
278
void CleanUp()
322
void CleanUp()
279
{
323
{
280
    printf("Cleaning up...\n");
324
    printf("Cleaning up...\n");
281
 
325
 
282
    write_history(history_filename.data());
326
    write_history(history_filename.data());
283
   
327
   
284
    cc.reset();
328
    cc.reset();
285
   
329
   
286
    net::Manager::Delete();
330
    net::Manager::Delete();
287
   
331
   
288
    if (buffer != NULL)
332
    if (buffer != NULL)
289
    {
333
    {
290
        free(buffer);
334
        free(buffer);
291
    }
335
    }
292
   
336
   
293
    printf("Thank you for using Atom. Goodbye!\n");
337
    printf("Thank you for using Atom. Goodbye!\n");
294
   
338
   
295
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
339
    tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore
296
}
340
}
297
 
341
 
298
void Handler(int status)
342
void Handler(int status)
299
{
343
{
300
    std::string signal_name = "Unknown";
344
    std::string signal_name = "Unknown";
301
   
345
   
302
    switch (status)
346
    switch (status)
303
    {
347
    {
304
        case SIGTERM:
348
        case SIGTERM:
305
        {
349
        {
306
            signal_name = "Terminate";
350
            signal_name = "Terminate";
307
            break;
351
            break;
308
        }  
352
        }  
309
        case SIGINT:
353
        case SIGINT:
310
        {
354
        {
311
            signal_name = "Interupt";
355
            signal_name = "Interupt";
312
            break;
356
            break;
313
        }  
357
        }  
314
        case SIGQUIT:
358
        case SIGQUIT:
315
        {
359
        {
316
            signal_name = "Quit";
360
            signal_name = "Quit";
317
            break;
361
            break;
318
        }  
362
        }  
319
        case SIGABRT:
363
        case SIGABRT:
320
        {
364
        {
321
            signal_name = "Abort";
365
            signal_name = "Abort";
322
            break;
366
            break;
323
        }  
367
        }  
324
        case SIGIO:
368
        case SIGIO:
325
        {
369
        {
326
            signal_name = "I/O";
370
            signal_name = "I/O";
327
            break;
371
            break;
328
        }  
372
        }  
329
        case SIGPIPE:
373
        case SIGPIPE:
330
        {
374
        {
331
            signal_name = "Pipe";
375
            signal_name = "Pipe";
332
            break;
376
            break;
333
        }
377
        }
334
    }
378
    }
335
   
379
   
336
   
380
   
337
    if (status != SIGPIPE)
381
    if (status != SIGPIPE)
338
    {
382
    {
339
        CleanUp();
383
        CleanUp();
340
        exit(0);
384
        exit(0);
341
        //on_message_condition.notify_all();
385
        //on_message_condition.notify_all();
342
    }
386
    }
343
}
387
}