Phoenix2D-Library  0.10
Reader.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <string>
22 #include <iostream>
23 #include <pthread.h>
24 #include "Reader.h"
25 #include "Connect.h"
26 #include "Parser.h"
27 #include "Config.h"
28 namespace Phoenix
29 {
32 bool running = false;
33 pthread_t thread = 0;
34 
35 void* run(void* arg) {
36  Reader* reader = (Reader *)arg;
37  reader->execute();
38  return 0;
39 }
40 
41 Reader::Reader(Connect *connect, Parser *parser) {
42  connect_ptr = connect;
43  parser_ptr = parser;
44 }
45 
47  if (Config::VERBOSE) std::cout << "Reader out" << std::endl;
48 }
49 
51  while (running) {
53  }
54 }
55 
57  return running;
58 }
59 
60 void Reader::start() {
61  running = true;
62  int success = pthread_create(&thread, 0, run, (void *)this);
63  if (success) {
64  running = false;
65  std::cerr << "Reader::start() -> error creating thread" << std::endl;
66  }
67 }
68 
69 void Reader::stop() {
70  void *res;
71  running = false;
72  connect_ptr->sendMessage("(bye)");
74  int success = pthread_join(thread, &res);
75  if (success) {
76  std::cerr << "Reader::stop() -> failed to join thread" << std::endl;
77  }
78 }
79 }
Reader(Connect *connect, Parser *parser)
Definition: Reader.cpp:41
void parseMessage(std::string message)
Definition: Parser.cpp:197
void * run(void *arg)
Definition: Reader.cpp:35
void disconnect()
Definition: Connect.cpp:81
pthread_t thread
Definition: Reader.cpp:33
bool isRunning()
Definition: Reader.cpp:56
void start()
Definition: Reader.cpp:60
bool sendMessage(std::string msg)
Definition: Connect.cpp:86
Parser The Parser lorem Ipsum
Definition: Parser.h:44
std::string receiveMessage()
Definition: Connect.cpp:95
static bool VERBOSE
Definition: Config.h:38
void stop()
Definition: Reader.cpp:69
Parser * parser_ptr
Definition: Reader.cpp:31
Connect * connect_ptr
Definition: Reader.cpp:30
Reader The Reader lorem Ipsum
Definition: Reader.h:39
void execute()
Definition: Reader.cpp:50
bool running
Definition: Reader.cpp:32