Subversion Repositories HomeAutomation

Rev

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

Rev 1609 Rev 1620
Line 53... Line 53...
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
    {
Line 79... Line 82...
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
        {
Line 101... Line 111...
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
 
Line 141... Line 178...
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;