Commit 40a1c120 authored by Mark Taylor's avatar Mark Taylor
Browse files

Lookin real nice - can now start actually building a proper setup

No related merge requests found
Showing with 105 additions and 73 deletions
+105 -73
......@@ -6,7 +6,7 @@
#include "FileIO.h"
#include <fstream>
#include <string>
#include <iostream> // DEBUG
//#include <iostream> // DEBUG
#include "dirent.h"
FileIO::FileIO()
......@@ -17,6 +17,24 @@ FileIO::~FileIO()
{
}
std::string FileIO::pathAppend(const std::string& p1, const std::string& p2)
{
char sep = '/';
std::string tmp = p1;
#ifdef _WIN32
sep = '\\';
#endif
if (p1[p1.length()] != sep)
{
tmp += sep;
return(tmp + p2);
}
return(p1 + p2);
}
bool FileIO::getFilesInDirectory(std::vector<std::string> &files, const std::string &directory)
{
DIR *dir;
......@@ -58,4 +76,4 @@ bool FileIO::readTextFile(const std::string &filePath, std::string &output)
output = contents;
my_file.close();
return true;
}
\ No newline at end of file
}
......@@ -18,6 +18,15 @@ public:
// Destructor
~FileIO();
/**
* @brief
* Append p2 path to end of p1
* @param p1 - 'root' directory
* @param p2 - 'child' directory/file
* @return std::string - resultant full path
*/
std::string pathAppend(const std::string& p1, const std::string& p2);
/**
* @brief Get the Files In Directory
*
......@@ -38,4 +47,4 @@ public:
private:
};
#endif
\ No newline at end of file
#endif
......@@ -48,6 +48,18 @@ public:
private:
// Disable default constructor
HTTPD() :
m_port(0),
m_ServerIntf(0),
m_ready(false),
m_indexCode(0),
m_lenCode(0),
m_lenDescription(0),
m_lenTemplateBodyPlaceholder(0),
m_lenFaviconPlaceholder(0)
{}
/**
* @brief
* When a client connects, a process is spawned and the child is sent into this
......@@ -77,16 +89,16 @@ private:
void sendResponse(ClientThreadContainer container, statusCodes statusCode, const std::string &html);
/**
* @brief
* @brief
* Performs a lookup in the m_threadStatus map and updates the status attributed to a thread
*
*
* @param uuid - the calling thread's uuid
* @param status - the new status
*/
void updateStatus(std::string uuid, ThreadingStatus status);
/**
* @brief
* @brief
* Loop through m_clientThreads map and calls join() on threads
* @param wait - if true, will call join() regardless of state, if false, will only call join()
* on threads that are marked as STOPPED
......
/*
Copyright (C) 2021 Embed Creativity LLC
All Rights Reserved
*/
#include "Home.h"
namespace embedcreativity
{
const std::string Home::PAGE_CONTENT = "<h1 class=\"welcome\">WiFi Setup</h1>"
"<p class=\"description\">Select your WiFi network. Add your WiFi password to connect your device to the Internet.</p>"
"<div class=\"form-box\" >"
"<form action=\"/connect\">"
"<input type=\"radio\" id=\"ssid_a\" name=\"selected_ssid\" value=\"ssid_a\">"
"<label for=\"ssid_a\">ssid_a</label><br>"
"<input type=\"radio\" id=\"ssid_b\" name=\"selected_ssid\" value=\"ssid_b\">"
"<label for=\"ssid_b\">ssid_b</label><br><br>"
"<label class=\"text-input-label\" for=\"password\">Password</label>"
"<input class=\"text-input\" type=\"text\" id=\"password\" name=\"password\" value=\"\" ><br><br>"
"<input class=\"submit-button\" type=\"submit\" value=\"Connect\">"
"</form>"
"</div>";
std::pair<statusCodes, std::string> Home::handleQuery(std::map<std::string, std::string> &arguments)
{
std::pair<statusCodes, std::string> ret;
ret.first = statusCodes::STATUS_OK;
ret.second = PAGE_CONTENT;
return ret;
}
}
\ No newline at end of file
Home.html 0 → 100755
<h1 class="welcome">WiFi Setup</h1>
<p class="description">Select your WiFi network. Add your WiFi password to connect your device to the Internet.</p>
<div class="form-box" >
<form action="/connect">
<input type="radio" id="ssid_a" name="selected_ssid" value="ssid_a">
<label for="ssid_a">ssid_a</label><br>
<input type="radio" id="ssid_b" name="selected_ssid" value="ssid_b">
<label for="ssid_b">ssid_b</label><br><br>
<label class="text-input-label" for="password">Password</label>
<input class="text-input" type="text" id="password" name="password" value="" ><br><br>
<input class="submit-button" type="submit" value="Connect">
</form>
</div>
#CXX := g++
Program := ecHTTPD
#CXXFLAGS += -Wall -g -std=c++11
CXXFLAGS += -Wall -std=c++11
......@@ -7,8 +6,8 @@ LIBS +=
all: $(Program)
$(Program): HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o Home.o
$(CXX) $(LDFLAGS) HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o Home.o -o $(Program) $(LIBS)
$(Program): HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o StaticPage.o
$(CXX) $(LDFLAGS) HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o StaticPage.o -o $(Program) $(LIBS)
HTTPD.o: HTTPD.cpp
$(CXX) $(CXXFLAGS) -c HTTPD.cpp
......@@ -29,8 +28,8 @@ main.o: main.cpp
$(CXX) $(CXXFLAGS) -c main.cpp
# Pages
Home.o: Home.cpp
$(CXX) $(CXXFLAGS) -c Home.cpp
StaticPage.o: StaticPage.cpp
$(CXX) $(CXXFLAGS) -c StaticPage.cpp
# remove object files and executable when user executes "make clean"
clean:
......
......@@ -4,12 +4,12 @@
*/
#include "QueryListener.h"
#include <fstream>
#include "FileIO.h"
namespace embedcreativity
{
QueryListener::QueryListener(QueryPublisher* publisher, const std::string &query) :
QueryListener::QueryListener(QueryPublisher* publisher, const std::string &query) :
m_queryPublisher(publisher)
{
m_queryPublisher->subscribe(this, query);
......@@ -23,26 +23,18 @@ QueryListener::~QueryListener()
}
}
bool QueryListener::assignSourceTemplate(const std::string &templateFile)
bool QueryListener::assignSourceTemplate(const std::string &path, const std::string &templateFile)
{
std::ifstream hFile(templateFile);
FileIO fileHandler;
std::string fullPath = fileHandler.pathAppend(path, templateFile);
// ensure buffer is clear
m_templateHTML = "";
if (hFile.is_open())
if (!fileHandler.readTextFile(fullPath, m_templateHTML))
{
// Read file in one character at a time to avoid multiple copies
while (hFile)
{
m_templateHTML += hFile.get();
}
if (!m_templateHTML.empty())
{
return true;
}
m_templateHTML = "";
return false;
}
return false;
return true;
}
}
\ No newline at end of file
}
......@@ -40,15 +40,17 @@ public:
* @param[in] templateFile - path to template file
* @return True if file was successfully read and contents stored, False otherwise
*/
bool assignSourceTemplate(const std::string &templateFile);
bool assignSourceTemplate(const std::string &path, const std::string &templateFile);
protected:
std::string m_templateHTML;
private:
// Disable default constructor
QueryListener();
QueryPublisher* m_queryPublisher;
std::string m_templateHTML;
};
}
#endif
\ No newline at end of file
#endif
/*
Copyright (C) 2021 Embed Creativity LLC
All Rights Reserved
*/
#include "StaticPage.h"
namespace embedcreativity
{
std::pair<statusCodes, std::string> StaticPage::handleQuery(std::map<std::string, std::string> &arguments)
{
std::pair<statusCodes, std::string> ret;
ret.first = statusCodes::STATUS_OK;
ret.second = m_templateHTML;
return ret;
}
}
......@@ -3,8 +3,8 @@
All Rights Reserved
*/
#ifndef Home_H_
#define Home_H_
#ifndef STATIC_PAGE_H_
#define STATIC_PAGE_H_
#include "QueryListener.h"
#include "HTTPStatusCodes.h"
......@@ -14,7 +14,7 @@
namespace embedcreativity
{
class Home : public QueryListener
class StaticPage : public QueryListener
{
public:
......@@ -35,4 +35,4 @@ private:
};
}
#endif
\ No newline at end of file
#endif
......@@ -3,7 +3,7 @@
#include <iostream>
#include <cstdlib>
#include "HTTPD.h"
#include "Home.h"
#include "StaticPage.h"
using namespace embedcreativity;
......@@ -69,11 +69,12 @@ int main(int argc, char **argv)
if (server->ready())
{
std::string page = "/";
Home homePage = Home(server, page);
StaticPage homePage = StaticPage(server, page);
homePage.assignSourceTemplate(templatePath, "Home.html");
// HTTPD will not return on its own from this call
server->run();
}
return 0;
}
\ No newline at end of file
}
Supports Markdown
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