commit 8e5f484d600ba532eeeb5330d57090219155070e
parent 15bafcc4c0566b13f2259f549d47684c38eccc22
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Wed, 16 Mar 2016 18:08:02 -0600
viewer: show the file contents and execution counts
Diffstat:
5 files changed, 90 insertions(+), 129 deletions(-)
diff --git a/viewer/af_unix.cxx b/viewer/af_unix.cxx
@@ -1,4 +1,5 @@
#include <err.h> // err
+#include <fcntl.h>
#include <string.h> // memset
#include <sys/socket.h> // socket
#include <sys/un.h> // sockaddr_un
@@ -8,17 +9,17 @@
#include "af_unix.h"
-af_unix_nonblock::af_unix_nonblock()
+af_unix::af_unix()
{
}
-af_unix_nonblock::af_unix_nonblock(int f) :
+af_unix::af_unix(int f) :
fd(f)
{
}
void
-af_unix_nonblock::set_listen()
+af_unix::set_listen()
{
if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1)
err(1, "socket");
@@ -35,8 +36,8 @@ af_unix_nonblock::set_listen()
err(1, "listen");
}
-af_unix_nonblock *
-af_unix_nonblock::accept()
+af_unix *
+af_unix::accept()
{
int new_fd;
struct sockaddr_un addr;
@@ -51,12 +52,20 @@ af_unix_nonblock::accept()
return NULL;
}
+ // Turn off non blocking mode
+ int flags = fcntl(new_fd, F_GETFL, 0);
+ if (flags < 0)
+ err(1, "fcntl(F_GETFL)");
+ fcntl(new_fd, F_SETFL, flags & ~O_NONBLOCK);
+ if (flags < 0)
+ err(1, "fcntl(F_SETFL)");
+
std::cerr << "accepted new connection" << std::endl;
- return new af_unix_nonblock(new_fd);
+ return new af_unix(new_fd);
}
int
-af_unix_nonblock::write_all(uint8_t *buf, size_t bytes_total)
+af_unix::write_all(uint8_t *buf, size_t bytes_total)
{
int bytes_left = bytes_total;
int bytes_wrote = 0;
@@ -65,9 +74,6 @@ af_unix_nonblock::write_all(uint8_t *buf, size_t bytes_total)
while (bytes_left > 0) {
n = write(fd, buf + bytes_wrote, bytes_left);
- if (n < 0 && errno == EAGAIN)
- /* Do not try to continue writing data */
- break;
if (n < 0)
err(1, "write()");
@@ -79,49 +85,19 @@ af_unix_nonblock::write_all(uint8_t *buf, size_t bytes_total)
}
int
-af_unix_nonblock::read_block(uint64_t &buf)
+af_unix::read_all(uint64_t &buf)
{
int bytes_left = sizeof(uint64_t);
int bytes_read = 0;
ssize_t n;
- // Read bytes, busy waiting when the socket returns EAGAIN
while (bytes_left > 0) {
n = read(fd, &buf + bytes_read, bytes_left);
if (n == 0)
errx(1, "read(): read 0 bytes on socket");
- if (n < 0 && errno != EAGAIN)
- err(1, "read()");
- if (n < 0 && errno == EAGAIN)
- // Don't let counters increment when n == -1!
- continue;
-
- bytes_read += n;
- bytes_left -= n;
- }
-
- return bytes_read;
-}
-
-int
-af_unix_nonblock::read_block(uint8_t *buf, size_t bytes_total)
-{
- int bytes_left = bytes_total;
- int bytes_read = 0;
- ssize_t n;
-
- // Read bytes, busy waiting when the socket returns EAGAIN
- while (bytes_left > 0) {
- n = read(fd, buf + bytes_read, bytes_left);
-
- if (n == 0)
- errx(1, "read(): read 0 bytes on socket");
- if (n < 0 && errno != EAGAIN)
+ if (n < 0)
err(1, "read()");
- if (n < 0 && errno == EAGAIN)
- // Don't let counters increment when n == -1!
- continue;
bytes_read += n;
bytes_left -= n;
@@ -131,24 +107,19 @@ af_unix_nonblock::read_block(uint8_t *buf, size_t bytes_total)
}
int
-af_unix_nonblock::read_nonblock(uint8_t *buf, size_t bytes_total)
+af_unix::read_all(uint8_t *buf, size_t bytes_total)
{
int bytes_left = bytes_total;
int bytes_read = 0;
ssize_t n;
- // Try and read some bytes, returning the number of bytes read so far if
- // the socket returns EAGAIN
while (bytes_left > 0) {
n = read(fd, buf + bytes_read, bytes_left);
if (n == 0)
errx(1, "read(): read 0 bytes on socket");
- if (n < 0 && errno != EAGAIN)
+ if (n < 0)
err(1, "read()");
- if (n < 0 && errno == EAGAIN)
- // Break out of the loop and return bytes read so far
- break;
bytes_read += n;
bytes_left -= n;
@@ -157,8 +128,8 @@ af_unix_nonblock::read_nonblock(uint8_t *buf, size_t bytes_total)
return bytes_read;
}
-af_unix_nonblock::~af_unix_nonblock()
+af_unix::~af_unix()
{
close(fd);
- unlink("socket");
+ unlink("../while/viewer_test.socket");
}
diff --git a/viewer/af_unix.h b/viewer/af_unix.h
@@ -3,19 +3,18 @@
#include <vector>
-class af_unix_nonblock {
+class af_unix {
public:
- af_unix_nonblock();
- af_unix_nonblock(int);
- ~af_unix_nonblock();
+ af_unix();
+ af_unix(int);
+ ~af_unix();
void set_listen();
- af_unix_nonblock *accept();
+ af_unix *accept();
- int read_block(uint64_t &);
- int read_block(uint8_t *, size_t);
+ int read_all(uint64_t &);
+ int read_all(uint8_t *, size_t);
- int read_nonblock(uint8_t *, size_t);
int write_all(uint8_t *, size_t);
private:
int fd;
diff --git a/viewer/text.cxx b/viewer/text.cxx
@@ -2,42 +2,63 @@
#include <cassert>
#include <iostream>
+#include <fstream>
+#include <sstream>
#include <vector>
#include "text.h"
-text::text(af_unix_nonblock *sock) :
+text::text(af_unix *sock) :
socket(sock),
- state(WRITE),
- font(FTGLPixmapFont("DejaVuSansMono.ttf")),
- buffer(NULL)
+ font(FTGLPixmapFont("DejaVuSansMono.ttf"))
{
if (font.Error())
errx(1, "%s", "font error");
- uint8_t msg_type = 0;
- assert(socket->write_all(&msg_type, 1) == 1);
-
- assert(socket->read_block(num_tus) == 8);
+ assert(socket->read_all(num_tus) == 8);
std::cerr << "text::text() num tus = " << num_tus << std::endl;
for (int i = 0; i < num_tus; i++) {
uint64_t file_name_sz;
- assert(socket->read_block(file_name_sz) == 8);
- std::cerr << "text::text() file name size= " << file_name_sz << std::endl;
+ assert(socket->read_all(file_name_sz) == 8);
+ std::cerr << "text::text() file name size = " << file_name_sz << std::endl;
file_name = (char *)malloc(file_name_sz + 1);
- assert(socket->read_block((uint8_t *)file_name, file_name_sz) == file_name_sz);
+ assert(socket->read_all((uint8_t *)file_name, file_name_sz) == file_name_sz);
file_name[file_name_sz] = '\0';
std::cerr << "text::text() file name = " << file_name << std::endl;
+ read_file();
- assert(socket->read_block(num_lines) == 8);
+ assert(socket->read_all(num_lines) == 8);
+ execution_counts.resize(num_lines);
std::cerr << "text::text() num lines = " << num_lines << std::endl;
}
- font.FaceSize(36);
- font.Render("Hello World!", 12, FTPoint(0, 36, 0));
+ font.FaceSize(24);
+
font.Render(file_name);
+ int vertical = num_lines * 24;
+ for (auto &line : source_file_contents) {
+ font.Render(&line[0], line.size(), FTPoint(0, vertical, 0));
+ vertical -= 24;
+ }
+}
+
+void
+text::read_file()
+{
+ std::wstring line;
+ std::wifstream file_stream(file_name);
+
+ if (file_stream.is_open() == 0)
+ errx(1, "ifstream.open()");
+
+ while (std::getline(file_stream, line)) {
+ source_file_contents.push_back(line);
+ std::wcerr << line << std::endl;
+ }
+
+ file_stream.close();
}
void
@@ -48,41 +69,20 @@ text::draw()
void
text::idle()
{
- if (state == READ) {
- size_t n = 0;
- n = socket->read_nonblock((uint8_t *)buffer + bytes_read, bytes_left);
-
- bytes_read += n;
- bytes_left -= n;
-
- if (bytes_left > 0)
- // There's more data coming
- return;
-
- std::cerr << "---" << std::endl;
- for (int i = 0; i < num_lines; i++) {
- std::cerr << "line " << i << ": " << buffer[i] << std::endl;
- }
+ size_t bytes_total = num_lines * sizeof(uint64_t);
+ assert(socket->read_all((uint8_t *)&execution_counts[0], bytes_total) == bytes_total);
- state = WRITE;
- }
- if (state == WRITE) {
- uint8_t msg_type = 1;
- if (socket->write_all(&msg_type, 1) != 1)
- // Couldn't write a request, try again later
- err(1, "write()");
-
- // Sent a successful request, listen for reply
- state = READ;
-
- if (buffer != NULL)
- free(buffer);
+ // Send response back
+ uint8_t msg_type = 1;
+ assert(socket->write_all(&msg_type, 1) == 1);
- buffer = (uint64_t *)malloc(num_lines * sizeof(uint64_t));
- if (buffer == NULL)
- err(1, "malloc");
+ int vertical = num_lines * 24;
+ for (auto &count : execution_counts) {
+ std::stringstream ss;
+ ss << count;
+ std::string s_count = ss.str();
- bytes_left = num_lines * sizeof(uint64_t);
- bytes_read = 0;
+ font.Render(&s_count[0], s_count.size(), FTPoint(600, vertical, 0));
+ vertical -= 24;
}
}
diff --git a/viewer/text.h b/viewer/text.h
@@ -1,9 +1,7 @@
#ifndef TEXT_H
#define TEXT_H
-#include <GL/glew.h>
-#include <GL/freeglut.h>
-
+#include <vector>
#include <FTGL/ftgl.h>
#include "af_unix.h"
@@ -11,27 +9,20 @@
class text : public drawable {
public:
- text(af_unix_nonblock *);
+ text(af_unix *);
void draw();
void idle();
private:
- af_unix_nonblock *socket;
+ void read_file();
+
+ af_unix *socket;
uint64_t num_tus;
char *file_name;
uint64_t num_lines;
- enum states {
- READ,
- WRITE
- };
- enum states state;
- uint64_t msg_size;
- uint64_t bytes_left;
- uint64_t bytes_read;
- uint64_t *buffer;
-
- void render_text(const char *, float x, float y, float sx, float sy);
+ std::vector<std::wstring> source_file_contents;
+ std::vector<uint64_t> execution_counts;
FTGLPixmapFont font;
};
diff --git a/viewer/viewer.cxx b/viewer/viewer.cxx
@@ -23,14 +23,14 @@ public:
private:
static std::vector<drawable*> drawables;
- static af_unix_nonblock socket;
+ static af_unix socket;
static void display();
static void idle();
};
// fuckin c++
std::vector<drawable*> window::drawables;
-af_unix_nonblock window::socket;
+af_unix window::socket;
window::window(int argc, char *argv[])
{
@@ -48,10 +48,10 @@ window::window(int argc, char *argv[])
if (!GLEW_VERSION_2_0)
errx(1, "No support for OpenGL 2.0 found");
- glutDisplayFunc(display);
+ glutDisplayFunc(window::display);
glutIdleFunc(idle);
- // set socket to listening mode
+ // This creates the socket with SOCK_NONBLOCK
socket.set_listen();
}
@@ -81,15 +81,15 @@ window::display(void)
for (auto &d : drawables)
d->draw();
+ std::cerr << "window__display" << std::endl;
+
glutSwapBuffers();
}
void
window::idle(void)
{
- af_unix_nonblock *temp_socket;
-
- temp_socket = socket.accept();
+ af_unix *temp_socket = socket.accept();
if (temp_socket)
drawables.push_back(new text(temp_socket));