summaryrefslogtreecommitdiff
path: root/cs/src/Ice/ConnectionMonitor.cs
blob: 4bfeab6f561c608fcc2f4bfd3684d72da766c921 (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
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

namespace IceInternal
{

    using System.Diagnostics;
    using IceUtilInternal;

    public sealed class ConnectionMonitor : TimerTask
    {
        public void destroy()
        {
            lock(this)
            {
                Debug.Assert(instance_ != null);
                
                instance_ = null;
                _connections = null;
            }
        }
        
        public void add(Ice.ConnectionI connection)
        {
            lock(this)
            {
                Debug.Assert(instance_ != null);
                _connections.Add(connection);
            }
        }
        
        public void remove(Ice.ConnectionI connection)
        {
            lock(this)
            {
                Debug.Assert(instance_ != null);
                _connections.Remove(connection);
            }
        }
        
        //
        // Only for use by Instance.
        //
        internal ConnectionMonitor(Instance instance, int interval)
        {
            Debug.Assert(interval > 0);
            instance_ = instance;
            _connections = new Set();

            instance_.timer().scheduleRepeated(this, interval * 1000);
        }
        
        public void runTimerTask()
        {
            Set connections = new Set();
            lock(this)
            {                    
                if(instance_ == null)
                {
                    return;
                }

                connections.Clear();
                foreach(Ice.ConnectionI connection in _connections)
                {
                    connections.Add(connection);
                }
            }
                
            //
            // Monitor connections outside the thread synchronization,
            // so that connections can be added or removed during
            // monitoring.
            //
            foreach(Ice.ConnectionI connection in connections)
            {
                try
                {
                    connection.monitor();
                }
                catch(System.Exception ex)
                {
                    lock(this)
                    {
                        if(instance_ == null)
                        {
                            return;
                        }
                        instance_.initializationData().logger.error("unknown exception in connection monitor:\n" + ex);
                    }
                }
            }
        }
        
        private Instance instance_;
        private Set _connections;
    }

}