]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/ur_functor.h
Merge branch 'log-unauth-reasons' into full-month-stats
[stg.git] / projects / stargazer / plugins / other / rscript / ur_functor.h
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 : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __UR_FUNCTOR_H__
22 #define __UR_FUNCTOR_H__
23
24 #include <functional>
25 #include <algorithm>
26 #include <utility>
27
28 #include "stg/os_int.h"
29 #include "stg/common.h"
30
31 #include "rscript.h"
32
33 namespace RS
34 {
35
36 class UpdateRouter : public std::unary_function<std::pair<const uint32_t, RS::USER>, void>
37 {
38 public:
39     UpdateRouter(REMOTE_SCRIPT & t)
40         : obj(t) {}
41
42     void operator() (std::pair<const uint32_t, USER> & val)
43         {
44         std::vector<uint32_t> newRouters = obj.IP2Routers(val.second.ip);
45         std::vector<uint32_t>::const_iterator oldIt(val.second.routers.begin());
46         std::vector<uint32_t>::const_iterator newIt(newRouters.begin());
47         val.second.shortPacketsCount = 0;
48         while (oldIt != val.second.routers.end() ||
49                newIt != newRouters.end())
50             {
51             if (oldIt == val.second.routers.end())
52                 {
53                 if (newIt != newRouters.end())
54                     {
55                     obj.SendDirect(val.second, *newIt); // Connect on new router
56                     ++newIt;
57                     }
58                 }
59             else if (newIt == newRouters.end())
60                 {
61                 obj.SendDirect(val.second, *oldIt, true); // Disconnect on old router
62                 ++oldIt;
63                 } 
64             else if (*oldIt < *newIt)
65                 {
66                 obj.SendDirect(val.second, *oldIt, true); // Disconnect on old router
67                 ++oldIt;
68                 }
69             else if (*oldIt > *newIt)
70                 {
71                 obj.SendDirect(val.second, *newIt); // Connect on new router
72                 ++newIt;
73                 }
74             else
75                 {
76                 if (oldIt != val.second.routers.end())
77                     ++oldIt;
78                 if (newIt != newRouters.end())
79                     ++newIt;
80                 }
81             }
82         val.second.routers = newRouters;
83         }
84 private:
85     REMOTE_SCRIPT & obj;
86 };
87
88 } // namespace RS
89
90 #endif