aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs
blob: 4440db1b16a1ba990dad166900e124917cf871eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSim Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using libsecondlife;
using libsecondlife.Packets;
using log4net;
using Mono.Addins;
using Nwc.XmlRpc;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Region.ClientStack;
using OpenSim.Region.ClientStack.LindenUDP;
using OpenSim.Region.Environment.Scenes;

[assembly : Addin]
[assembly : AddinDependency("OpenSim", "0.5")]
[assembly : AddinDependency("RegionProxy", "0.1")]

namespace OpenSim.ApplicationPlugins.LoadBalancer
{
    [Extension("/OpenSim/Startup")]
    public class LoadBalancerPlugin : IApplicationPlugin
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private BaseHttpServer commandServer;
        private bool[] isLocalNeighbour;
        private bool isSplit;
        private TcpServer mTcpServer;
        private readonly object padlock = new object();

        private int proxyOffset;
        private string proxyURL;
        private List<RegionInfo> regionData;
        private int[] regionPortList;
        private SceneManager sceneManager;
        private string[] sceneURL;
        private string serializeDir;
        private OpenSimMain simMain;
        private TcpClient[] tcpClientList;
        private List<IClientNetworkServer> m_clientServers;

        #region IApplicationPlugin Members

        public void Initialise(OpenSimMain openSim)
        {
            m_log.Info("[BALANCER] " + "Entering Initialize()");

            proxyURL = openSim.ConfigSource.Configs["Network"].GetString("proxy_url", "");
            if (proxyURL.Length == 0) return;

            StartTcpServer();

            LLClientView.SynchronizeClient = SynchronizePackets;
            AsynchronousSocketListener.PacketHandler = SynchronizePacketRecieve;

            sceneManager = openSim.SceneManager;
            m_clientServers = openSim.ClientServers;
            regionData = openSim.RegionData;
            simMain = openSim;
            commandServer = openSim.HttpServer;

            proxyOffset = Int32.Parse(openSim.ConfigSource.Configs["Network"].GetString("proxy_offset", "0"));
            serializeDir = openSim.ConfigSource.Configs["Network"].GetString("serialize_dir", "/tmp/");

            commandServer.AddXmlRPCHandler("SerializeRegion", SerializeRegion);
            commandServer.AddXmlRPCHandler("DeserializeRegion_Move", DeserializeRegion_Move);
            commandServer.AddXmlRPCHandler("DeserializeRegion_Clone", DeserializeRegion_Clone);
            commandServer.AddXmlRPCHandler("TerminateRegion", TerminateRegion);

            commandServer.AddXmlRPCHandler("SplitRegion", SplitRegion);
            commandServer.AddXmlRPCHandler("MergeRegions", MergeRegions);
            commandServer.AddXmlRPCHandler("UpdatePhysics", UpdatePhysics);
            commandServer.AddXmlRPCHandler("GetStatus", GetStatus);

            m_log.Info("[BALANCER] " + "Exiting Initialize()");
        }

        public void Close()
        {
        }

        #endregion

        private void StartTcpServer()
        {
            Thread server_thread = new Thread(new ThreadStart(
                                                  delegate
                                                      {
                                                          mTcpServer = new TcpServer(10001);
                                                          mTcpServer.start();
                                                      }));
            server_thread.Start();
        }

        private XmlRpcResponse GetStatus(XmlRpcRequest request)
        {
            XmlRpcResponse response = new XmlRpcResponse();
            try
            {
                m_log.Info("[BALANCER] " + "Entering RegionStatus()");

                int src_port = (int) request.Params[0];
                Scene scene;
                // try to get the scene object
                RegionInfo src_region = SearchRegionFromPortNum(src_port);
                if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false)
                {
                    m_log.Error("[BALANCER] " + "The Scene is not found");
                    return response;
                }
                // serialization of client's informations
                List<ScenePresence> presences = scene.GetScenePresences();
                int get_scene_presence = presences.Count;
                int get_scene_presence_filter = 0;
                foreach (ScenePresence pre in presences)
                {
                    IClientAPI client = pre.ControllingClient;
                    //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) {
                    if (client.IsActive)
                    {
                        get_scene_presence_filter++;
                    }
                }
                List<ScenePresence> avatars = scene.GetAvatars();
                int get_avatar = avatars.Count;
                int get_avatar_filter = 0;
                string avatar_names = "";
                foreach (ScenePresence pre in avatars)
                {
                    IClientAPI client = pre.ControllingClient;
                    //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) {
                    if (client.IsActive)
                    {
                        get_avatar_filter++;
                        avatar_names += pre.Firstname + " " + pre.Lastname + "; ";
                    }
                }

                Hashtable responseData = new Hashtable();
                responseData["get_scene_presence_filter"] = get_scene_presence_filter;
                responseData["get_scene_presence"] = get_scene_presence;
                responseData["get_avatar_filter"] = get_avatar_filter;
                responseData["get_avatar"] = get_avatar;
                responseData["avatar_names"] = avatar_names;
                response.Value = responseData;

                m_log.Info("[BALANCER] " + "Exiting RegionStatus()");
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
            }
            return response;
        }

        private XmlRpcResponse SerializeRegion(XmlRpcRequest request)
        {
            try
            {
                m_log.Info("[BALANCER] " + "Entering SerializeRegion()");

                string src_url = (string) request.Params[0];
                int src_port = (int) request.Params[1];

                SerializeRegion(src_url, src_port);

                m_log.Info("[BALANCER] " + "Exiting SerializeRegion()");
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
            }

            return new XmlRpcResponse();
        }

        private XmlRpcResponse DeserializeRegion_Move(XmlRpcRequest request)
        {
            try
            {
                m_log.Info("[BALANCER] " + "Entering DeserializeRegion_Move()");

                string src_url = (string) request.Params[0];
                int src_port = (int) request.Params[1];
                string dst_url = (string) request.Params[2];
                int dst_port = (int) request.Params[3];

                DeserializeRegion_Move(src_port, dst_port, src_url, dst_url);

                m_log.Info("[BALANCER] " + "Exiting DeserializeRegion_Move()");
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
            }

            return new XmlRpcResponse();
        }

        private XmlRpcResponse DeserializeRegion_Clone(XmlRpcRequest request)
        {
            try
            {
                m_log.Info("[BALANCER] " + "Entering DeserializeRegion_Clone()");

                string src_url = (string) request.Params[0];
                int src_port = (int) request.Params[1];
                string dst_url = (string) request.Params[2];
                int dst_port = (int) request.Params[3];

                DeserializeRegion_Clone(src_port, dst_port, src_url, dst_url);

                m_log.Info("[BALANCER] " + "Exiting DeserializeRegion_Clone()");
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
                throw;
            }

            return new XmlRpcResponse();
        }

        private XmlRpcResponse TerminateRegion(XmlRpcRequest request)
        {
            try
            {
                m_log.Info("[BALANCER] " + "Entering TerminateRegion()");

                int src_port = (int) request.Params[0];

                // backgroud
                WaitCallback callback = TerminateRegion;
                ThreadPool.QueueUserWorkItem(callback, src_port);

                m_log.Info("[BALANCER] " + "Exiting TerminateRegion()");
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
            }

            return new XmlRpcResponse();
        }

        // internal functions

        private void SerializeRegion(string src_url, int src_port)
        {
            //------------------------------------------
            // Processing of origin region
            //------------------------------------------

            // search origin region
            RegionInfo src_region = SearchRegionFromPortNum(src_port);

            if (src_region == null)
            {
                m_log.Error("[BALANCER] " + "Region not found");
                return;
            }

            Util.XmlRpcCommand(src_region.proxyUrl, "BlockClientMessages", src_url, src_port + proxyOffset);

            // serialization of origin region's data
            SerializeRegion(src_region, serializeDir);
        }

        private void DeserializeRegion_Move(int src_port, int dst_port, string src_url, string dst_url)
        {
            //------------------------------------------
            // Processing of destination region
            //------------------------------------------

            // import the source region's data
            RegionInfo dst_region = DeserializeRegion(dst_port, serializeDir);

            Util.XmlRpcCommand(dst_region.proxyUrl, "ChangeRegion", src_port + proxyOffset, src_url, dst_port + proxyOffset, dst_url);
            Util.XmlRpcCommand(dst_region.proxyUrl, "UnblockClientMessages", dst_url, dst_port + proxyOffset);
        }

        private void DeserializeRegion_Clone(int src_port, int dst_port, string src_url, string dst_url)
        {
            //------------------------------------------
            // Processing of destination region
            //------------------------------------------

            // import the source region's data
            RegionInfo dst_region = DeserializeRegion(dst_port, serializeDir);

            // Decide who is in charge for each section
            int[] port = new int[] {src_port, dst_port};
            string[] url = new string[] {"http://" + src_url + ":" + commandServer.Port, "http://" + dst_url + ":" + commandServer.Port};
            for (int i = 0; i < 2; i++) Util.XmlRpcCommand(url[i], "SplitRegion", i, 2, port[0], port[1], url[0], url[1]);

            // Enable the proxy
            Util.XmlRpcCommand(dst_region.proxyUrl, "AddRegion", src_port + proxyOffset, src_url, dst_port + proxyOffset, dst_url);
            Util.XmlRpcCommand(dst_region.proxyUrl, "UnblockClientMessages", dst_url, dst_port + proxyOffset);
        }

        private void TerminateRegion(object param)
        {
            int src_port = (int) param;

            //------------------------------------------
            // Processing of remove region
            //------------------------------------------

            // search origin region
            RegionInfo src_region = SearchRegionFromPortNum(src_port);

            if (src_region == null)
            {
                m_log.Error("[BALANCER] " + "Region not found");
                return;
            }

            isSplit = false;

            // remove client resources
            RemoveAllClientResource(src_region);
            // remove old region
            RemoveRegion(src_region.RegionID, src_region.InternalEndPoint.Port);

            m_log.Info("[BALANCER] " + "Region terminated");
        }

        private RegionInfo SearchRegionFromPortNum(int portnum)
        {
            RegionInfo result = null;

            foreach (RegionInfo rinfo in regionData)
            {
                if (rinfo.InternalEndPoint.Port == portnum)
                {
//                    m_log.Info("BALANCER",
//                            "Region found. Internal Port = {0}, Handle={1}",
//                            rinfo.InternalEndPoint.Port, rinfo.RegionHandle);
                    result = rinfo;
                    break;
                }
            }

            return result;
        }

        private IClientNetworkServer SearchClientServerFromPortNum(int portnum)
        {
            return m_clientServers.Find(
                delegate(IClientNetworkServer server)
                    {
// ReSharper disable PossibleNullReferenceException
                        return (portnum + proxyOffset == ((IPEndPoint) server.Server.LocalEndPoint).Port);
// ReSharper restore PossibleNullReferenceException
                    }
                );
        }

        private void SerializeRegion(RegionInfo src_region, string export_dir)
        {
            Scene scene;
            int i = 0;

            // try to get the scene object
            if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false)
            {
                m_log.Error("[BALANCER] " + "The Scene is not found");
                return;
            }

            // create export directory
            DirectoryInfo dirinfo = new DirectoryInfo(export_dir);
            if (!dirinfo.Exists)
            {
                dirinfo.Create();
            }

            // serialization of client's informations
            List<ScenePresence> presences = scene.GetScenePresences();

            foreach (ScenePresence pre in presences)
            {
                SerializeClient(i, scene, pre, export_dir);
                i++;
            }

            // serialization of region data
            SearializableRegionInfo dst_region = new SearializableRegionInfo(src_region);

            string filename = export_dir + "RegionInfo_" + src_region.RegionID + ".bin";
            Util.SerializeToFile(filename, dst_region);

            // backup current scene's entities
            //scene.Backup();

            m_log.InfoFormat("[BALANCER] " + "region serialization completed [{0}]",
                             src_region.RegionID.ToString());
        }

        private static void SerializeClient(int idx, IScene scene, EntityBase pre, string export_dir)
        {
            string filename;
            IClientAPI controller;

            m_log.InfoFormat("[BALANCER] " + "agent id : {0}", pre.UUID);

            uint[] circuits = scene.ClientManager.GetAllCircuits(pre.UUID);

            foreach (uint code in circuits)
            {
                m_log.InfoFormat("[BALANCER] " + "circuit code : {0}", code);

                if (scene.ClientManager.TryGetClient(code, out controller))
                {
                    ClientInfo info = controller.GetClientInfo();

                    filename = export_dir + "ClientInfo-" + String.Format("{0:0000}", idx) + "_" + controller.CircuitCode + ".bin";

                    Util.SerializeToFile(filename, info);

                    m_log.InfoFormat("[BALANCER] " + "client info serialized [filename={0}]", filename);
                }
            }

            //filename = export_dir + "Presence_" + controller.AgentId.ToString() + ".bin";
            filename = export_dir + "Presence_" + String.Format("{0:0000}", idx) + ".bin";

            Util.SerializeToFile(filename, pre);

            m_log.InfoFormat("[BALANCER] " + "scene presence serialized [filename={0}]", filename);
        }

        private RegionInfo DeserializeRegion(int dst_port, string import_dir)
        {
            RegionInfo dst_region = null;

            try
            {
                // deserialization of region data
                string[] files = Directory.GetFiles(import_dir, "RegionInfo_*.bin");

                foreach (string filename in files)
                {
                    m_log.InfoFormat("[BALANCER] RegionInfo filename = [{0}]", filename);

                    dst_region = new RegionInfo((SearializableRegionInfo) Util.DeserializeFromFile(filename));

                    m_log.InfoFormat("[BALANCER] " + "RegionID = [{0}]", dst_region.RegionID.ToString());
                    m_log.InfoFormat("[BALANCER] " + "RegionHandle = [{0}]", dst_region.RegionHandle);
                    m_log.InfoFormat("[BALANCER] " + "ProxyUrl = [{0}]", dst_region.proxyUrl);
                    m_log.InfoFormat("[BALANCER] " + "OriginRegionID = [{0}]", dst_region.originRegionID.ToString());

                    CreateCloneRegion(dst_region, dst_port, true);

                    File.Delete(filename);

                    m_log.InfoFormat("[BALANCER] " + "region deserialized [{0}]", dst_region.RegionID);
                }

                if (dst_region != null)
                {
                    // deserialization of client data
                    DeserializeClient(dst_region, import_dir);

                    m_log.InfoFormat("[BALANCER] " + "region deserialization completed [{0}]",
                                     dst_region.ToString());
                }
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
                throw;
            }

            return dst_region;
        }

        private void DeserializeClient(SimpleRegionInfo dst_region, string import_dir)
        {
            ScenePresence sp;
            ClientInfo data;
            Scene scene;
            IClientAPI controller;

            if (sceneManager.TryGetScene(dst_region.RegionID, out scene))
            {
                IClientNetworkServer clientserv = SearchClientServerFromPortNum(scene.RegionInfo.InternalEndPoint.Port);

                // restore the scene presence
                for (int i = 0;; i++)
                {
                    string filename = import_dir + "Presence_" + String.Format("{0:0000}", i) + ".bin";

                    if (!File.Exists(filename))
                    {
                        break;
                    }

                    sp = (ScenePresence) Util.DeserializeFromFile(filename);
                    Console.WriteLine("agent id = {0}", sp.UUID);

                    scene.m_restorePresences.Add(sp.UUID, sp);
                    File.Delete(filename);

                    m_log.InfoFormat("[BALANCER] " + "scene presence deserialized [{0}]", sp.UUID);

                    // restore the ClientView

                    string[] files = Directory.GetFiles(import_dir, "ClientInfo-" + String.Format("{0:0000}", i) + "_*.bin");

                    foreach (string fname in files)
                    {
                        int start = fname.IndexOf('_');
                        int end = fname.LastIndexOf('.');
                        uint circuit_code = uint.Parse(fname.Substring(start + 1, end - start - 1));
                        m_log.InfoFormat("[BALANCER] " + "client circuit code = {0}", circuit_code);

                        data = (ClientInfo) Util.DeserializeFromFile(fname);

                        AgentCircuitData agentdata = new AgentCircuitData(data.agentcircuit);
                        scene.AuthenticateHandler.AddNewCircuit(circuit_code, agentdata);

                        // TODO: This needs to be abstracted and converted into IClientNetworkServer
                        if (clientserv is LLUDPServer)
                        {
                            ((LLUDPServer) clientserv).RestoreClient(agentdata, data.userEP, data.proxyEP);
                        }

                        // waiting for the scene-presense restored
                        lock (scene.m_restorePresences)
                        {
                            Monitor.Wait(scene.m_restorePresences, 3000);
                        }

                        if (scene.ClientManager.TryGetClient(circuit_code, out controller))
                        {
                            m_log.InfoFormat("[BALANCER] " + "get client [{0}]", circuit_code);
                            controller.SetClientInfo(data);
                        }

                        File.Delete(fname);

                        m_log.InfoFormat("[BALANCER] " + "client info deserialized [{0}]", circuit_code);
                    }

                    // backup new scene's entities
                    //scene.Backup();
                }
            }
        }

        private void CreateCloneRegion(RegionInfo dst_region, int dst_port, bool createID_flag)
        {
            if (createID_flag)
            {
                dst_region.RegionID = LLUUID.Random();
            }

            // change RegionInfo (memory only)
            dst_region.InternalEndPoint.Port = dst_port;
            dst_region.ExternalHostName = proxyURL.Split(new char[] {'/', ':'})[3];

            // Create new region
            simMain.CreateRegion(dst_region, false);
        }

        private void RemoveRegion(LLUUID regionID, int port)
        {
            Scene killScene;
            if (sceneManager.TryGetScene(regionID, out killScene))
            {
                Console.WriteLine("scene found.");

                if ((sceneManager.CurrentScene != null)
                    && (sceneManager.CurrentScene.RegionInfo.RegionID == killScene.RegionInfo.RegionID))
                {
                    sceneManager.TrySetCurrentScene("..");
                }

                m_log.Info("Removing region : " + killScene.RegionInfo.RegionName);
                regionData.Remove(killScene.RegionInfo);
                sceneManager.CloseScene(killScene);
            }

            // Shutting down the UDP server
            IClientNetworkServer clientsvr = SearchClientServerFromPortNum(port);

            if (clientsvr != null)
            {
                clientsvr.Server.Close();
                m_clientServers.Remove(clientsvr);
            }
        }

        private void RemoveAllClientResource(SimpleRegionInfo src_region)
        {
            Scene scene;
            IClientAPI controller;

            // try to get the scene object
            if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false)
            {
                m_log.Error("[BALANCER] " + "The Scene is not found");
                return;
            }

            // serialization of client's informations
            List<ScenePresence> presences = scene.GetScenePresences();

            // remove all scene presences
            foreach (ScenePresence pre in presences)
            {
                uint[] circuits = scene.ClientManager.GetAllCircuits(pre.UUID);

                foreach (uint code in circuits)
                {
                    m_log.InfoFormat("[BALANCER] " + "circuit code : {0}", code);

                    if (scene.ClientManager.TryGetClient(code, out controller))
                    {
                        // stopping clientview thread
                        if ((controller).IsActive)
                        {
                            controller.Stop();
                            (controller).IsActive = false;
                        }
                        // teminateing clientview thread
                        controller.Terminate();
                        m_log.Info("[BALANCER] " + "client thread stopped");
                    }
                }

                // remove scene presence
                scene.RemoveClient(pre.UUID);
            }
        }

        /*
         * This section implements scene splitting and synchronization
         */

        private XmlRpcResponse SplitRegion(XmlRpcRequest request)
        {
            try
            {
                int myID = (int) request.Params[0];
                int numRegions = (int) request.Params[1];
                regionPortList = new int[numRegions];
                sceneURL = new string[numRegions];
                tcpClientList = new TcpClient[numRegions];

                for (int i = 0; i < numRegions; i++)
                {
                    regionPortList[i] = (int) request.Params[i + 2];
                    sceneURL[i] = (string) request.Params[i + 2 + numRegions];
                }

                for (int i = 0; i < numRegions; i++)
                {
                    string hostname = sceneURL[i].Split(new char[] {'/', ':'})[3];
                    m_log.InfoFormat("[SPLITSCENE] " + "creating tcp client host:{0}", hostname);
                    tcpClientList[i] = new TcpClient(hostname, 10001);
                }

                bool isMaster = (myID == 0);

                isLocalNeighbour = new bool[numRegions];
                for (int i = 0; i < numRegions; i++) isLocalNeighbour[i] = (sceneURL[i] == sceneURL[myID]);

                RegionInfo region = SearchRegionFromPortNum(regionPortList[myID]);

                //Console.WriteLine("\n === SplitRegion {0}\n", region.RegionID);

                Scene scene;
                if (sceneManager.TryGetScene(region.RegionID, out scene))
                {
                    // Disable event updates, backups etc in the slave(s)
                    scene.Region_Status = isMaster ? RegionStatus.Up : RegionStatus.SlaveScene;

                    //Console.WriteLine("=== SplitRegion {0}: Scene found, status {1}", region.RegionID, scene.Region_Status);

                    // Disabling half of the avatars in master, and the other half in slave

                    int i = 0;

                    List<uint> circuits = scene.ClientManager.GetAllCircuitCodes();
                    circuits.Sort();

                    foreach (uint code in circuits)
                    {
                        m_log.InfoFormat("[BALANCER] " + "circuit code : {0}", code);

                        IClientAPI controller;
                        if (scene.ClientManager.TryGetClient(code, out controller))
                        {
                            // Divide the presences evenly over the set of subscenes
                            LLClientView client = (LLClientView) controller;
                            client.IsActive = (((i + myID) % sceneURL.Length) == 0);

                            m_log.InfoFormat("[SPLITSCENE] === SplitRegion {0}: SP.PacketEnabled {1}", region.RegionID, client.IsActive);

                            if (!client.IsActive)
                            {
                                // stopping clientview thread
                                client.Stop();
                            }

                            ++i;
                        }
                    }

                    scene.splitID = myID;
                    scene.SynchronizeScene = SynchronizeScenes;
                    isSplit = true;
                }
                else
                {
                    m_log.Error("[SPLITSCENE] " + String.Format("Scene not found {0}", region.RegionID));
                }
            }
            catch (Exception e)
            {
                m_log.Error("[SPLITSCENE] " + e);
                m_log.Error("[SPLITSCENE] " + e.StackTrace);
            }

            return new XmlRpcResponse();
        }

        private XmlRpcResponse MergeRegions(XmlRpcRequest request)
        {
            // This should only be called for the master scene
            try
            {
                m_log.Info("[BALANCER] " + "Entering MergeRegions()");

                string src_url = (string) request.Params[0];
                int src_port = (int) request.Params[1];

                RegionInfo region = SearchRegionFromPortNum(src_port);

                Util.XmlRpcCommand(region.proxyUrl, "BlockClientMessages", src_url, src_port + proxyOffset);

                Scene scene;
                if (sceneManager.TryGetScene(region.RegionID, out scene))
                {
                    isSplit = false;

                    scene.SynchronizeScene = null;
                    scene.Region_Status = RegionStatus.Up;

                    List<ScenePresence> presences = scene.GetScenePresences();
                    foreach (ScenePresence pre in presences)
                    {
                        LLClientView client = (LLClientView) pre.ControllingClient;
                        if (!client.IsActive)
                        {
                            client.Restart();
                            client.IsActive = true;
                        }
                    }
                }

                // Delete the slave scenes
                for (int i = 1; i < sceneURL.Length; i++)
                {
                    string url = (sceneURL[i].Split('/')[2]).Split(':')[0]; // get URL part from EP
                    Util.XmlRpcCommand(region.proxyUrl, "DeleteRegion", regionPortList[i] + proxyOffset, url);
                    Thread.Sleep(1000);
                    Util.XmlRpcCommand(sceneURL[i], "TerminateRegion", regionPortList[i]); // TODO: need + proxyOffset?
                }

                Util.XmlRpcCommand(region.proxyUrl, "UnblockClientMessages", src_url, src_port + proxyOffset);
            }
            catch (Exception e)
            {
                m_log.Error("[BALANCER] " + e);
                m_log.Error("[BALANCER] " + e.StackTrace);
                throw;
            }

            return new XmlRpcResponse();
        }

        private XmlRpcResponse UpdatePhysics(XmlRpcRequest request)
        {
            // this callback receives physic scene updates from the other sub-scenes (in split mode)

            int regionPort = (int) request.Params[0];
            LLUUID scenePresenceID = new LLUUID((byte[]) request.Params[1], 0);
            LLVector3 position = new LLVector3((byte[]) request.Params[2], 0);
            LLVector3 velocity = new LLVector3((byte[]) request.Params[3], 0);
            bool flying = (bool) request.Params[4];

            LocalUpdatePhysics(regionPort, scenePresenceID, position, velocity, flying);

            return new XmlRpcResponse();
        }

        private void LocalUpdatePhysics(int regionPort, LLUUID scenePresenceID, LLVector3 position, LLVector3 velocity, bool flying)
        {
            //m_log.Info("[SPLITSCENE] "+String.Format("UpdatePhysics called {0}", regionID));

            //m_log.Info("[SPLITSCENE] "+"LocalUpdatePhysics [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", 
            //                                       regionPort, scenePresenceID.ToString(), position.ToString(), 
            //                                       velocity.ToString(), flying);

            RegionInfo region = SearchRegionFromPortNum(regionPort);

            // Find and update the scene precense
            Scene scene;
            if (sceneManager.TryGetScene(region.RegionID, out scene))
            {
                ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == scenePresenceID; });

                if (pre == null)
                {
                    m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePhysics] ScenePresence is missing... ({0})", scenePresenceID.ToString());
                    return;
                }

//                m_log.Info("[SPLITSCENE] "+"LocalUpdatePhysics [region:{0}, client:{1}]", 
//                                                                             regionID.ToString(), pre.UUID.ToString());

                pre.AbsolutePosition = position; // will set PhysicsActor.Position
                pre.Velocity = velocity; // will set PhysicsActor.Velocity
                pre.PhysicsActor.Flying = flying;
            }
        }

        private void SynchronizeScenes(Scene scene)
        {
            if (!isSplit)
            {
                return;
            }

            lock (padlock)
            {
                // Callback activated after a physics scene update
//                int i = 0;
                List<ScenePresence> presences = scene.GetScenePresences();
                foreach (ScenePresence pre in presences)
                {
                    LLClientView client = (LLClientView) pre.ControllingClient;

                    // Because data changes by the physics simulation when the client doesn't move, 
                    // if MovementFlag is false, It is necessary to synchronize.
                    //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) 
                    if (client.IsActive)
                    {
                        //m_log.Info("[SPLITSCENE] "+String.Format("Client moving in {0} {1}", scene.RegionInfo.RegionID, pre.AbsolutePosition));

                        for (int i = 0; i < sceneURL.Length; i++)
                        {
                            if (i == scene.splitID)
                            {
                                continue;
                            }

                            if (isLocalNeighbour[i])
                            {
                                //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Local) [region:{0}=>{1}, client:{2}]", 
                                //                                             scene.RegionInfo.RegionID, regionPortList[i], pre.UUID.ToString());
                                LocalUpdatePhysics(regionPortList[i], pre.UUID, pre.AbsolutePosition, pre.Velocity, pre.PhysicsActor.Flying);
                            }
                            else
                            {
                                //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Remote) [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", 
                                //                                   regionPortList[i], pre.UUID.ToString(), pre.AbsolutePosition.ToString(), 
                                //                                   pre.Velocity.ToString(), pre.PhysicsActor.Flying);


                                Util.XmlRpcCommand(sceneURL[i], "UpdatePhysics",
                                                   regionPortList[i], pre.UUID.GetBytes(),
                                                   pre.AbsolutePosition.GetBytes(), pre.Velocity.GetBytes(),
                                                   pre.PhysicsActor.Flying);

/*
  byte[] buff = new byte[12+12+1];

  Buffer.BlockCopy(pre.AbsolutePosition.GetBytes(), 0, buff, 0, 12); 
  Buffer.BlockCopy(pre.Velocity.GetBytes(), 0, buff, 12, 12); 
  buff[24] = (byte)((pre.PhysicsActor.Flying)?1:0);

  // create header
  InternalPacketHeader header = new InternalPacketHeader();

  header.type = 1;
  header.throttlePacketType = 0;
  header.numbytes = buff.Length;
  header.agent_id = pre.UUID.UUID;
  header.region_port = regionPortList[i];

  //Send
  tcpClientList[i].send(header, buff);
*/
                            }
                        }
                    }
//                    ++i;
                }
            }
        }

        public bool SynchronizePackets(IScene scene, Packet packet, LLUUID agentID, ThrottleOutPacketType throttlePacketType)
        {
            if (!isSplit)
            {
                return false;
            }

            Scene localScene = (Scene) scene;

            for (int i = 0; i < sceneURL.Length; i++)
            {
                if (i == localScene.splitID)
                {
                    continue;
                }

                if (isLocalNeighbour[i])
                {
                    //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Local) [type:{0}, client:{1}]", 
                    //                packet.Type.ToString(), agentID.ToString());
                    LocalUpdatePacket(regionPortList[i], agentID, packet, throttlePacketType);
                }
                else
                {
                    //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Remote) [type:{0}, client:{1}]", 
                    //                            packet.Type.ToString(), agentID.ToString());
                    // to bytes
                    byte[] buff = packet.ToBytes();

                    // create header
                    InternalPacketHeader header = new InternalPacketHeader();

                    header.type = 0;
                    header.throttlePacketType = (int) throttlePacketType;
                    header.numbytes = buff.Length;
                    header.agent_id = agentID.UUID;
                    header.region_port = regionPortList[i];

                    //Send
                    tcpClientList[i].send(header, buff);

                    PacketPool.Instance.ReturnPacket(packet);
                }
            }

            return true;
        }

        private void LocalUpdatePacket(int regionPort, LLUUID agentID, Packet packet, ThrottleOutPacketType throttlePacketType)
        {
            Scene scene;

            RegionInfo region = SearchRegionFromPortNum(regionPort);

//            m_log.Info("[SPLITSCENE] "+"LocalUpdatePacket [region port:{0}, client:{1}, packet type:{2}]", 
//                                                          regionPort, agentID.ToString(), packet.GetType().ToString());

            if (sceneManager.TryGetScene(region.RegionID, out scene))
            {
                ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == agentID; });

                if (pre == null)
                {
                    m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePacket] ScenePresence is missing... ({0})", agentID.ToString());
                    return;
                }

                if (((LLClientView) pre.ControllingClient).IsActive)
                {
                    pre.ControllingClient.OutPacket(packet, throttlePacketType);
                }
                else
                {
                    PacketPool.Instance.ReturnPacket(packet);
                }
            }
        }

        public void SynchronizePacketRecieve(InternalPacketHeader header, byte[] buff)
        {
//            m_log.Info("[SPLITSCENE] "+"entering SynchronizePacketRecieve[type={0}]", header.type);

            if (!isSplit)
            {
                return;
            }

            switch (header.type)
            {
                case 0:

                    byte[] zero = new byte[3000];

                    // deserialize packet
                    int packetEnd = buff.Length - 1;
//                    packetEnd = buff.Length;

                    try
                    {
                        //m_log.Info("[SPLITSCENE] "+"PacketPool.Instance : {0}", (PacketPool.Instance == null)?"null":"not null");
                        //m_log.Info("[SPLITSCENE] "+"buff length={0}", buff.Length);

                        Packet packet = PacketPool.Instance.GetPacket(buff, ref packetEnd, zero);

                        LocalUpdatePacket(header.region_port, new LLUUID(header.agent_id),
                                          packet, (ThrottleOutPacketType) header.throttlePacketType);
                    }
                    catch (Exception e)
                    {
                        m_log.Error("[SPLITSCENE] " + e);
                        m_log.Error("[SPLITSCENE] " + e.StackTrace);
                    }

                    break;

                case 1:

                    int regionPort = header.region_port;
                    LLUUID scenePresenceID = new LLUUID(header.agent_id);
                    LLVector3 position = new LLVector3(buff, 0);
                    LLVector3 velocity = new LLVector3(buff, 12);
                    bool flying = ((buff[24] == 1) ? true : false);

                    LocalUpdatePhysics(regionPort, scenePresenceID, position, velocity, flying);

                    break;

                default:
                    m_log.Info("[SPLITSCENE] " + "Invalid type");
                    break;
            }

//            m_log.Info("[SPLITSCENE] "+"exiting SynchronizePacketRecieve");
        }
    }
}