3 using System
.Collections
;
15 Random random
= new Random ();
17 public int TotalCount
= -1; // in iterations
18 public double TotalTime
= -1; // in minutes
20 public int Cycles
= -1;
21 public int MinCycles
= 1;
22 public int MaxCycles
= -1;
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;
30 const int default_count
= 10;
31 const int default_cycles
= 100;
32 const double default_pause
= 0;
34 // This is where we track the state of our current cycle
40 GLib
.IdleHandler idle_handler
;
41 GLib
.TimeoutHandler timeout_handler
;
42 Daemon
.VerifiedHandler verified_handler
;
44 public Abuse (DirectoryObject root
,
49 this.tracker
= tracker
;
51 this.hammers
= new IHammer
[hammers
.Count
];
53 foreach (IHammer hammer
in hammers
)
54 this.hammers
[i
++] = hammer
;
56 idle_handler
= new GLib
.IdleHandler (AbuseWorker
);
57 timeout_handler
= new GLib
.TimeoutHandler (RescheduleAbuse
);
58 verified_handler
= new Daemon
.VerifiedHandler (VerifiedWorker
);
64 cycles_remaining
= GetCycles ();
65 start_time
= DateTime
.Now
;
67 // We start by verifying the index, to make sure we
68 // are in a reasonable state.
69 Daemon
.WaitUntilVerified (root
, verified_handler
);
72 ///////////////////////////////////////////////////////////////////////
74 private int GetCycles ()
78 else if (MaxCycles
> MinCycles
)
79 return MinCycles
+ random
.Next (MaxCycles
- MinCycles
);
80 return default_cycles
;
83 private int GetPauseInMs ()
85 double t
= default_pause
;
88 else if (MaxPause
> MinPause
)
89 t
= MinPause
+ random
.NextDouble () * (MaxPause
- MinPause
);
90 return (int) (1000 * t
);
93 private bool AbuseWorker ()
95 // Pick a hammer, and use it.
97 i
= random
.Next (hammers
.Length
);
98 if (! hammers
[i
].HammerOnce (root
, tracker
))
102 if (cycles_remaining
== 0) {
103 cycles_remaining
= GetCycles ();
107 Daemon
.WaitUntilVerified (root
, verified_handler
);
114 private bool RescheduleAbuse ()
116 Action
.Add (idle_handler
);
121 private void VerifiedWorker (bool index_is_sane
)
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.
129 // Are we finished yet?
130 bool finished
= false;
131 if (hammers
.Length
== 0) {
133 } else if (TotalTime
> 0) {
135 t
= (DateTime
.Now
- start_time
).TotalSeconds
;
136 finished
= (t
> 60*TotalTime
);
139 target_count
= TotalCount
;
140 if (target_count
< 0)
141 target_count
= default_count
;
142 finished
= (count
>= target_count
);
145 // If we aren't finished, schedule some more abuse.
148 Action
.Add (idle_handler
);
150 Action
.Add ((uint) GetPauseInMs (), timeout_handler
);