summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/Task.h16
-rw-r--r--include/TaskMan.h5
-rw-r--r--lib/Buffer.cc2
-rw-r--r--lib/CopyJob.cc2
-rw-r--r--lib/Handle.cc2
-rw-r--r--lib/HttpServ.cc10
-rw-r--r--lib/Socket.cc2
-rw-r--r--lib/String.cc2
-rw-r--r--lib/Task.cc34
-rw-r--r--lib/TaskMan.cc75
-rw-r--r--src/Main.cc2
11 files changed, 90 insertions, 62 deletions
diff --git a/include/Task.h b/include/Task.h
index b45bfea..39b414f 100644
--- a/include/Task.h
+++ b/include/Task.h
@@ -23,9 +23,11 @@ class Task : public Base {
int GetState();
void Suspend() throw (GeneralException);
void WaitFor(Handle *, int = 0);
- void WaitFor(Task *, int = 0);
- void WaitFor(pid_t, int = 0);
+ void WaitFor(Task *);
+ void WaitFor(pid_t);
void WaitFor(struct timeval, int = 0);
+ bool WaitingFor(Handle *);
+ bool WaitingFor(pid_t);
void SetBurst();
void SetCleanUp();
bool HasToClean();
@@ -45,18 +47,16 @@ class Task : public Base {
int flags;
};
- class w4ta_t {
+ class wbta_t {
public:
- w4ta_t(Task * ata, int aflags) : ta(ata), flags(aflags) { }
+ wbta_t(Task * ata) : ta(ata) { }
Task * ta;
- int flags;
};
class w4pr_t {
public:
- w4pr_t(pid_t apr, int aflags) : pr(apr), flags(aflags) { }
+ w4pr_t(pid_t apr) : pr(apr) { }
pid_t pr;
- int flags;
};
class w4to_t {
@@ -71,7 +71,7 @@ class Task : public Base {
bool cleanup;
bool suspended;
vector<w4ha_t> w4ha;
- vector<w4ta_t> w4ta;
+ vector<wbta_t> wbta;
vector<w4pr_t> w4pr;
vector<w4to_t> w4to;
};
diff --git a/include/TaskMan.h b/include/TaskMan.h
index 2025e5c..e1c59d0 100644
--- a/include/TaskMan.h
+++ b/include/TaskMan.h
@@ -8,12 +8,13 @@
class TaskMan : public Base {
public:
static int AddTask(Task *);
- static int RemoveTask(Task *);
static void Init() throw (GeneralException);
static void MainLoop() throw (GeneralException);
private:
- static vector<Task *> TaskList;
+ typedef vector<Task *> TaskList_t;
+ static TaskList_t TaskList;
+ static TaskList_t Zombies;
static int number;
static bool inited;
};
diff --git a/lib/Buffer.cc b/lib/Buffer.cc
index 123cc65..b3cb10b 100644
--- a/lib/Buffer.cc
+++ b/lib/Buffer.cc
@@ -65,6 +65,6 @@ Buffer Buffer::operator=(const Buffer & b) {
} else {
buffer = 0;
}
- return *this;
}
+ return *this;
}
diff --git a/lib/CopyJob.cc b/lib/CopyJob.cc
index 55fe59d..3b01092 100644
--- a/lib/CopyJob.cc
+++ b/lib/CopyJob.cc
@@ -1,7 +1,7 @@
#include "CopyJob.h"
#include "General.h"
-CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads) : s(as), d(ad), siz(asiz), ds(ads), cursiz(0), r(0) {
+CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads) : s(as), d(ad), ds(ads), siz(asiz), cursiz(0), r(0) {
WaitFor(s, W4_STICKY);
WaitFor(d, W4_STICKY);
}
diff --git a/lib/Handle.cc b/lib/Handle.cc
index 6f7c6e5..30ad7ef 100644
--- a/lib/Handle.cc
+++ b/lib/Handle.cc
@@ -120,7 +120,7 @@ Handle & operator>>(Handle & h, String & s) {
}
t[i] = '\0';
- s.set("%s", t);
+ s = t;
return h;
}
diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc
index cda572d..c534741 100644
--- a/lib/HttpServ.cc
+++ b/lib/HttpServ.cc
@@ -361,13 +361,17 @@ String ProcessRequest::GetMime(const String & f) {
HttpServ::HttpServ(Action * ap, int port, const String & nname) throw (GeneralException) : p(ap), name(nname), localport(port) {
bool r = true;
+ cerr << "Initialising Mini HTTP-Server on port " << localport << endl;
+
r = Listener.SetLocal("", port);
- if (r) {
- r = Listener.Listen();
+ if (!r) {
+ throw GeneralException("Initialisation of the Mini HTTP-Server failed: can't bind");
}
+ r = Listener.Listen();
+
if (!r) {
- throw GeneralException("Initialisation of the Mini HTTP-Server failed.");
+ throw GeneralException("Initialisation of the Mini HTTP-Server failed: can't listen");
}
Listener.SetNonBlock();
diff --git a/lib/Socket.cc b/lib/Socket.cc
index 5d80783..d3d7c19 100644
--- a/lib/Socket.cc
+++ b/lib/Socket.cc
@@ -104,7 +104,7 @@ bool Socket::Connect(String host, int port) {
bool Socket::Listen(void) {
if (!listening && !connected) {
- if (listen(GetHandle(), 10)) {
+ if (!listen(GetHandle(), 10)) {
listening = true;
}
}
diff --git a/lib/String.cc b/lib/String.cc
index d64f065..d587b75 100644
--- a/lib/String.cc
+++ b/lib/String.cc
@@ -51,7 +51,7 @@ const char * String::set(char * s, ...) {
va_list ap;
va_start(ap, s);
- vsprintf(t, s, ap);
+ vsnprintf(t, BUFSIZ, s, ap);
free(str);
str = ::strdup(t);
va_end(ap);
diff --git a/lib/Task.cc b/lib/Task.cc
index a56a2ff..b54402f 100644
--- a/lib/Task.cc
+++ b/lib/Task.cc
@@ -4,11 +4,11 @@
#include "Task.h"
#include "String.h"
-Task::Task() : state(TASK_ON_HOLD), suspended(false), cleanup(false), current(0) {
+Task::Task() : current(0), state(TASK_ON_HOLD), stopped(false), cleanup(false), suspended(false) {
TaskMan::AddTask(this);
}
+
Task::~Task() {
- TaskMan::RemoveTask(this);
}
int Task::Do() throw (GeneralException) {
@@ -23,7 +23,7 @@ int Task::Run() {
}
catch (TaskSwitch) {
cerr << "Catch a task switching.\n";
- throw;
+ return state;
}
catch (GeneralException e) {
cerr << "Task " << GetName() << " caused an unexpected exception: '" << e.GetMsg() << "', closing it.\n";
@@ -53,18 +53,38 @@ void Task::WaitFor(Handle * h, int flags) {
w4ha.push_back(w4ha_t(h, flags));
}
-void Task::WaitFor(Task * t, int flags) {
- w4ta.push_back(w4ta_t(t, flags));
+void Task::WaitFor(Task * t) {
+ t->wbta.push_back(wbta_t(this));
}
-void Task::WaitFor(pid_t p, int flags) {
- w4pr.push_back(w4pr_t(p, flags));
+void Task::WaitFor(pid_t p) {
+ w4pr.push_back(w4pr_t(p));
}
void Task::WaitFor(timeval t, int flags) {
w4to.push_back(w4to_t(t, flags));
}
+bool Task::WaitingFor(Handle * ha) {
+ vector<w4ha_t>::iterator p;
+
+ for (p = w4ha.begin(); p && (p != w4ha.end()); p++) {
+ if (p->ha == ha) return true;
+ }
+
+ return false;
+}
+
+bool Task::WaitingFor(pid_t pr) {
+ vector<w4pr_t>::iterator p;
+
+ for (p = w4pr.begin(); p && (p != w4pr.end()); p++) {
+ if (p->pr == pr) return true;
+ }
+
+ return false;
+}
+
void Task::SetBurst() {
state = TASK_BURST;
}
diff --git a/lib/TaskMan.cc b/lib/TaskMan.cc
index ded359a..1af9234 100644
--- a/lib/TaskMan.cc
+++ b/lib/TaskMan.cc
@@ -4,6 +4,7 @@
#include "TaskMan.h"
vector<Task *> TaskMan::TaskList;
+vector<Task *> TaskMan::Zombies;
int TaskMan::number = 0;
bool TaskMan::inited = false;
@@ -30,59 +31,61 @@ void TaskMan::Init() throw (GeneralException) {
}
int TaskMan::AddTask(Task * t) {
+ if (!inited) {
+ Init();
+ }
TaskList.push_back(t);
number++;
return 0;
}
-int TaskMan::RemoveTask(Task * t) {
- int i;
+void TaskMan::MainLoop() throw (GeneralException) {
+ TaskList_t::iterator p;
+ Task * t;
- for (i = 0; i < number; i++) {
- if (TaskList[i] == t) {
- TaskList.erase(&TaskList[i]);
- number--;
- return 0;
- }
+ int no_burst;
+
+ if (!inited) {
+ Init();
}
- return -1;
-}
-
-void TaskMan::MainLoop() throw (GeneralException) {
- Task ** p, * t;
while (1) {
+ cerr << "TaskMan: Begin of main loop with " << number << " task to handle.\n";
+
if (number == 0) {
throw GeneralException("TaskMan: No more task to manage.");
}
- p = TaskList.begin();
-
- while (1) {
- t = *p;
-
-#ifdef HAVE_POLL
-#else
-#endif
+ no_burst = 0;
+ while (!no_burst) {
+ no_burst = 1;
+ /* First, we will check for any burning task and run 'em */
+ for (p = TaskList.begin(); p && (p != TaskList.end()); p++) {
+ t = *p;
- try {
- t->Run();
- }
- catch (TaskSwitch) {
- continue;
- }
- if (t->GetState() == TASK_DONE) {
- TaskList.erase(p);
- }
+ if (t->GetState() == TASK_BURST) {
+ t->Run();
+ }
- if (p == TaskList.end()) {
- break;
+ if (t->GetState() == TASK_BURST) {
+ no_burst = 0;
+ } else if (t->GetState() == TASK_DONE) {
+ TaskList.erase(p);
+ number--;
+ p--;
+ if (t->HasToClean()) {
+ delete t;
+ } else {
+ Zombies.push_back(t);
+ }
+ }
}
-
- p++;
}
-
-
}
+
+#ifdef HAVE_POLL
+#else
+#endif
+
}
diff --git a/src/Main.cc b/src/Main.cc
index 9075f5d..2398814 100644
--- a/src/Main.cc
+++ b/src/Main.cc
@@ -124,7 +124,7 @@ int main(int argc, char ** argv) {
TaskMan::MainLoop();
}
catch (GeneralException e) {
- cerr << e.GetMsg() << endl;
+ cerr << "Main function got an exception: '" << e.GetMsg() << "'.\n";
exit(-1);
}