Subversion Repositories HomeAutomation

Rev

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

Rev 1608 Rev 1609
Line 22... Line 22...
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
 
28
 
28
#include <boost/lexical_cast.hpp>
29
#include <boost/lexical_cast.hpp>
29
#include <boost/thread/mutex.hpp>
30
#include <boost/thread/mutex.hpp>
30
#include <boost/thread/condition.hpp>
31
#include <boost/thread/condition.hpp>
31
#include <boost/thread/locks.hpp>
32
#include <boost/thread/locks.hpp>
Line 94... Line 95...
94
        else
95
        else
95
        {
96
        {
96
            printf("\nDisconnected!\n");
97
            printf("\nDisconnected!\n");
97
            this->client_id_ = 0;
98
            this->client_id_ = 0;
98
           
99
           
99
            kill(getpid(), SIGABRT);
100
            kill(getpid(), SIGTERM);
100
        }
101
        }
101
    }
102
    }
102
   
103
   
103
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
104
    void SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
104
    {
105
    {
105
        std::string s(data.ToCharString());
106
        std::string s(data.ToCharString());
106
       
107
       
107
        if (waiting_for_autocomplete)
108
        if (waiting_for_autocomplete)
108
        {
109
        {
109
            autocomplete_list.clear();
110
            autocomplete_list.clear();
-
 
111
           
-
 
112
            boost::algorithm::trim_if(s, boost::is_any_of("\n"));
-
 
113
            boost::algorithm::trim_if(s, boost::is_any_of(" "));
-
 
114
           
-
 
115
            if (s != "")
-
 
116
            {
110
            boost::algorithm::split(autocomplete_list, s, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
117
                boost::algorithm::split(autocomplete_list, s, boost::is_any_of("\n"), boost::algorithm::token_compress_on);
-
 
118
            }
111
        }
119
        }
112
        else
120
        else
113
        {
121
        {
114
            if (s[s.length()-1] != '\n')
122
            if (s[s.length()-1] != '\n')
115
            {
123
            {
116
                s += "\n";
124
                s += "\n";
117
            }
125
            }
118
           
126
           
119
            printf("%s", s.data());
127
            printf("%s", s.data());
120
        }
128
        }
121
         
129
         
122
        on_message_condition.notify_all();
130
        on_message_condition.notify_all();
123
    }
131
    }
124
};
132
};
125
 
133
 
126
ConsoleClient::Pointer cc;
134
ConsoleClient::Pointer cc;
127
struct termios original_flags;
135
struct termios original_flags;
-
 
136
std::string history_filename;
128
 
137
 
129
int main(int argc, char **argv)
138
int main(int argc, char **argv)
130
{
139
{
131
    signal(SIGTERM, Handler);
140
    signal(SIGTERM, Handler);
132
    signal(SIGINT, Handler);
141
    signal(SIGINT, Handler);
133
    signal(SIGQUIT, Handler);
142
    signal(SIGQUIT, Handler);
Line 162... Line 171...
162
    printf("success!\n");
171
    printf("success!\n");
163
   
172
   
164
    std::string prompt = address + ":" + boost::lexical_cast<std::string>(port) + "] ";
173
    std::string prompt = address + ":" + boost::lexical_cast<std::string>(port) + "] ";
165
   
174
   
166
   
175
   
-
 
176
    passwd* user_struct = getpwuid(getuid());
-
 
177
   
-
 
178
    history_filename = std::string(user_struct->pw_dir) + "/.atomic_history";
-
 
179
   
-
 
180
    read_history(history_filename.data());
167
   
181
   
168
    rl_attempted_completion_function = AutoComplete;
182
    rl_attempted_completion_function = AutoComplete;
169
   
183
   
170
    while ((buffer = readline(prompt.data())) != NULL)
184
    while ((buffer = readline(prompt.data())) != NULL)
171
    {
185
    {
172
        if (strlen(buffer) == 0)
186
        if (strlen(buffer) == 0)
173
        {
187
        {
174
            continue;
188
            continue;
175
        }
189
        }
176
       
190
       
177
        if (strcmp(buffer, "quit") == 0)
191
        if (strcmp(buffer, "quit") == 0)
178
        {
192
        {
179
            break;
193
            break;
180
        }
194
        }
181
       
195
       
182
        waiting_for_autocomplete = false;
196
        waiting_for_autocomplete = false;
183
       
197
       
184
        cc->Send("E;" + std::string(buffer));
198
        cc->Send("E;" + std::string(buffer));
185
       
199
       
186
        boost::mutex::scoped_lock guard(guard_mutex);
200
        boost::mutex::scoped_lock guard(guard_mutex);
187
        on_message_condition.wait(guard);    
201
        on_message_condition.wait(guard);    
188
       
202
       
189
        add_history(buffer);
203
        add_history(buffer);
190
    }
204
    }
191
   
205
   
192
    CleanUp();
206
    CleanUp();
193
   
207
   
194
    return EXIT_SUCCESS;
208
    return EXIT_SUCCESS;
195
}
209
}
196
 
210
 
197
static char** AutoComplete(const char* text, int start, int end)
211
static char** AutoComplete(const char* text, int start, int end)
198
{
212
{
199
    unsigned int count = 0;
213
    unsigned int count = 0;
200
   
214
   
-
 
215
    //printf("\nAutoComplete:start=%d, end=%d\n", start, end);
-
 
216
   
201
    for (unsigned int n = 0; n < start; n++)
217
    for (unsigned int n = 0; n < start; n++)
202
    {
218
    {
203
        if (text[n] == ' ')
219
        if (rl_line_buffer[n] == ' ')
204
        {
220
        {
205
            count++;
221
            count++;
206
        }
222
        }
207
    }
223
    }
208
   
-
 
209
    waiting_for_autocomplete = true;
-
 
210
   
224
   
211
    //printf("Sending request\n");
225
    waiting_for_autocomplete = true;
212
   
226
   
-
 
227
 
213
    cc->Send("A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(text));
228
    std::string request = "A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(rl_line_buffer);
-
 
229
    //printf("\nSending request:%s\n", request.data());
-
 
230
   
-
 
231
    cc->Send(request);
214
   
232
   
215
    boost::mutex::scoped_lock guard(guard_mutex);
233
    boost::mutex::scoped_lock guard(guard_mutex);
216
    on_message_condition.wait(guard);    
234
    on_message_condition.wait(guard);    
217
   
235
   
-
 
236
    if (autocomplete_list.size() == 0)
-
 
237
    {
-
 
238
        return NULL;
-
 
239
    }
-
 
240
   
218
    //printf("processing response\n");
241
    //printf("processing response\n");
219
   
242
   
220
    return rl_completion_matches(text, &AutoCompleteGet);
243
    return rl_completion_matches(text, &AutoCompleteGet);
221
}
244
}
222
 
245
 
223
char* AutoCompleteGet(const char* text, int state)
246
char* AutoCompleteGet(const char* text, int state)
224
{
247
{
225
    static int index = 0;
248
    static int index = 0;
226
    int length = strlen(text);
249
    int length = strlen(text);
Line 253... Line 276...
253
}
276
}
254
 
277
 
255
void CleanUp()
278
void CleanUp()
256
{
279
{
257
    printf("Cleaning up...\n");
280
    printf("Cleaning up...\n");
-
 
281
 
-
 
282
    write_history(history_filename.data());
258
   
283
   
259
    cc.reset();
284
    cc.reset();
260
   
285
   
261
    net::Manager::Delete();
286
    net::Manager::Delete();
262
   
287