Commit a9f45ef1 authored by Seblu's avatar Seblu
Browse files

Ajout du multi-threading

Gestion de la reception d'info depuis le serveur
parent 9ae35914
Loading
Loading
Loading
Loading
+22 −4
Original line number Diff line number Diff line
@@ -12,6 +12,10 @@

Connection::Connection() : socket_(0) {}

bool Connection::connected() const {
  return socket_ != 0;
}

void Connection::start() {
  // make connection
  connect_(O.server.c_str(), O.port);
@@ -20,6 +24,8 @@ void Connection::start() {

  // send pass info

  

}

void Connection::stop() {
@@ -35,13 +41,18 @@ void Connection::connect_(const char *addr, int port) {

  // retrieve remote host info
  h = gethostbyname(addr);
  if (h == 0)
    throw Error(ERR_NET, (string) "gethostbyname(" + addr  + "): " + hstrerror(h_errno));
  if (h == 0) {
    S << "Unable to resolve: " << addr  << ": " << hstrerror(h_errno) << ".\n";
    return;
  }

  // create socket
  socket_ = socket(PF_INET, SOCK_STREAM, 0);
  if (socket_ == -1)
    throw Error(ERR_NET, (string) "socket: " + strerror(errno));
  if (socket_ == -1) {
    S << "Unable to create socket: " << strerror(errno) << ".\n";
    socket_ = 0;
    return;
  }

  daddr.sin_family = AF_INET;
  daddr.sin_port = htons(port);
@@ -83,3 +94,10 @@ void Connection::sendln(const char* buf, size_t len) {
  write(socket_, buf, len);
  write(socket_, "\n", 1);
}

string Connection::recvln() {
  char buf[MAX_LINE_SIZE];

  read(socket_, buf, MAX_LINE_SIZE);
  return buf;
}
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ class Connection {
public:
  Connection();


  bool connected() const;
  void start();
  void stop();
  void send(const string &data);
+16 −4
Original line number Diff line number Diff line
@@ -284,6 +284,15 @@ void Screen::msg_print(int i) {
  msg_buffer_ << i;
}

/*!
** Read and print socket
**
*/
void Screen::socket_reader() {
  while (1)
    msg_println(C.recvln());
}


/*
*********************************************************************************
@@ -510,20 +519,23 @@ void Screen::cmd_exec(const char *s) {
      exit(0);
    }
    else if (!strcmp(s, "/logout")) {
      C.stop();
      S << s << "\n";
      C.stop();
    }
    else if (!strcmp(s, "/login")) {
      C.start();
      S << s << "\n";
      C.start();
    }
    else
      S << s << ": Unkown command.\n";
  }
  // String is a remote command
  else {
    if (C.connected()) {
      C.sendln(s, strlen(s));
    msg_println(s);
      S << ">> " << s << "\n";
    }
    else S << "Unable to send: " << s << ". Try to reconnect with /login !\n";
  }
}

+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ public:
  void msg_println(int i);
  void msg_print(const string &s);
  void msg_print(int i);
  void socket_reader();

private:
  void create_windows();
+18 −1
Original line number Diff line number Diff line

#include <getopt.h>
#include <pthread.h>

#include "slc.hh"
#include "options.hh"
@@ -13,6 +14,8 @@ Options O;
History H;
Connection C;

void main2();

/*!
** Program entry point
**
@@ -23,6 +26,8 @@ Connection C;
*/
int main(int argc, char *argv[])
{
  pthread_t t;

  try {
    // get argvline options
    O.load(argc, argv);
@@ -42,9 +47,12 @@ int main(int argc, char *argv[])
    // Showing dialog screen
    S.dialog();

    // start connexion. Launch a thread.
    // start connexion
    C.start();

    // Start reader thread
    pthread_create(&t, 0, (void *(*)(void *)) main2, 0);

    // Treat keyboards events
    S.eventsloop();

@@ -59,3 +67,12 @@ int main(int argc, char *argv[])

  return 0;
}


/*!
** Entry point of the second thread which is used to
** print data sent by server
*/
void main2() {
  S.socket_reader();
}