Skip to content

Commit

Permalink
Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeusz Sośnierz committed Jun 6, 2012
1 parent 357329b commit 15a1214
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CXX?=clang++
CFLAGS=-Wall -Wextra -pedantic -ggdb
LDFLAGS=-lssl -lcrypto
LDFLAGS=-lssl -lcrypto -lpthread
COMPILE= $(CXX) $(CFLAGS)

all: app skunk_test
Expand Down
17 changes: 15 additions & 2 deletions app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
#include <iostream>

class MyApp : public CSGI::Application {
int counter_;
public:
MyApp() : counter_(0) { }

int increment() { return ++counter_; }

virtual CSGI::Response operator()(CSGI::Env& env)
{
CSGI::Response resp;
Expand All @@ -21,6 +26,10 @@ class MyApp : public CSGI::Application {
}

resp.content = "Hello, world!";
resp.content.append(" Counter = ");
std::stringstream counter;
counter << counter_;
resp.content.append(counter.str());

std::stringstream len;
len << resp.content.length();
Expand All @@ -35,14 +44,18 @@ class MyApp : public CSGI::Application {

int main()
{
CSGI::Application *app = new MyApp;
MyApp *app = new MyApp;
CSGI::Server srv(app, 8080);
try {
srv.run(false);
srv.run(true);
} catch (CSGI::Exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
for (;;) {
sleep(1);
std::cerr << "counter is now " << app->increment() << std::endl;
}
std::cerr << "Server running on port 8080" << std::endl;
return 0;
}
13 changes: 9 additions & 4 deletions csgi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "csgi.hpp"

void *run_thread(void *arg)
{
CSGI::Server *srv = (CSGI::Server *)arg;
srv->serve();
return NULL;
}

void CSGI::Server::run(bool async)
{
int i;
Expand Down Expand Up @@ -53,7 +60,8 @@ void CSGI::Server::run(bool async)
throw CSGI::Exception(err);
}
if (async) {
if ((pid_ = fork()) == 0) serve();
worker_ = (pthread_t *)malloc(sizeof(pthread_t));
pthread_create(worker_, NULL, run_thread, (void*)this);
} else {
serve();
}
Expand Down Expand Up @@ -98,9 +106,6 @@ void CSGI::Server::serve()
}

bool can_read(int fd) {
if (fd < 0) {
std::cerr << "lol what" << std::endl;
}
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
Expand Down
14 changes: 8 additions & 6 deletions csgi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <openssl/err.h>

#include <iostream> // FIXME REMOVEME
#include <pthread.h>

/**
* @file csgi.hpp
Expand Down Expand Up @@ -109,15 +110,16 @@ class Server {
throw CSGI::Exception("Error checking private key");
}

pid_ = -1;
worker_ = NULL;
}

~Server()
{
std::cerr << "Cleaning up\n" << std::endl;
if (pid_ != -1) {
kill(pid_, SIGTERM);
}
//FIXME
//if (pid_ != -1) {
// kill(pid_, SIGTERM);
//}
close(sockfd_);
freeaddrinfo(res_);
SSL_CTX_free(ssl_ctx_);
Expand All @@ -130,17 +132,17 @@ class Server {
* jeśli nie, wywołanie jest blokujące
*/
void run(bool async);
private: // udokumentowane w csgi.cpp
void serve();
private: // udokumentowane w csgi.cpp
Env parse_request(SSL*);
void send_response(Response&, SSL*);
Application *app_;
struct addrinfo hints_, *res_;
int sockfd_;
int port_;
int backlog_;
int pid_;

pthread_t *worker_;
SSL_METHOD *ssl_method_;
SSL_CTX *ssl_ctx_;
};
Expand Down

0 comments on commit 15a1214

Please sign in to comment.