Subversion Repositories HomeAutomation

Rev

Rev 1608 | Rev 1620 | Go to most recent revision | Show entire file | Ignore 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
            {
Line 118... Line 126...
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);
134
    signal(SIGABRT, Handler);
143
    signal(SIGABRT, Handler);
135
    signal(SIGPIPE, Handler);
144
    signal(SIGPIPE, Handler);
136
   
145
   
137
    tcgetattr(fileno(stdin), &original_flags);
146
    tcgetattr(fileno(stdin), &original_flags);
138
       
147
       
139
    printf("Atom Interactive Console, version 1.5.0 starting...\n");
148
    printf("Atom Interactive Console, version 1.5.0 starting...\n");
140
    printf("Written by Mattias Runge 2010.\n");
149
    printf("Written by Mattias Runge 2010.\n");
141
    printf("Released under GPL version 2.\n");
150
    printf("Released under GPL version 2.\n");
Line 144... Line 153...
144
   
153
   
145
    std::string address = "localhost";
154
    std::string address = "localhost";
146
    unsigned int port = 1202;
155
    unsigned int port = 1202;
147
   
156
   
148
    printf("Connecting to %s:%d...", address.data(), port);
157
    printf("Connecting to %s:%d...", address.data(), port);
149
   
158
   
150
    try
159
    try
151
    {
160
    {
152
        cc = ConsoleClient::Pointer(new ConsoleClient(address, port));
161
        cc = ConsoleClient::Pointer(new ConsoleClient(address, port));
153
    }
162
    }
154
    catch (std::runtime_error& e)
163
    catch (std::runtime_error& e)
Line 161... Line 170...
161
   
170
   
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
   
-
 
175
   
-
 
176
    passwd* user_struct = getpwuid(getuid());
166
   
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
        }
Line 195... Line 209...
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;
-
 
214
   
-
 
215
    //printf("\nAutoComplete:start=%d, end=%d\n", start, end);
200
   
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
   
224
   
209
    waiting_for_autocomplete = true;
225
    waiting_for_autocomplete = true;
210
   
226
   
-
 
227
 
-
 
228
    std::string request = "A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(rl_line_buffer);
211
    //printf("Sending request\n");
229
    //printf("\nSending request:%s\n", request.data());
212
   
230
   
213
    cc->Send("A;"+ boost::lexical_cast<std::string>(count) + ";" + std::string(text));
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);    
-
 
235
   
-
 
236
    if (autocomplete_list.size() == 0)
-
 
237
    {
-
 
238
        return NULL;
-
 
239
    }
217
   
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
}
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