]> git.stg.codes - stg.git/blob - stargazer/plugins/other/rscript/ur_functor.h
Port to CMake, get rid of os_int.h.
[stg.git] / 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 "rscript.h"
25
26 #include "stg/common.h"
27
28 #include <functional>
29 #include <algorithm>
30 #include <utility>
31 #include <cstdint>
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     explicit 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                 ++oldIt;
77                 if (newIt != newRouters.end())
78                     ++newIt;
79                 }
80             }
81         val.second.routers = newRouters;
82         }
83 private:
84     REMOTE_SCRIPT & obj;
85 };
86
87 } // namespace RS
88
89 #endif