~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to bludgeon/Abuse.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
using System;
3
 
using System.Collections;
4
 
 
5
 
using Beagle.Util;
6
 
 
7
 
namespace Bludgeon {
8
 
 
9
 
        public class Abuse {
10
 
 
11
 
                DirectoryObject root;
12
 
                EventTracker tracker;
13
 
 
14
 
                IHammer[] hammers;
15
 
                Random random = new Random ();
16
 
                
17
 
                public int    TotalCount = -1; // in iterations
18
 
                public double TotalTime  = -1; // in minutes
19
 
 
20
 
                public int Cycles    = -1;
21
 
                public int MinCycles =  1;
22
 
                public int MaxCycles = -1;
23
 
 
24
 
                // These are delays that are introduced between calls
25
 
                // to HammerOnce.  They are measured in seconds.
26
 
                public double Pause    = -1; 
27
 
                public double MinPause =  0;
28
 
                public double MaxPause = -1;
29
 
 
30
 
                const int    default_count  =  10;
31
 
                const int    default_cycles = 100;
32
 
                const double default_pause  =   0;
33
 
 
34
 
                // This is where we track the state of our current cycle
35
 
                // of abuse.
36
 
                int      count;
37
 
                int      cycles_remaining;
38
 
                DateTime start_time;
39
 
 
40
 
                GLib.IdleHandler idle_handler;
41
 
                GLib.TimeoutHandler timeout_handler;
42
 
                Daemon.VerifiedHandler verified_handler;
43
 
 
44
 
                public Abuse (DirectoryObject root,
45
 
                              EventTracker    tracker,
46
 
                              ICollection     hammers)
47
 
                {
48
 
                        this.root = root;
49
 
                        this.tracker = tracker;
50
 
 
51
 
                        this.hammers = new IHammer [hammers.Count];
52
 
                        int i = 0;
53
 
                        foreach (IHammer hammer in hammers)
54
 
                                this.hammers [i++] = hammer;
55
 
 
56
 
                        idle_handler = new GLib.IdleHandler (AbuseWorker);
57
 
                        timeout_handler = new GLib.TimeoutHandler (RescheduleAbuse);
58
 
                        verified_handler = new Daemon.VerifiedHandler (VerifiedWorker);
59
 
                }
60
 
 
61
 
                public void Run ()
62
 
                {
63
 
                        count = 0;
64
 
                        cycles_remaining = GetCycles ();
65
 
                        start_time = DateTime.Now;
66
 
                        
67
 
                        // We start by verifying the index, to make sure we
68
 
                        // are in a reasonable state.
69
 
                        Daemon.WaitUntilVerified (root, verified_handler);
70
 
                }
71
 
 
72
 
                ///////////////////////////////////////////////////////////////////////
73
 
 
74
 
                private int GetCycles ()
75
 
                {
76
 
                        if (Cycles > 0)
77
 
                                return Cycles;
78
 
                        else if (MaxCycles > MinCycles)
79
 
                                return MinCycles + random.Next (MaxCycles - MinCycles);
80
 
                        return default_cycles;
81
 
                }
82
 
 
83
 
                private int GetPauseInMs ()
84
 
                {
85
 
                        double t = default_pause;
86
 
                        if (Pause >= 0)
87
 
                                t = Pause;
88
 
                        else if (MaxPause > MinPause)
89
 
                                t = MinPause + random.NextDouble () * (MaxPause - MinPause);
90
 
                        return (int) (1000 * t);
91
 
                }
92
 
 
93
 
                private bool AbuseWorker ()
94
 
                {
95
 
                        // Pick a hammer, and use it.
96
 
                        int i;
97
 
                        i = random.Next (hammers.Length);
98
 
                        if (! hammers [i].HammerOnce (root, tracker))
99
 
                                return false;
100
 
 
101
 
                        --cycles_remaining;
102
 
                        if (cycles_remaining == 0) {
103
 
                                cycles_remaining = GetCycles ();
104
 
                                ++count;
105
 
 
106
 
                                // Verify the index
107
 
                                Daemon.WaitUntilVerified (root, verified_handler);
108
 
                                return false;
109
 
                        }
110
 
 
111
 
                        return true;
112
 
                }
113
 
 
114
 
                private bool RescheduleAbuse ()
115
 
                {
116
 
                        Action.Add (idle_handler);
117
 
 
118
 
                        return false;
119
 
                }
120
 
 
121
 
                private void VerifiedWorker (bool index_is_sane)
122
 
                {
123
 
                        // If the index is bad, just return.  The index-checking
124
 
                        // code will generate spew to tell us what went wrong, so
125
 
                        // we don't need to output anything.
126
 
                        if (! index_is_sane)
127
 
                                return;
128
 
                        
129
 
                        // Are we finished yet?
130
 
                        bool finished = false;
131
 
                        if (hammers.Length == 0) {
132
 
                                finished = true;
133
 
                        } else if (TotalTime > 0) {
134
 
                                double t;
135
 
                                t = (DateTime.Now - start_time).TotalSeconds;
136
 
                                finished = (t > 60*TotalTime);
137
 
                        } else {
138
 
                                int target_count;
139
 
                                target_count = TotalCount;
140
 
                                if (target_count < 0)
141
 
                                        target_count = default_count;
142
 
                                finished = (count >= target_count);
143
 
                        }
144
 
                        
145
 
                        // If we aren't finished, schedule some more abuse.
146
 
                        if (! finished) {
147
 
                                if (count == 0)
148
 
                                        Action.Add (idle_handler);
149
 
                                else
150
 
                                        Action.Add ((uint) GetPauseInMs (), timeout_handler);
151
 
                        }
152
 
                }
153
 
        }
154
 
}