summaryrefslogtreecommitdiff
path: root/modules/extra/m_async_commands.cpp
blob: 814c86c923de1df0f7fc47f8702dae7a715818e1 (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
#include "module.h"
#include "async_commands.h"

static bool ignore_pre_command = false;
static Pipe *me;
static CommandMutex *current_command = NULL;
static std::list<CommandMutex *> commands;
/* Mutex held by the core when it is processing. Used by threads to halt the core */
static Mutex main_mutex;

class AsynchCommandMutex : public CommandMutex
{
 public:
	bool destroy;
	bool started;

	AsynchCommandMutex(CommandSource &s, Command *c, const std::vector<Anope::string> &p) : CommandMutex(s, c, p), destroy(false), started(false)
	{
		commands.push_back(this);

		this->mutex.Lock();
	}

	~AsynchCommandMutex()
	{
		std::list<CommandMutex *>::iterator it = std::find(commands.begin(), commands.end(), this);
		if (it != commands.end())
			commands.erase(it);
		if (this == current_command)
			current_command = NULL;
	}

	void Run()
	{
		this->started = true;

		User *u = this->source.u;
		BotInfo *bi = this->source.owner;
	
		ignore_pre_command = true;
		EventReturn MOD_RESULT;
		FOREACH_RESULT(I_OnPreCommand, OnPreCommand(source, command, params));
		if (MOD_RESULT == EVENT_STOP)
		{
			source.DoReply();
			return;
		}

		if (!this->source.permission.empty() && !u->HasCommand(this->source.permission))
		{
			u->SendMessage(bi, ACCESS_DENIED);
			Log(LOG_COMMAND, "denied", bi) << "Access denied for user " << u->GetMask() << " with command " << command;
		}
		else
		{
			command->Execute(source, params);
			FOREACH_MOD(I_OnPostCommand, OnPostCommand(source, command, params));
			source.DoReply();
		}

		main_mutex.Unlock();
	}

	void Lock()
	{
		if (this->destroy)
			this->Exit();

		this->processing = true;
		me->Notify();
		this->mutex.Lock();

		if (this->destroy)
		{
			this->Unlock();
			this->Exit();
		}
	}

	void Unlock()
	{
		this->processing = false;
		main_mutex.Unlock();
	}
	
	void Destroy()
	{
		this->destroy = true;
	}
};


class ModuleAsynchCommands : public Module, public Pipe, public AsynchCommandsService
{
	bool reset;

	void Reset()
	{
		this->reset = false;

		unsigned count = 0, size = commands.size();
		for (std::list<CommandMutex *>::iterator it = commands.begin(); count < size; ++count, ++it)
		{
			AsynchCommandMutex *cm = debug_cast<AsynchCommandMutex *>(*it);
			cm->Destroy();

			new AsynchCommandMutex(cm->source, cm->command, cm->params);
		}
	}

 public:
	ModuleAsynchCommands(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED), Pipe(), AsynchCommandsService(this, "asynch_commands"), reset(false)
	{
		me = this;

		this->SetPermanent(true);

		main_mutex.Lock();

		Implementation i[] = { I_OnDeleteObject, I_OnPreCommand };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));

		ModuleManager::SetPriority(this, PRIORITY_FIRST);

		ModuleManager::RegisterService(this);
	}
	
	void OnDeleteObject(Base *b)
	{
		for (std::list<CommandMutex *>::iterator it = commands.begin(), it_end = commands.end(); it != it_end; ++it)
		{
			AsynchCommandMutex *cm = debug_cast<AsynchCommandMutex *>(*it);

			if (cm->started && (cm->command == b || cm->source.u == b || cm->source.owner == b || cm->source.service == b))
				cm->Destroy();
		}

		this->reset = true;
	}

	EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> &params)
	{
		if (ignore_pre_command)
		{
			ignore_pre_command = false;
			return EVENT_CONTINUE;
		}
		else if (current_command)
			return EVENT_CONTINUE;
		else if (command->name == "RESTART")
			return EVENT_CONTINUE;

		CommandSource source_copy = source;
		AsynchCommandMutex *cm = new AsynchCommandMutex(source_copy, command, params);

		try
		{
			// Give processing to the command thread
			Log(LOG_DEBUG_2) << "Waiting for command thread " << cm->command->name << " from " << source_copy.u->nick;
			current_command = cm;
			threadEngine.Start(cm);
			main_mutex.Lock();
			current_command = NULL;

			return EVENT_STOP;
		}
		catch (const CoreException &ex)
		{
			delete cm;
			Log() << "Unable to thread for command: " << ex.GetReason();
		}

		return EVENT_CONTINUE;
	}

	void OnNotify()
	{
		for (std::list<CommandMutex *>::iterator it = commands.begin(), it_end = commands.end(); it != it_end;)
		{
			AsynchCommandMutex *cm = debug_cast<AsynchCommandMutex *>(*it++);

			// Thread engine will pick this up later
			if (cm->GetExitState() || !cm->processing)
				continue;
			else if (cm->destroy)
			{
				if (cm->started)
				{
					cm->mutex.Unlock();
					continue;
				}
				else
					delete cm;
			}

			if (this->reset)
			{
				this->Reset();
				return this->OnNotify();
			}

			Log(LOG_DEBUG_2) << "Waiting for command thread " << cm->command->name << " from " << cm->source.u->nick;
			current_command = cm;

			// Unlock to give processing back to the command thread
			if (!cm->started)
			{
				try
				{
					threadEngine.Start(cm);
				}
				catch (const CoreException &)
				{
					delete cm;
					continue;
				}
			}
			else
				cm->mutex.Unlock();
			// Relock to regain processing once the command thread hangs for any reason
			main_mutex.Lock();

			current_command = NULL;
		}
	}

	CommandMutex *CurrentCommand()
	{
		return current_command;
	}
};

MODULE_INIT(ModuleAsynchCommands)