From 4ae013ecea326778b29edd8e21cd3964bb40ec6b Mon Sep 17 00:00:00 2001 From: strange Date: Wed, 20 Jan 2010 18:03:46 -0700 Subject: [PATCH] There is now a partially functional block display in the aesalon GUI. The "partially" is because only allocation events are generated in the monitor . . . --- src/gui/ActiveSession.cpp | 11 +++++++++++ src/gui/ActiveSession.h | 3 +++ src/gui/ActiveSessionBlockView.cpp | 32 ++++++++++++++++++++++++++++++++ src/gui/ActiveSessionBlockView.h | 31 +++++++++++++++++++++++++++++++ src/gui/ActiveSessionSocket.cpp | 9 ++++++++- src/gui/ActiveSessionSocket.h | 4 ++++ src/monitor/Initializer.cpp | 3 +++ src/platform/TCPServerSocket.cpp | 34 ++++++++++++++++++++++++++++++++-- src/platform/TCPServerSocket.h | 1 + src/platform/TCPSocket.cpp | 15 +++++++++------ src/platform/TCPSocket.h | 2 ++ 11 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 src/gui/ActiveSessionBlockView.cpp create mode 100644 src/gui/ActiveSessionBlockView.h diff --git a/src/gui/ActiveSession.cpp b/src/gui/ActiveSession.cpp index 29a4c29..672a60e 100644 --- a/src/gui/ActiveSession.cpp +++ b/src/gui/ActiveSession.cpp @@ -13,6 +13,9 @@ ActiveSession::ActiveSession(Session *session, QWidget *parent) : QTabWidget(par status = WAITING_FOR_CONNECTION; connect(this, SIGNAL(status_changed(QString)), overview, SLOT(update_status(QString))); emit status_changed(get_status_as_string()); + + block_view = new ActiveSessionBlockView(memory); + this->addTab(block_view, tr("&Block view")); } ActiveSession::~ActiveSession() { @@ -25,12 +28,20 @@ void ActiveSession::execute() { void ActiveSession::connect_to(QString host, int port) { socket = new ActiveSessionSocket(host, port, memory); + connect(socket, SIGNAL(connected()), this, SLOT(socket_connection())); + connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnection())); + + connect(socket, SIGNAL(event_received(Platform::Event*)), block_view, SLOT(memory_changed(Platform::Event*))); } void ActiveSession::socket_connection() { set_status(CONNECTED); } +void ActiveSession::socket_disconnection() { + set_status(CONNECTION_CLOSED); +} + QString ActiveSession::get_status_as_string() const { switch(get_status()) { case INITIALIZING: diff --git a/src/gui/ActiveSession.h b/src/gui/ActiveSession.h index 2324943..fe65adf 100644 --- a/src/gui/ActiveSession.h +++ b/src/gui/ActiveSession.h @@ -7,6 +7,7 @@ #include "Session.h" #include "ActiveSessionSocket.h" #include "ActiveSessionOverview.h" +#include "ActiveSessionBlockView.h" #include "platform/Memory.h" @@ -27,6 +28,7 @@ private: Platform::Memory *memory; ActiveSessionSocket *socket; ActiveSessionOverview *overview; + ActiveSessionBlockView *block_view; status_e status; void set_status(status_e new_status) { @@ -49,6 +51,7 @@ public: public slots: void terminate_session() { emit close_session(this); } void socket_connection(); + void socket_disconnection(); signals: void close_session(ActiveSession *session); void status_changed(QString new_status); diff --git a/src/gui/ActiveSessionBlockView.cpp b/src/gui/ActiveSessionBlockView.cpp new file mode 100644 index 0000000..70db783 --- /dev/null +++ b/src/gui/ActiveSessionBlockView.cpp @@ -0,0 +1,32 @@ +#include "ActiveSessionBlockView.h" +#include "ActiveSessionBlockView.moc" +#include "platform/BlockEvent.h" + +namespace Aesalon { +namespace GUI { + +ActiveSessionBlockView::ActiveSessionBlockView(Platform::Memory *memory, QWidget *parent) : QTableWidget(parent), memory(memory) { + this->setColumnCount(2); + +} + +ActiveSessionBlockView::~ActiveSessionBlockView() { + +} + +void ActiveSessionBlockView::memory_changed(Platform::Event *event) { + /* This is the block view, don't care about non-block events . . . */ + if(event->get_type() != Platform::Event::BLOCK_EVENT) return; + Platform::BlockEvent *be = dynamic_cast(event); + /*this->addItem(QString().setNum(be->get_address()));*/ + this->setRowCount(this->rowCount()+1); + QTableWidgetItem *address, *size; + address = new QTableWidgetItem(QString().setNum((quint64)be->get_address(), 16)); + size = new QTableWidgetItem(QString().setNum((quint64)be->get_size())); + this->setItem(this->rowCount()-1, 0, address); + this->setItem(this->rowCount()-1, 1, size); + +} + +} // namespace GUI +} // namespace Aesalon diff --git a/src/gui/ActiveSessionBlockView.h b/src/gui/ActiveSessionBlockView.h new file mode 100644 index 0000000..e44ff75 --- /dev/null +++ b/src/gui/ActiveSessionBlockView.h @@ -0,0 +1,31 @@ +#ifndef AESALON_GUI_ACTIVE_SESSION_BLOCK_VIEW_H +#define AESALON_GUI_ACTIVE_SESSION_BLOCK_VIEW_H + +#include +#include + +#include "platform/Memory.h" +#include "platform/Event.h" + +namespace Aesalon { +namespace GUI { + +class ActiveSessionBlock { +public: + +}; + +class ActiveSessionBlockView : public QTableWidget { Q_OBJECT +private: + Platform::Memory *memory; +public: + ActiveSessionBlockView(Platform::Memory *memory, QWidget *parent = 0); + virtual ~ActiveSessionBlockView(); +public slots: + void memory_changed(Platform::Event *event); +}; + +} // namespace GUI +} // namespace Aesalon + +#endif diff --git a/src/gui/ActiveSessionSocket.cpp b/src/gui/ActiveSessionSocket.cpp index abb340d..4a193a6 100644 --- a/src/gui/ActiveSessionSocket.cpp +++ b/src/gui/ActiveSessionSocket.cpp @@ -1,3 +1,4 @@ +#include #include "ActiveSessionSocket.h" #include "ActiveSessionSocket.moc" @@ -10,6 +11,7 @@ ActiveSessionSocket::ActiveSessionSocket(QString host, int port, Platform::Memor socket->connectToHost(host, port); connect(socket, SIGNAL(readyRead()), this, SLOT(handle_data())); connect(socket, SIGNAL(connected()), this, SLOT(reemit_connected())); + connect(socket, SIGNAL(disconnected()), this, SLOT(reemit_disconnected())); } ActiveSessionSocket::~ActiveSessionSocket() { @@ -17,11 +19,16 @@ ActiveSessionSocket::~ActiveSessionSocket() { void ActiveSessionSocket::handle_data() { QByteArray data = socket->readAll(); + QString str = data; while(data.length()) { QString string = data; data.remove(0, string.length()); Platform::Event *event = Platform::Event::deserialize(string.toStdString()); - if(event) memory->handle_event(event); + if(event) { + memory->handle_event(event); + emit event_received(event); + delete event; + } } } diff --git a/src/gui/ActiveSessionSocket.h b/src/gui/ActiveSessionSocket.h index 1fd35b3..0f7c886 100644 --- a/src/gui/ActiveSessionSocket.h +++ b/src/gui/ActiveSessionSocket.h @@ -5,6 +5,7 @@ #include #include "platform/Memory.h" +#include "platform/Event.h" namespace Aesalon { namespace GUI { @@ -19,8 +20,11 @@ public: public slots: void handle_data(); void reemit_connected() { emit connected(); } + void reemit_disconnected() { emit disconnected(); } signals: void connected(); + void disconnected(); + void event_received(Platform::Event *event); }; } // namespace GUI diff --git a/src/monitor/Initializer.cpp b/src/monitor/Initializer.cpp index 2cb2182..8273b49 100644 --- a/src/monitor/Initializer.cpp +++ b/src/monitor/Initializer.cpp @@ -73,6 +73,8 @@ void Initializer::initialize() { /*symbol_manager->parse_from_executable(ap->get_file(0)->get_filename());*/ run(); + + server_socket->disconnect_all(); } void Initializer::deinitialize() { @@ -106,6 +108,7 @@ void Initializer::run() { std::cout << "Sending data from event queue . . ." << std::endl; get_socket()->send_data(event_queue); } + server_socket->accept_connections(); } } diff --git a/src/platform/TCPServerSocket.cpp b/src/platform/TCPServerSocket.cpp index fb16170..b8c05c9 100644 --- a/src/platform/TCPServerSocket.cpp +++ b/src/platform/TCPServerSocket.cpp @@ -1,6 +1,8 @@ #include +#include #include #include +#include #include #include #include @@ -44,9 +46,15 @@ TCPServerSocket::TCPServerSocket(int port) : port(port) { freeaddrinfo(result); if(listen(socket_fd, 8) == -1) throw PlatformException("Couldn't listen on socket: "); + + int yes = 1; + + if(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + throw PlatformException("Couldn't set SO_REUSEADDR on socket: "); } TCPServerSocket::~TCPServerSocket() { + close(socket_fd); } @@ -60,8 +68,23 @@ int TCPServerSocket::get_port() const { void TCPServerSocket::accept_connections() { int s_fd; - while((s_fd = accept(socket_fd, NULL, 0))) { - socket_list.push_back(new TCPSocket(s_fd)); + fd_set listen_set, read_set; + struct timeval tv; + FD_ZERO(&listen_set); + FD_ZERO(&read_set); + FD_SET(socket_fd, &listen_set); + + tv.tv_sec = 0; + tv.tv_usec = 0; + + read_set = listen_set; + + while(select(socket_fd+1, &read_set, NULL, NULL, &tv)) { + if(FD_ISSET(socket_fd, &read_set)) { + s_fd = accept(socket_fd, NULL, 0); + socket_list.push_back(new TCPSocket(s_fd)); + } + read_set = listen_set; } } @@ -95,5 +118,12 @@ void TCPServerSocket::send_data(Misc::SmartPointer data) { } } +void TCPServerSocket::disconnect_all() { + socket_list_t::iterator i = socket_list.begin(); + for(; i != socket_list.end(); i ++) { + if((*i)->is_valid()) (*i)->disconnect(); + } +} + } // namespace Platform } // namespace Aesalon diff --git a/src/platform/TCPServerSocket.h b/src/platform/TCPServerSocket.h index 6b2a6a3..4dd13de 100644 --- a/src/platform/TCPServerSocket.h +++ b/src/platform/TCPServerSocket.h @@ -23,6 +23,7 @@ public: void accept_connections(); void remove_invalid_sockets(); + void disconnect_all(); void send_data(std::string data); void send_data(Misc::SmartPointer data); diff --git a/src/platform/TCPSocket.cpp b/src/platform/TCPSocket.cpp index 28b1f09..c9d246e 100644 --- a/src/platform/TCPSocket.cpp +++ b/src/platform/TCPSocket.cpp @@ -43,15 +43,11 @@ TCPSocket::TCPSocket(std::string host, int port) { valid = true; } TCPSocket::~TCPSocket() { - close(socket_fd); + if(socket_fd) close(socket_fd); } void TCPSocket::send_data(std::string data) { - std::string raw_data; - - raw_data = Misc::StreamAsString() << htons(data.length()) << data; - - int sent = write(socket_fd, raw_data.c_str(), raw_data.length()); + int sent = write(socket_fd, data.c_str(), data.length()); if(sent == -1) valid = false; } std::string TCPSocket::get_data() { @@ -79,5 +75,12 @@ std::string TCPSocket::get_data() { return data; } +void TCPSocket::disconnect() { + if(socket_fd) { + close(socket_fd); + socket_fd = 0; + } +} + } // namespace Platform } // namespace Aesalon diff --git a/src/platform/TCPSocket.h b/src/platform/TCPSocket.h index 570ca4a..72edb16 100644 --- a/src/platform/TCPSocket.h +++ b/src/platform/TCPSocket.h @@ -18,6 +18,8 @@ public: void send_data(std::string data); std::string get_data(); + void disconnect(); + bool is_valid() const { return valid; } }; -- 2.11.4.GIT