]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/ur_functor.h
Suffix construction and prefix compare methods added to the OID
[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 /*
22  $Revision: 1.3 $
23  $Date: 2010/03/04 12:07:03 $
24  $Author: faust $
25 */
26
27 #ifndef __UR_FUNCTOR_H__
28 #define __UR_FUNCTOR_H__
29
30 #include <functional>
31 #include <algorithm>
32 #include <utility>
33
34 #include "stg/os_int.h"
35 #include "stg/common.h"
36
37 #include "rscript.h"
38
39 class UpdateRouter : public std::unary_function<std::pair<const uint32_t, RS_USER>, void>
40 {
41 public:
42     UpdateRouter(REMOTE_SCRIPT & t)
43         : obj(t) {};
44
45     void operator() (std::pair<const uint32_t, RS_USER> & val)
46         {
47         std::vector<uint32_t> newRouters = obj.IP2Routers(val.first);
48         std::vector<uint32_t>::const_iterator oldIt(val.second.routers.begin());
49         std::vector<uint32_t>::const_iterator newIt(newRouters.begin());
50         val.second.shortPacketsCount = 0;
51         while (oldIt != val.second.routers.end() ||
52                newIt != newRouters.end())
53             {
54             if (oldIt == val.second.routers.end())
55                 {
56                 if (newIt != newRouters.end())
57                     {
58                     obj.SendDirect(val.first, val.second, *newIt); // Connect on new router
59                     ++newIt;
60                     }
61                 }
62             else if (newIt == newRouters.end())
63                 {
64                 //if (oldIt != newRouters.end())
65                     //{ // Already checked it
66                     obj.SendDirect(val.first, val.second, *oldIt, true); // Disconnect on old router
67                     ++oldIt;
68                     //}
69                 } 
70             else if (*oldIt < *newIt)
71                 {
72                 obj.SendDirect(val.first, val.second, *oldIt, true); // Disconnect on old router
73                 ++oldIt;
74                 }
75             else if (*oldIt > *newIt)
76                 {
77                 obj.SendDirect(val.first, val.second, *newIt); // Connect on new router
78                 ++newIt;
79                 }
80             else
81                 {
82                 if (oldIt != val.second.routers.end())
83                     ++oldIt;
84                 if (newIt != newRouters.end())
85                     ++newIt;
86                 }
87             }
88         val.second.routers = newRouters;
89         /*if (val.second.souters != newRouters)
90             {
91             obj.Send(val.first, val.second, true); // Disconnect on old router
92             val.second.routerIP = obj.IP2Router(val.first); // Change router
93             val.second.shortPacketsCount = 0; // Reset packets count (to prevent alive send)
94             obj.Send(val.first, val.second); // Connect on new router
95             }*/
96         }
97 private:
98     REMOTE_SCRIPT & obj;
99 };
100
101 #endif