Permalink
Browse files

pauzowanie serwera

  • Loading branch information...
1 parent 24c3bd8 commit e8164948f9743cb9fb0b3f68e7a529782de712be @widlok widlok committed Jun 6, 2012
Showing with 39 additions and 10 deletions.
  1. +1 −1 Makefile
  2. +2 −0 Skunk.hpp
  3. +22 −6 csgi.cpp
  4. +4 −2 csgi.hpp
  5. +10 −1 skunk_test.cpp
View
@@ -1,5 +1,5 @@
CXX?=clang++
-CFLAGS=-Wall -Wextra -pedantic -ggdb
+CFLAGS=-Wall -Wextra -pedantic -ggdb -std=c++0x
LDFLAGS=-lssl -lcrypto -lpthread
COMPILE= $(CXX) $(CFLAGS)
View
@@ -228,6 +228,8 @@ class Server : CSGI::Application {
void run(bool);
CSGI::Response get(CSGI::Env&, std::string&);
virtual CSGI::Response operator()(CSGI::Env&);
+ void pause(){srv->pause();}
+ void unpause(){srv->unpause();}
};
}
View
@@ -8,7 +8,14 @@ void *run_thread(void *arg)
srv->serve();
return NULL;
}
-
+void CSGI::Server::pause(){
+ std::cout<<"lock w pause"<<std::endl;
+ pthread_mutex_lock(&pause_);
+}
+void CSGI::Server::unpause(){
+ std::cout<<"unlock w pause"<<std::endl;
+ pthread_mutex_unlock(&pause_);
+}
void CSGI::Server::run(bool async)
{
int i;
@@ -74,19 +81,25 @@ void CSGI::Server::run(bool async)
*/
void CSGI::Server::serve()
{
- int newfd;
+ int newfd, err;
+ CSGI::Env env;
- for (;;) {
+ for (;;pthread_mutex_unlock(&pause_)) {
+ std::cout<<"lock serve"<<std::endl;
+ pthread_mutex_lock(&pause_);
+ std::cout<<"udalo sie"<<std::endl;
if (stop_) {
//std::cerr << "serve() stopping" << std::endl;
return;
}
SSL *ssl = SSL_new(ssl_ctx_);
// so we check stop_
- if (!can_read(sockfd_)) continue;
+ if (!can_read(sockfd_))continue;
+ std::cout<<"accept serve"<<std::endl;
newfd = accept(sockfd_, 0, 0);
+ std::cout<<"udal sie accept"<<std::endl;
SSL_set_fd(ssl, newfd);
- int err = SSL_accept(ssl);
+ err = SSL_accept(ssl);
if (err != 1) {
std::string res;
res.append("HTTP/1.1 400 Bad Request\r\n\r\n");
@@ -97,7 +110,6 @@ void CSGI::Server::serve()
close(newfd);
continue;
}
- CSGI::Env env;
try {
env = parse_request(ssl);
} catch (CSGI::InvalidRequest&) {
@@ -106,10 +118,14 @@ void CSGI::Server::serve()
close(newfd);
continue;
}
+ {
Response resp = (*app_)(env);
send_response(resp, ssl);
close(newfd);
SSL_free(ssl);
+ std::cout<<"unlock serve"<<std::endl;
+ }
+
}
}
View
@@ -90,7 +90,7 @@ class Server {
Server(Application * app, int port) : app_(app), port_(port)
{
backlog_ = 10;
-
+ pause_ = PTHREAD_MUTEX_INITIALIZER;
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
@@ -136,6 +136,8 @@ class Server {
*/
void run(bool async);
void serve();
+ void pause();
+ void unpause();
private: // udokumentowane w csgi.cpp
Env parse_request(SSL*);
void send_response(Response&, SSL*);
@@ -145,7 +147,7 @@ class Server {
int port_;
int backlog_;
int stop_;
-
+ pthread_mutex_t pause_;
pthread_t *worker_;
SSL_METHOD *ssl_method_;
SSL_CTX *ssl_ctx_;
View
@@ -75,5 +75,14 @@ int main(void) {
srv->setAuth(auth);
- srv->run(false);
+ srv->run(true);
+ for(;;){
+ srv->pause();
+ std::cout<<"pause"<<std::endl;
+ sleep(5);
+ srv->unpause();
+ std::cout<<"unpause"<<std::endl;
+ sleep(5);
+ }
+
}

0 comments on commit e816494

Please sign in to comment.