]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.cpp
Introduced logger for plugins.
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 /*
23  $Revision: 1.33 $
24  $Date: 2010/04/16 12:30:37 $
25  $Author: faust $
26 */
27
28 #include <sys/time.h>
29
30 #include <csignal>
31 #include <cassert>
32 #include <cstdlib>
33 #include <algorithm>
34
35 #include "stg/common.h"
36 #include "stg/locker.h"
37 #include "stg/user_property.h"
38 #include "stg/plugin_creator.h"
39 #include "stg/logger.h"
40 #include "rscript.h"
41 #include "ur_functor.h"
42 #include "send_functor.h"
43
44 extern volatile const time_t stgTime;
45
46 #define RS_MAX_ROUTERS  (100)
47
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 PLUGIN_CREATOR<REMOTE_SCRIPT> rsc;
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 PLUGIN * GetPlugin()
56 {
57 return rsc.GetPlugin();
58 }
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 RS_USER & RS_USER::operator=(const RS_USER & rvalue)
63 {
64 lastSentTime = rvalue.lastSentTime;
65 user = rvalue.user;
66 routers = rvalue.routers;
67 shortPacketsCount = rvalue.shortPacketsCount;
68 return *this;
69 }
70 //-----------------------------------------------------------------------------
71 RS_SETTINGS::RS_SETTINGS()
72     : sendPeriod(0),
73       port(0),
74       errorStr(),
75       netRouters(),
76       userParams(),
77       password(),
78       subnetFile()
79 {
80 }
81 //-----------------------------------------------------------------------------
82 int RS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
83 {
84 int p;
85 PARAM_VALUE pv;
86 vector<PARAM_VALUE>::const_iterator pvi;
87 netRouters.clear();
88 ///////////////////////////
89 pv.param = "Port";
90 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
91 if (pvi == s.moduleParams.end())
92     {
93     errorStr = "Parameter \'Port\' not found.";
94     printfd(__FILE__, "Parameter 'Port' not found\n");
95     return -1;
96     }
97 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
98     {
99     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
100     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
101     return -1;
102     }
103 port = p;
104 ///////////////////////////
105 pv.param = "SendPeriod";
106 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
107 if (pvi == s.moduleParams.end())
108     {
109     errorStr = "Parameter \'SendPeriod\' not found.";
110     printfd(__FILE__, "Parameter 'SendPeriod' not found\n");
111     return -1;
112     }
113
114 if (ParseIntInRange(pvi->value[0], 5, 600, &sendPeriod))
115     {
116     errorStr = "Cannot parse parameter \'SendPeriod\': " + errorStr;
117     printfd(__FILE__, "Cannot parse parameter 'SendPeriod'\n");
118     return -1;
119     }
120 ///////////////////////////
121 pv.param = "UserParams";
122 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
123 if (pvi == s.moduleParams.end())
124     {
125     errorStr = "Parameter \'UserParams\' not found.";
126     printfd(__FILE__, "Parameter 'UserParams' not found\n");
127     return -1;
128     }
129 userParams = pvi->value;
130 ///////////////////////////
131 pv.param = "Password";
132 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
133 if (pvi == s.moduleParams.end())
134     {
135     errorStr = "Parameter \'Password\' not found.";
136     printfd(__FILE__, "Parameter 'Password' not found\n");
137     return -1;
138     }
139 password = pvi->value[0];
140 ///////////////////////////
141 pv.param = "SubnetFile";
142 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
143 if (pvi == s.moduleParams.end())
144     {
145     errorStr = "Parameter \'SubnetFile\' not found.";
146     printfd(__FILE__, "Parameter 'SubnetFile' not found\n");
147     return -1;
148     }
149 subnetFile = pvi->value[0];
150
151 NRMapParser nrMapParser;
152
153 if (!nrMapParser.ReadFile(subnetFile))
154     {
155     netRouters = nrMapParser.GetMap();
156     }
157 else
158     {
159     GetStgLogger()("mod_rscript: error opening subnets file '%s'", subnetFile.c_str());
160     }
161
162 return 0;
163 }
164 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
167 REMOTE_SCRIPT::REMOTE_SCRIPT()
168     : ctx(),
169       afterChgIPNotifierList(),
170       authorizedUsers(),
171       errorStr(),
172       rsSettings(),
173       settings(),
174       sendPeriod(15),
175       halfPeriod(8),
176       nonstop(false),
177       isRunning(false),
178       users(NULL),
179       netRouters(),
180       thread(),
181       mutex(),
182       sock(0),
183       onAddUserNotifier(*this),
184       onDelUserNotifier(*this),
185       logger(GetPluginLogger(GetStgLogger(), "rscript"))
186 {
187 pthread_mutex_init(&mutex, NULL);
188 }
189 //-----------------------------------------------------------------------------
190 REMOTE_SCRIPT::~REMOTE_SCRIPT()
191 {
192 pthread_mutex_destroy(&mutex);
193 }
194 //-----------------------------------------------------------------------------
195 void * REMOTE_SCRIPT::Run(void * d)
196 {
197 sigset_t signalSet;
198 sigfillset(&signalSet);
199 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
200
201 REMOTE_SCRIPT * rs = static_cast<REMOTE_SCRIPT *>(d);
202
203 rs->isRunning = true;
204
205 while (rs->nonstop)
206     {
207     rs->PeriodicSend();
208     sleep(2);
209     }
210
211 rs->isRunning = false;
212 return NULL;
213 }
214 //-----------------------------------------------------------------------------
215 int REMOTE_SCRIPT::ParseSettings()
216 {
217 int ret = rsSettings.ParseSettings(settings);
218 if (ret)
219     errorStr = rsSettings.GetStrError();
220
221 sendPeriod = rsSettings.GetSendPeriod();
222 halfPeriod = sendPeriod / 2;
223
224 return ret;
225 }
226 //-----------------------------------------------------------------------------
227 int REMOTE_SCRIPT::Start()
228 {
229 netRouters = rsSettings.GetSubnetsMap();
230
231 InitEncrypt(&ctx, rsSettings.GetPassword());
232
233 //onAddUserNotifier.SetRemoteScript(this);
234 //onDelUserNotifier.SetRemoteScript(this);
235
236 users->AddNotifierUserAdd(&onAddUserNotifier);
237 users->AddNotifierUserDel(&onDelUserNotifier);
238
239 nonstop = true;
240
241 if (GetUsers())
242     {
243     return -1;
244     }
245
246 if (PrepareNet())
247     {
248     return -1;
249     }
250
251 if (!isRunning)
252     {
253     if (pthread_create(&thread, NULL, Run, this))
254         {
255         errorStr = "Cannot create thread.";
256         printfd(__FILE__, "Cannot create thread\n");
257         return -1;
258         }
259     }
260
261 errorStr = "";
262 return 0;
263 }
264 //-----------------------------------------------------------------------------
265 int REMOTE_SCRIPT::Stop()
266 {
267 if (!IsRunning())
268     return 0;
269
270 nonstop = false;
271
272 std::for_each(
273         authorizedUsers.begin(),
274         authorizedUsers.end(),
275         DisconnectUser(*this)
276         );
277
278 FinalizeNet();
279
280 if (isRunning)
281     {
282     //5 seconds to thread stops itself
283     for (int i = 0; i < 25 && isRunning; i++)
284         {
285         struct timespec ts = {0, 200000000};
286         nanosleep(&ts, NULL);
287         }
288     }
289
290 users->DelNotifierUserDel(&onDelUserNotifier);
291 users->DelNotifierUserAdd(&onAddUserNotifier);
292
293 if (isRunning)
294     return -1;
295
296 return 0;
297 }
298 //-----------------------------------------------------------------------------
299 int REMOTE_SCRIPT::Reload()
300 {
301 NRMapParser nrMapParser;
302
303 if (nrMapParser.ReadFile(rsSettings.GetMapFileName()))
304     {
305     errorStr = nrMapParser.GetErrorStr();
306     return -1;
307     }
308
309     {
310     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
311
312     printfd(__FILE__, "REMOTE_SCRIPT::Reload()\n");
313
314     netRouters = nrMapParser.GetMap();
315     }
316
317 std::for_each(authorizedUsers.begin(),
318               authorizedUsers.end(),
319               UpdateRouter(*this));
320
321 return 0;
322 }
323 //-----------------------------------------------------------------------------
324 bool REMOTE_SCRIPT::PrepareNet()
325 {
326 sock = socket(AF_INET, SOCK_DGRAM, 0);
327
328 if (sock < 0)
329     {
330     errorStr = "Cannot create socket.";
331     printfd(__FILE__, "Cannot create socket\n");
332     return true;
333     }
334
335 return false;
336 }
337 //-----------------------------------------------------------------------------
338 bool REMOTE_SCRIPT::FinalizeNet()
339 {
340 close(sock);
341 return false;
342 }
343 //-----------------------------------------------------------------------------
344 void REMOTE_SCRIPT::PeriodicSend()
345 {
346 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
347
348 map<uint32_t, RS_USER>::iterator it(authorizedUsers.begin());
349 while (it != authorizedUsers.end())
350     {
351     if (difftime(stgTime, it->second.lastSentTime) - (rand() % halfPeriod) > sendPeriod)
352     //if (stgTime - it->second.lastSentTime > sendPeriod)
353         {
354         Send(it->first, it->second);
355         }
356     ++it;
357     }
358 }
359 //-----------------------------------------------------------------------------
360 #ifdef NDEBUG
361 bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t, uint32_t ip, RS_USER & rsu, bool forceDisconnect) const
362 #else
363 bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t bufSize, uint32_t ip, RS_USER & rsu, bool forceDisconnect) const
364 #endif
365 {
366 RS_PACKET_HEADER packetHead;
367
368 memset(packetHead.padding, 0, sizeof(packetHead.padding));
369 strcpy((char*)packetHead.magic, RS_ID);
370 packetHead.protoVer[0] = '0';
371 packetHead.protoVer[1] = '2';
372 if (forceDisconnect)
373     {
374     packetHead.packetType = RS_DISCONNECT_PACKET;
375     }
376 else
377     {
378     if (rsu.shortPacketsCount % MAX_SHORT_PCKT == 0)
379         {
380         //SendLong
381         packetHead.packetType = rsu.user->IsInetable() ? RS_CONNECT_PACKET : RS_DISCONNECT_PACKET;
382         }
383     else
384         {
385         //SendShort
386         packetHead.packetType = rsu.user->IsInetable() ? RS_ALIVE_PACKET : RS_DISCONNECT_PACKET;
387         }
388     }
389 rsu.shortPacketsCount++;
390 rsu.lastSentTime = stgTime;
391
392 packetHead.ip = htonl(ip);
393 packetHead.id = htonl(rsu.user->GetID());
394 strncpy((char*)packetHead.login, rsu.user->GetLogin().c_str(), RS_LOGIN_LEN);
395 packetHead.login[RS_LOGIN_LEN - 1] = 0;
396
397 memcpy(buf, &packetHead, sizeof(packetHead));
398
399 if (packetHead.packetType == RS_ALIVE_PACKET)
400     {
401     return false;
402     }
403
404 RS_PACKET_TAIL packetTail;
405
406 memset(packetTail.padding, 0, sizeof(packetTail.padding));
407 strcpy((char*)packetTail.magic, RS_ID);
408 vector<string>::const_iterator it;
409 std::string params;
410 for(it = rsSettings.GetUserParams().begin();
411     it != rsSettings.GetUserParams().end();
412     ++it)
413     {
414     std::string parameter(GetUserParam(rsu.user, *it));
415     if (params.length() + parameter.length() > RS_PARAMS_LEN - 1)
416         break;
417     params += parameter + " ";
418     }
419 strncpy((char *)packetTail.params, params.c_str(), RS_PARAMS_LEN);
420 packetTail.params[RS_PARAMS_LEN - 1] = 0;
421
422 assert(sizeof(packetHead) + sizeof(packetTail) <= bufSize && "Insufficient buffer space");
423
424 Encrypt(&ctx, buf + sizeof(packetHead), (char *)&packetTail, sizeof(packetTail) / 8);
425
426 return false;
427 }
428 //-----------------------------------------------------------------------------
429 bool REMOTE_SCRIPT::Send(uint32_t ip, RS_USER & rsu, bool forceDisconnect) const
430 {
431 char buffer[RS_MAX_PACKET_LEN];
432
433 memset(buffer, 0, sizeof(buffer));
434
435 if (PreparePacket(buffer, sizeof(buffer), ip, rsu, forceDisconnect))
436     {
437     printfd(__FILE__, "REMOTE_SCRIPT::Send() - Invalid packet length!\n");
438     return true;
439     }
440
441 std::for_each(
442         rsu.routers.begin(),
443         rsu.routers.end(),
444         PacketSender(sock, buffer, sizeof(buffer), htons(rsSettings.GetPort()))
445         );
446
447 return false;
448 }
449 //-----------------------------------------------------------------------------
450 bool REMOTE_SCRIPT::SendDirect(uint32_t ip, RS_USER & rsu, uint32_t routerIP, bool forceDisconnect) const
451 {
452 char buffer[RS_MAX_PACKET_LEN];
453
454 if (PreparePacket(buffer, sizeof(buffer), ip, rsu, forceDisconnect))
455     {
456     printfd(__FILE__, "REMOTE_SCRIPT::SendDirect() - Invalid packet length!\n");
457     return true;
458     }
459
460 struct sockaddr_in sendAddr;
461
462 sendAddr.sin_family = AF_INET;
463 sendAddr.sin_port = htons(rsSettings.GetPort());
464 sendAddr.sin_addr.s_addr = routerIP;
465
466 int res = sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&sendAddr, sizeof(sendAddr));
467
468 return (res != sizeof(buffer));
469 }
470 //-----------------------------------------------------------------------------
471 bool REMOTE_SCRIPT::GetUsers()
472 {
473 USER_PTR u;
474
475 int h = users->OpenSearch();
476 if (!h)
477     {
478     errorStr = "users->OpenSearch() error.";
479     printfd(__FILE__, "OpenSearch() error\n");
480     return true;
481     }
482
483 while (!users->SearchNext(h, &u))
484     {
485     SetUserNotifier(u);
486     }
487
488 users->CloseSearch(h);
489 return false;
490 }
491 //-----------------------------------------------------------------------------
492 void REMOTE_SCRIPT::ChangedIP(USER_PTR u, uint32_t oldIP, uint32_t newIP)
493 {
494 /*
495  * When ip changes process looks like:
496  * old => 0, 0 => new
497  *
498  */
499 if (newIP)
500     {
501     RS_USER rsu(IP2Routers(newIP), u);
502     Send(newIP, rsu);
503
504     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
505     authorizedUsers[newIP] = rsu;
506     }
507 else
508     {
509     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
510     const map<uint32_t, RS_USER>::iterator it(
511             authorizedUsers.find(oldIP)
512             );
513     if (it != authorizedUsers.end())
514         {
515         Send(oldIP, it->second, true);
516         authorizedUsers.erase(it);
517         }
518     }
519 }
520 //-----------------------------------------------------------------------------
521 std::vector<uint32_t> REMOTE_SCRIPT::IP2Routers(uint32_t ip)
522 {
523 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
524 for (size_t i = 0; i < netRouters.size(); ++i)
525     {
526     if ((ip & netRouters[i].subnetMask) == (netRouters[i].subnetIP & netRouters[i].subnetMask))
527         {
528         return netRouters[i].routers;
529         }
530     }
531 return std::vector<uint32_t>();
532 }
533 //-----------------------------------------------------------------------------
534 string REMOTE_SCRIPT::GetUserParam(USER_PTR u, const string & paramName) const
535 {
536 string value = "";
537 if (strcasecmp(paramName.c_str(), "cash") == 0)
538     strprintf(&value, "%f", u->GetProperty().cash.Get());
539 else
540 if (strcasecmp(paramName.c_str(), "freeMb") == 0)
541     strprintf(&value, "%f", u->GetProperty().freeMb.Get());
542 else
543 if (strcasecmp(paramName.c_str(), "passive") == 0)
544     strprintf(&value, "%d", u->GetProperty().passive.Get());
545 else
546 if (strcasecmp(paramName.c_str(), "disabled") == 0)
547     strprintf(&value, "%d", u->GetProperty().disabled.Get());
548 else
549 if (strcasecmp(paramName.c_str(), "alwaysOnline") == 0)
550     strprintf(&value, "%d", u->GetProperty().alwaysOnline.Get());
551 else
552 if (strcasecmp(paramName.c_str(), "tariffName") == 0 ||
553     strcasecmp(paramName.c_str(), "tariff") == 0)
554     value = "\"" + u->GetProperty().tariffName.Get() + "\"";
555 else
556 if (strcasecmp(paramName.c_str(), "nextTariff") == 0)
557     value = "\"" + u->GetProperty().nextTariff.Get() + "\"";
558 else
559 if (strcasecmp(paramName.c_str(), "address") == 0)
560     value = "\"" + u->GetProperty().address.Get() + "\"";
561 else
562 if (strcasecmp(paramName.c_str(), "note") == 0)
563     value = "\"" + u->GetProperty().note.Get() + "\"";
564 else
565 if (strcasecmp(paramName.c_str(), "group") == 0)
566     value = "\"" + u->GetProperty().group.Get() + "\"";
567 else
568 if (strcasecmp(paramName.c_str(), "email") == 0)
569     value = "\"" + u->GetProperty().email.Get() + "\"";
570 else
571 if (strcasecmp(paramName.c_str(), "realName") == 0)
572     value = "\"" + u->GetProperty().realName.Get() + "\"";
573 else
574 if (strcasecmp(paramName.c_str(), "credit") == 0)
575     strprintf(&value, "%f", u->GetProperty().credit.Get());
576 else
577 if (strcasecmp(paramName.c_str(), "userdata0") == 0)
578     value = "\"" + u->GetProperty().userdata0.Get() + "\"";
579 else
580 if (strcasecmp(paramName.c_str(), "userdata1") == 0)
581     value = "\"" + u->GetProperty().userdata1.Get() + "\"";
582 else
583 if (strcasecmp(paramName.c_str(), "userdata2") == 0)
584     value = "\"" + u->GetProperty().userdata2.Get() + "\"";
585 else
586 if (strcasecmp(paramName.c_str(), "userdata3") == 0)
587     value = "\"" + u->GetProperty().userdata3.Get() + "\"";
588 else
589 if (strcasecmp(paramName.c_str(), "userdata4") == 0)
590     value = "\"" + u->GetProperty().userdata4.Get() + "\"";
591 else
592 if (strcasecmp(paramName.c_str(), "userdata5") == 0)
593     value = "\"" + u->GetProperty().userdata5.Get() + "\"";
594 else
595 if (strcasecmp(paramName.c_str(), "userdata6") == 0)
596     value = "\"" + u->GetProperty().userdata6.Get() + "\"";
597 else
598 if (strcasecmp(paramName.c_str(), "userdata7") == 0)
599     value = "\"" + u->GetProperty().userdata7.Get() + "\"";
600 else
601 if (strcasecmp(paramName.c_str(), "userdata8") == 0)
602     value = "\"" + u->GetProperty().userdata8.Get() + "\"";
603 else
604 if (strcasecmp(paramName.c_str(), "userdata9") == 0)
605     value = "\"" + u->GetProperty().userdata9.Get() + "\"";
606 else
607 if (strcasecmp(paramName.c_str(), "enabledDirs") == 0)
608     value = u->GetEnabledDirs();
609 else
610     printfd(__FILE__, "Unknown value name: %s\n", paramName.c_str());
611 return value;
612 }
613 //-----------------------------------------------------------------------------
614 void REMOTE_SCRIPT::SetUserNotifier(USER_PTR u)
615 {
616 RS_CHG_AFTER_NOTIFIER<uint32_t> afterChgIPNotifier(*this, u);
617
618 afterChgIPNotifierList.push_front(afterChgIPNotifier);
619
620 u->AddCurrIPAfterNotifier(&(*afterChgIPNotifierList.begin()));
621 }
622 //-----------------------------------------------------------------------------
623 void REMOTE_SCRIPT::UnSetUserNotifier(USER_PTR u)
624 {
625 list<RS_CHG_AFTER_NOTIFIER<uint32_t> >::iterator  ipAIter;
626 std::list<list<RS_CHG_AFTER_NOTIFIER<uint32_t> >::iterator> toErase;
627
628 for (ipAIter = afterChgIPNotifierList.begin(); ipAIter != afterChgIPNotifierList.end(); ++ipAIter)
629     {
630     if (ipAIter->GetUser() == u)
631         {
632         u->DelCurrIPAfterNotifier(&(*ipAIter));
633         toErase.push_back(ipAIter);
634         }
635     }
636
637 std::list<list<RS_CHG_AFTER_NOTIFIER<uint32_t> >::iterator>::iterator eIter;
638
639 for (eIter = toErase.begin(); eIter != toErase.end(); ++eIter)
640     {
641     afterChgIPNotifierList.erase(*eIter);
642     }
643 }
644 //-----------------------------------------------------------------------------
645 template <typename varParamType>
646 void RS_CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType & oldValue, const varParamType & newValue)
647 {
648 rs.ChangedIP(user, oldValue, newValue);
649 }
650 //-----------------------------------------------------------------------------
651 void REMOTE_SCRIPT::InitEncrypt(BLOWFISH_CTX * ctx, const string & password) const
652 {
653 unsigned char keyL[PASSWD_LEN];  // Пароль для шифровки
654 memset(keyL, 0, PASSWD_LEN);
655 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
656 Blowfish_Init(ctx, keyL, PASSWD_LEN);
657 }
658 //-----------------------------------------------------------------------------
659 void REMOTE_SCRIPT::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, size_t len8) const
660 {
661 if (dst != src)
662     memcpy(dst, src, len8 * 8);
663 for (size_t i = 0; i < len8; ++i)
664     Blowfish_Encrypt(ctx, (uint32_t *)(dst + i * 8), (uint32_t *)(dst + i * 8 + 4));
665 }
666 //-----------------------------------------------------------------------------