Commit bba2fd8c authored by Andreas Kempe's avatar Andreas Kempe

Ordering an image rendered by a single thread is now possible.

parent e9d20f87
......@@ -2,13 +2,16 @@
#include <QPixmap>
#include <QPushButton>
#include <QDebug>
#include <QVBoxLayout>
#include <Magick++.h>
#include <string>
main_window::main_window(unsigned int port, QWidget* parent) :
port(port), QWidget(parent)
QWidget(parent), socket(nullptr), port(port)
{
mandel_image = new QImage(1064, 800, QImage::Format_RGB30);
mandel_image = new QImage;
QVBoxLayout* layout = new QVBoxLayout(this);
scene = new QGraphicsScene(this);
scene->setSceneRect(0, 0, 1064, 800);
......@@ -23,10 +26,17 @@ main_window::main_window(unsigned int port, QWidget* parent) :
QPushButton* img_btn = new QPushButton("Show image", this);
connect(img_btn, &QPushButton::clicked, this, &main_window::show_image);
layout->addWidget(img_btn);
QPushButton* rnd_btn = new QPushButton("Render image", this);
connect(rnd_btn, &QPushButton::clicked, this, &main_window::send_img_render);
layout->addWidget(rnd_btn);
server = new mandel_tcp_server(this);
connect(server, &QTcpServer::newConnection, this, &main_window::handle_new_connection);
server->listen(QHostAddress::Any, port);
setLayout(layout);
}
main_window::~main_window()
......@@ -40,6 +50,16 @@ void main_window::show_image()
view->show();
}
void main_window::send_img_render()
{
if (!socket)
return;
// Send an image request to the server.
data_packet rnd_packet(op_codes::RENDER_IMG, 0, nullptr);
socket->send_data_packet(rnd_packet);
}
void main_window::handle_new_connection()
{
socket = server->nextPendingConnection();
......@@ -54,6 +74,8 @@ void main_window::handle_new_connection()
void main_window::handle_socket_close()
{
socket->deleteLater();
socket = nullptr;
// If we lose the cluster process we start listening again.
server->listen(QHostAddress::Any, port);
}
......@@ -74,7 +96,10 @@ void main_window::handle_recv_data()
mandel_image->loadFromData((const uchar*) blob.data(), blob.length());
item->setPixmap(QPixmap::fromImage(*mandel_image));
break;
}
default:
qDebug() << "Unrecognised op code received: " << static_cast<quint32>(packet->get_op_code());
}
delete packet;
......
......@@ -29,6 +29,7 @@ class main_window : public QWidget
public slots:
void show_image();
void send_img_render();
private slots:
void handle_new_connection();
......
......@@ -16,17 +16,17 @@ data_packet::~data_packet()
payload = nullptr;
}
op_codes data_packet::get_op_code()
op_codes data_packet::get_op_code() const
{
return static_cast<op_codes>(op_code);
}
uint data_packet::get_payload_size()
uint data_packet::get_payload_size() const
{
return payload_size;
}
const char* data_packet::get_payload()
const char* data_packet::get_payload() const
{
return payload;
}
......
......@@ -16,9 +16,9 @@ class data_packet
data_packet(quint32 op_code, uint payload_size, char* payload);
~data_packet();
op_codes get_op_code();
uint get_payload_size();
const char* get_payload();
op_codes get_op_code() const;
uint get_payload_size() const;
const char* get_payload() const;
private:
quint32 op_code;
......
......@@ -39,8 +39,6 @@ int main(int argc, char** argv)
.iterations = ITERATIONS };
mandel_render_srv srv(settings, "192.168.1.2", PORT, id, procs, nullptr);
srv.start_drawing();
int exit_code = app.exec();
MPI_Finalize();
......
......@@ -15,7 +15,10 @@ mandel_render_srv::mandel_render_srv(mandel_settings settings, std::string host,
{
// We only let the root process connect to the client.
if (id == 0)
{
socket = new mandel_socket(host.c_str(), port, this);
connect(socket, &mandel_socket::packet_ready, this, &mandel_render_srv::handle_recv_data);
}
else
socket = nullptr;
}
......@@ -79,8 +82,27 @@ void mandel_render_srv::root_process_handling(unsigned long *receive_buffer)
std::string enc_img = image_data.base64();
char* data = new char[enc_img.size() + 1];
memcpy(data, enc_img.c_str(), enc_img.size() + 1);
memcpy(data, enc_img.c_str(), enc_img.size());
data[enc_img.size()] = '\0';
data_packet packet(op_codes::IMG_DATA, enc_img.size() + 1, data);
socket->send_data_packet(packet);
}
void mandel_render_srv::handle_recv_data()
{
data_packet* packet = socket->get_next_packet();
switch (packet->get_op_code())
{
case op_codes::RENDER_IMG:
{
start_drawing();
break;
}
default:
qDebug() << "Unrecognised op code received: " << static_cast<quint32>(packet->get_op_code());
}
delete packet;
}
......@@ -20,6 +20,9 @@ class mandel_render_srv : public QObject
const int procs;
mandel_socket* socket;
private slots:
void handle_recv_data();
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment