diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Buffer.cc | 2 | ||||
| -rw-r--r-- | lib/Exceptions.cc | 2 | ||||
| -rw-r--r-- | lib/HttpServ.cc | 6 | ||||
| -rw-r--r-- | lib/TaskMan.cc | 254 | ||||
| -rw-r--r-- | lib/Variables.cc | 2 | 
5 files changed, 145 insertions, 121 deletions
diff --git a/lib/Buffer.cc b/lib/Buffer.cc index 83369bd..905090e 100644 --- a/lib/Buffer.cc +++ b/lib/Buffer.cc @@ -14,7 +14,7 @@ Buffer::Buffer(const Buffer & b) : Handle(-1), buffer(0), zero(b.zero), realsiz(      memcpy(buffer, b.buffer, bufsiz);  } -ssize_t Buffer::write(const void *buf, size_t count) { +ssize_t Buffer::write(const void *buf, size_t count) throw (GeneralException) {      if (!count) {  	return 0;      } diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc index 7c9d318..fa9c6da 100644 --- a/lib/Exceptions.cc +++ b/lib/Exceptions.cc @@ -30,6 +30,8 @@ GeneralException::~GeneralException() {      free(msg);  } +TaskNotFound::TaskNotFound() : GeneralException("Task not found") { } +  char * GeneralException::GetMsg() {      return msg;  } diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index e1ddd73..8824bcc 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -15,7 +15,7 @@ class ProcessRequest : public Task {        virtual ~ProcessRequest() {}      virtual String GetName();    protected: -    virtual int Do(); +    virtual int Do() throw (GeneralException);    private:      String GetMime(const String &);      bool ParseUri(String &, String &, String &, Handle *); @@ -45,7 +45,7 @@ String ProcessRequest::GetName() {      return "Processing HTTP request";  } -int ProcessRequest::Do() { +int ProcessRequest::Do() throw(GeneralException) {      switch (current) {      case 0:          if (!s.IsConnected()) return TASK_DONE; @@ -450,7 +450,7 @@ HttpServ::~HttpServ(void) {      Listener.close();  } -int HttpServ::Do() { +int HttpServ::Do() throw (GeneralException) {      try {  	Socket s = Listener.Accept();  	s.SetNonBlock(); diff --git a/lib/TaskMan.cc b/lib/TaskMan.cc index a348ab0..e75863b 100644 --- a/lib/TaskMan.cc +++ b/lib/TaskMan.cc @@ -51,11 +51,12 @@ void taskman_sighole(int sig) {  int TaskMan::GotChild(pid_t pid, int status) {      int r = 0; +    unsigned int i; -    for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) { -	if (p->pr == pid) { -	    p->flag = true; -	    p->status = status; +    for (i = 0; i < w4pr.size(); i++) { +	if (w4pr[i].pr == pid) { +	    w4pr[i].flag = true; +	    w4pr[i].status = status;  	    r = 1;  	}      } @@ -115,53 +116,64 @@ void TaskMan::AddTask(Task * t) {      }  } -vector<Task *>::iterator TaskMan::FindTask(Task * t) { +vector<Task *>::iterator TaskMan::FindTask(Task * t) throw (GeneralException) {      if (!inited) {  	Init();      } -    for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) { +    if (TaskList.empty()) +	throw TaskNotFound(); +     +    for (vector<Task *>::iterator p = TaskList.begin(); p != TaskList.end(); p++) {  	if (*p == t) {  	    return p;  	}      } -    return NULL; +    throw TaskNotFound();  }  void TaskMan::RemoveFromWatches(Task * t) { -    for (vector<w4ha_t>::iterator p = w4ha.begin(); p && (p != w4ha.end()); p++) { -	if (p->T == t) { -	    w4ha.erase(p); -	    p--; +    if (!w4ha.empty()) { +	for (vector<w4ha_t>::iterator p = w4ha.begin(); p != w4ha.end(); p++) { +	    if (p->T == t) { +		w4ha.erase(p); +		p--; +	    }  	}      } -    for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) { -	if (p->T == t) { -	    w4pr.erase(p); -	    p--; +    if (!w4pr.empty()) { +	for (vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) { +	    if (p->T == t) { +		w4pr.erase(p); +		p--; +	    }  	}      } -    for (vector<w4to_t>::iterator p = w4to.begin(); p && (p != w4to.end()); p++) { -	if (p->T == t) { -	    w4to.erase(p); -	    p--; +    if (!w4to.empty()) { +	for (vector<w4to_t>::iterator p = w4to.begin(); p != w4to.end(); p++) { +	    if (p->T == t) { +		w4to.erase(p); +		p--; +	    }  	}      } -    for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) { -	if ((*p)->WaitedBy() == t) { -	    Zombies.push_back(*p); -	    (*p)->RemoveFromWatches(); -	    TaskList.erase(p); -	    number--; -	    p--; -	} else if ((*p) == t) { -	    TaskList.erase(p); -	    number--; -	    p--; +    if (!TaskList.empty()) { +	for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) { +	    if ((*p)->WaitedBy() == t) { +		Zombies.push_back(*p); +	        (*p)->RemoveFromWatches(); +		TaskList.erase(p); +		number--; +	        p--; +	    } else if ((*p) == t) { +		TaskList.erase(p); +	        number--; +		p--; +	    }  	}      }  } @@ -173,12 +185,14 @@ void TaskMan::WaitFor(Handle * h, Task * t, int flags) {  void TaskMan::WaitFor(pid_t pid, Task * t, int status) {      if (status == -1) { -	for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) { -	    if (p->pr == pid) { -		p->T = t; -		p->flag = true; -		got_sigchild++; -		return; +	if (!w4pr.empty()) { +	    for (vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) { +		if (p->pr == pid) { +		    p->T = t; +		    p->flag = true; +		    got_sigchild++; +		    return; +		}  	    }  	}      } @@ -208,9 +222,11 @@ void TaskMan::MainLoop() throw (GeneralException) {  	if (stopped) return;  //	cerr << "-=- TaskMan: begin main loop with " << number << " task to manage.\n"; -	for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) { -	    Task * t = *p; -//	    cerr << "-=- TaskMan: task " << t->GetName() << endl; +	if (!TaskList.empty()) { +	    for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) { +		Task * t = *p; +//	 	   cerr << "-=- TaskMan: task " << t->GetName() << endl; +	    }  	}  //	cerr << "-=- TaskMan: processing burning tasks.\n"; @@ -220,26 +236,28 @@ void TaskMan::MainLoop() throw (GeneralException) {  	    no_burst = 1;              /* First, we will check for any burning task and run 'em */  	    event = E_BURST; -	    for (TaskList_t::iterator p = TaskList.begin(); p && (p != TaskList.end()); p++) { -		Task * t = *p; +	    if (!TaskList.empty()) { +		for (TaskList_t::iterator p = TaskList.begin(); p != TaskList.end(); p++) { +		    Task * t = *p; -		if (t->IsStopped()) { -		    continue; -		} +		    if (t->IsStopped()) { +			continue; +		    } -		if (t->GetState() == TASK_BURST) { -//		    cerr << "-=- TaskMan: running burning task " << t->GetName() << endl; -		    t->Run(); -		    /* if the task added some new tasks, we have to rerun the loop */ -		    no_burst = 0; -		    break; -		} +		    if (t->GetState() == TASK_BURST) { +//		    	cerr << "-=- TaskMan: running burning task " << t->GetName() << endl; +			t->Run(); +		        /* if the task added some new tasks, we have to rerun the loop */ +			no_burst = 0; +		        break; +		    } -		if (t->GetState() == TASK_DONE) { -		    TaskList.erase(p); -		    number--; -		    p--; -		    Zombies.push_back(t); +		    if (t->GetState() == TASK_DONE) { +			TaskList.erase(p); +		        number--; +			p--; +		        Zombies.push_back(t); +		    }  		}  	    }  	} @@ -264,25 +282,27 @@ void TaskMan::MainLoop() throw (GeneralException) {  #ifdef USE_POLL  	    ufsd = (struct pollfd *) malloc(nfds * sizeof(struct pollfd)); -	    for (q = ufsd, p = w4ha.begin(); p && (p != w4ha.end()); p++, q++) { -		p->dirthy = false; -		if (p->T->IsStopped()) { -		    q->fd = 0; -		    q->events = 0; -		} else { -		    if (p->ha->CanWatch()) { -			q->fd = p->ha->GetHandle(); -			q->events = (p->flags & W4_READING ? POLLIN : 0) | (p->flags & W4_WRITING ? POLLOUT : 0); -		    } else { -			p->T->SetBurst(); -			no_burst = 0; -			p->dirthy = true; -			if (!(p->flags & W4_STICKY)) { -			    w4ha.erase(p); -			    p--; -			} +	    if (!w4ha.empty()) { +		for (q = ufsd, p = w4ha.begin(); p != w4ha.end(); p++, q++) { +		    p->dirthy = false; +		    if (p->T->IsStopped()) {  			q->fd = 0;  			q->events = 0; +		    } else { +			if (p->ha->CanWatch()) { +		    	    q->fd = p->ha->GetHandle(); +			    q->events = (p->flags & W4_READING ? POLLIN : 0) | (p->flags & W4_WRITING ? POLLOUT : 0); +			} else { +			    p->T->SetBurst(); +			    no_burst = 0; +			    p->dirthy = true; +			    if (!(p->flags & W4_STICKY)) { +				w4ha.erase(p); +			        p--; +			    } +			    q->fd = 0; +			    q->events = 0; +			}  		    }  		}  	    } @@ -329,13 +349,13 @@ void TaskMan::MainLoop() throw (GeneralException) {  			throw GeneralException(String(_("Error with poll, handle ")) + q->fd + _(" invalid."));  		    } -		    if (q->revents & POLLERR) { +//		    if (q->revents & POLLERR) {  //			cerr << _("Error condition with poll, handle ") << q->fd << endl; -		    } +//		    } -		    if (q->revents & POLLHUP) { +//		    if (q->revents & POLLHUP) {  //			cerr << _("Handle ") << q->fd << _(" hung up.\n"); -		    } +//		    }  		    fd = q->fd;  		    if (q->revents & (POLLIN | POLLOUT | POLLERR | POLLHUP)) { @@ -348,33 +368,36 @@ void TaskMan::MainLoop() throw (GeneralException) {  #endif  			// We have to look into the handle structure now...  			bool touched; -			for (vector<w4ha_t>::iterator p = w4ha.begin(); p && (p != w4ha.end()); p = touched ? w4ha.begin() : p + 1) { -			    touched = false; -			    if ((p->ha->GetHandle() == fd) && (!p->T->IsStopped()) && (p->T->GetState() != TASK_DONE) && (!p->dirthy)) { -				// We've got one, launch it. -//				cerr << "-=- TaskMan: launching task " << p->T->GetName() << " for handle " << p->ha->GetHandle() << endl; -				w4ha_t w4 = *p; -				p->dirthy = true; - -				if (!(p->flags & W4_STICKY)) { -				    w4ha.erase(p); -				} +			if (!w4ha.empty()) { +			    for (vector<w4ha_t>::iterator p = w4ha.begin(); p != w4ha.end(); p = touched ? w4ha.begin() : p + 1) { +				touched = false; +			        if ((p->ha->GetHandle() == fd) && (!p->T->IsStopped()) && (p->T->GetState() != TASK_DONE) && (!p->dirthy)) { +			    	    // We've got one, launch it. +//				    cerr << "-=- TaskMan: launching task " << p->T->GetName() << " for handle " << p->ha->GetHandle() << endl; +				    w4ha_t w4 = *p; +				    p->dirthy = true; + +				    if (!(p->flags & W4_STICKY)) { +					w4ha.erase(p); +				    } -				touched = true; +				    touched = true; -				ehandle = p->ha; -				w4.T->Run(); +				    ehandle = p->ha; +				    w4.T->Run(); -				if (w4.T->GetState() == TASK_DONE) { -				    // This task died, remove it. -				    TaskList_t::iterator q = FindTask(w4.T); -				    if (q) { -					TaskList.erase(q); -					number--; -					Zombies.push_back(w4.T); +				    if (w4.T->GetState() == TASK_DONE) { +					// This task died, remove it. +				        try { +				    	    vector<Task *>::iterator q = FindTask(w4.T); +					    TaskList.erase(q); +					    number--; +					    Zombies.push_back(w4.T); +					} +					catch (TaskNotFound) { +					}  				    }  				} -				  			    }  			}  		    } @@ -399,7 +422,7 @@ void TaskMan::MainLoop() throw (GeneralException) {  		Task * t = Zombies[0], * o;  		if (!t) { -		    cerr << "!?!?!? We have t = NULL ?!?!?! WTF\n"; +//		    cerr << "!?!?!? We have t = NULL ?!?!?! WTF\n";  		    break;  		} @@ -410,9 +433,6 @@ void TaskMan::MainLoop() throw (GeneralException) {  		    if (o->GetState() == TASK_DONE) {  			TaskList_t::iterator f = FindTask(o); -			if (!f) { -			    throw GeneralException(_("TaskMan: internal error (task not found) -- SHOULD NOT HAPPEN!!")); -			}  			TaskList.erase(f);  			number--;  			Zombies.push_back(o); @@ -431,20 +451,22 @@ void TaskMan::MainLoop() throw (GeneralException) {  //	cerr << "-=- TaskMan: processing child-waiting tasks.\n";  	if (got_sigchild) { -	    for (vector<w4pr_t>::iterator p = w4pr.begin(); p && (p != w4pr.end()); p++) { -		if (p->flag) { -		    Task * t; -		    if (p->T->IsStopped()) { -			continue; +	    if (!w4pr.empty()) { +		for (vector<w4pr_t>::iterator p = w4pr.begin(); p != w4pr.end(); p++) { +		    if (p->flag) { +			Task * t; +		        if (p->T->IsStopped()) { +		    	    continue; +			} +			eprocess = p->pr; +			estatus = p->status; +//		        cerr << "-=- TaskMan: running task " << p->T->GetName() << " for process " << p->pr << " (" << p->status << ")\n"; +			t = p->T; +			w4pr.erase(p); +			got_sigchild--; +			t->Run(); +		        break;  		    } -		    eprocess = p->pr; -		    estatus = p->status; -//		    cerr << "-=- TaskMan: running task " << p->T->GetName() << " for process " << p->pr << " (" << p->status << ")\n"; -		    t = p->T; -		    w4pr.erase(p); -		    got_sigchild--; -		    t->Run(); -		    break;  		}  	    }  	} diff --git a/lib/Variables.cc b/lib/Variables.cc index d96cad8..8ba5374 100644 --- a/lib/Variables.cc +++ b/lib/Variables.cc @@ -67,7 +67,7 @@ void Variables::Add(const String & s) {  void Variables::Del(int i) {      nbvars--; -    Vars.erase(&Vars[i], &Vars[i]); +    Vars.erase(Vars.begin() + i, Vars.begin() + i);  }  void Variables::Del(const String & name) {  | 
