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