First sync

This commit is contained in:
hoguchi live 2022-02-17 16:15:31 +01:00
parent d8eb74f6e5
commit 69e082faac
41 changed files with 1758 additions and 0 deletions

1
nasfaq/api/src/cJSON Submodule

@ -0,0 +1 @@
Subproject commit a6424b85dde200a87cac26451c6f0a6f3426681f

2
nasfaq/api/src/common.c Normal file
View File

@ -0,0 +1,2 @@
#include "common.h"

20
nasfaq/api/src/common.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
/**
USERINFO
*/
#define USERINFO_COLOR_SIZE 16
#define USERINFO_EMAIL_SIZE 64
#define USERINFO_HAT_SIZE 32
#define USERINFO_USERNAME_SIZE 64
#endif

55
nasfaq/api/src/items.c Normal file
View File

@ -0,0 +1,55 @@
// @file items.c
#include "items.h"
void item_init_(item_t *op) {
op->category = UNDEFINED;
strcpy(op->type, "");
op->acquiredTimestamp = 0;
op->lastPurchasePrice = 0;
op->quantity = 0;
}
item_t *item_init() {
item_t *op = malloc(sizeof(item_t));
item_init_(op);
return op;
}
void item_set(item_t *op, item_category op1, char *op2, ulong op3, float op4, uint op5) {
op->category = op1;
strcpy(op->type, op2);
op->acquiredTimestamp = op3;
op->lastPurchasePrice = op4;
op->quantity = op5;
}
item_t *item_init_set(item_category op1, char *op2, ulong op3, float op4, uint op5) {
item_t *op = item_init();
item_set(op, op1, op2, op3, op4, op5);
return op;
}
void item_clear(item_t *op) {
free(op);
}
void item_print(item_t *op) {
printf("ITEM\n Category: ");
switch (op->category) {
case LICENSE: printf("license "); break;
case COSMETIC: printf("cosmetic "); break;
default: printf("undefined ");
}
printf("\n Type: %s", op->type);
printf("\n Acquired timestamp: %lu", op->acquiredTimestamp);
printf("\n Last purchase price: %f", op->lastPurchasePrice);
printf("\n Quantity: %u\n", op->quantity);
}

27
nasfaq/api/src/items.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef _ITEMS_H_
#define _ITEMS_H_
#include "common.h"
#define ITEMS_TYPE_SIZE 32
typedef enum {LICENSE, COSMETIC, UNDEFINED} item_category;
typedef struct item_t {
item_category category;
char type[ITEMS_TYPE_SIZE];
ulong acquiredTimestamp;
float lastPurchasePrice;
uint quantity;
} item_t;
item_t *item_init();
void item_set(item_t *, item_category, char *, ulong, float, uint);
item_t *item_init_set(item_category, char *, ulong, float, uint);
void item_clear(item_t *);
void item_print(item_t *);
#endif

43
nasfaq/api/src/mute.c Normal file
View File

@ -0,0 +1,43 @@
// @file mute.c
#include "mute.h"
void mute_init_(mute_t *op) {
op->muted = 0;
op->until = 0;
strcpy(op->message, "");
}
mute_t *mute_init() {
mute_t *op = malloc(sizeof(mute_t));
mute_init_(op);
return op;
}
void mute_set(mute_t *op, bool muted, ulong until, char *message) {
op->muted = 0;
op->until = until;
strcpy(op->message, message);
}
mute_t *mute_init_set(bool muted, ulong until, char *message) {
mute_t *op = mute_init();
mute_set(op, muted, until, message);
return op;
}
void mute_clear(mute_t *op) {
free(op);
}
void mute_print(mute_t *op) {
printf("MUTE\n Muted: %d", op->muted);
printf("\n Until: %lu", op->until);
printf("\n Message: %s\n", op->message);
}

22
nasfaq/api/src/mute.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef _MUTE_H_
#define _MUTE_H_
#include "common.h"
#define MUTE_MESSAGE_SIZE 128
typedef struct mute_t {
bool muted;
ulong until;
char message[MUTE_MESSAGE_SIZE];
} mute_t;
mute_t *mute_init();
void mute_set(mute_t *, bool, ulong, char *);
mute_t *mute_init_set(bool, ulong, char *);
void mute_clear(mute_t *);
void mute_print(mute_t *);
#endif

View File

@ -0,0 +1,98 @@
// @file performance.c
#include "performance.h"
/**
Performance points
*/
void performance_point_init_(performance_point_t *p) {
p->worth = 0;
p->timestamp = 0;
}
performance_point_t *performance_point_init() {
performance_point_t *p = malloc(sizeof(performance_point_t));
performance_point_init_(p);
return p;
}
void performance_point_set(performance_point_t *p, float worth, ulong timestamp) {
p->worth = worth;
p->timestamp = timestamp;
}
void performance_point_init_set_(performance_point_t *p, float worth, ulong timestamp) {
performance_point_init_(p);
performance_point_set(p, worth, timestamp);
}
performance_point_t *performance_point_init_set(float worth, ulong timestamp) {
performance_point_t *p = performance_point_init();
performance_point_set(p, worth, timestamp);
return p;
}
void performance_point_clear(performance_point_t *p) {
free(p);
}
void performance_point_print(performance_point_t *p) {
printf("PERFORMANCE POINT\n");
printf(" Worth: %f\n", p->worth);
printf(" Timestamp: %lu\n", p->timestamp);
}
/**
Performance data (arrays of points)
*/
void performance_data_init_(performance_data_t *d) {
d->size = 0;
d->data = malloc(sizeof(performance_data_t) * PERFORMANCE_DATA_SIZE);
}
performance_data_t *performance_data_init() {
performance_data_t *p = malloc(sizeof(performance_data_t));
performance_data_init_(p);
return p;
}
void performance_data_set(performance_data_t *d, uint size, performance_point_t *data) {
d->size = size;
memcpy(d->data, data, sizeof(performance_point_t) * size); // Woah mama
}
void performance_data_init_set_(performance_data_t *d, uint size, performance_point_t *data) {
performance_data_init_(d);
performance_data_set(d, size, data);
}
performance_data_t *performance_data_init_set(uint size, performance_point_t *data) {
performance_data_t *d = performance_data_init();
performance_data_set(d, size, data);
return d;
}
void performance_data_clear(performance_data_t *d) {
free(d->data);
free(d);
}
void performance_data_print(performance_data_t *d) {
printf("PERFORMANCE DATA\n");
printf(" Size: %u\n", d->size);
for(int i = 0; i < d->size; i++) performance_point_print(d->data + i);
}

View File

@ -0,0 +1,50 @@
#ifndef _PERFORMANCE_H_
#define _PERFORMANCE_H_
#include "common.h"
#define PERFORMANCE_DATA_SIZE 365*2 // Very dangerous TODO: REPLACE SIZE WITH LENGTH WHEN DEALING WITH ARRAYS
/**
Custom types
*/
typedef struct performance_point_t {
float worth;
ulong timestamp;
} performance_point_t;
typedef struct performance_data_t {
uint size;
performance_point_t *data;
} performance_data_t;
/**
Performance points
*/
void performance_point_init_(performance_point_t *);
performance_point_t *performance_point_init();
void performance_point_set(performance_point_t *, float, ulong);
void performance_point_init_set_(performance_point_t *, float, ulong);
performance_point_t *performance_point_init_set(float, ulong);
void performance_point_clear(performance_point_t *);
void performance_point_print(performance_point_t *);
/**
Performance data
*/
void performance_data_init_(performance_data_t *);
performance_data_t *performance_data_init();
void performance_data_set(performance_data_t *, uint, performance_point_t *);
void performance_data_init_set_(performance_data_t *, uint, performance_point_t *);
performance_data_t *performance_data_init_set(uint, performance_point_t *);
void performance_data_clear(performance_data_t *);
void performance_data_print(performance_data_t *);
#endif

37
nasfaq/api/src/settings.c Normal file
View File

@ -0,0 +1,37 @@
// @file settings.c
#include "settings.h"
void settings_init_(settings_t *op) {
op->walletIsPublic = 0;
}
settings_t *settings_init() {
settings_t *op = malloc(sizeof(settings_t));
settings_init_(op);
return op;
}
void settings_set(settings_t *op, bool walletIsPublic) {
op->walletIsPublic = walletIsPublic;
}
settings_t *settings_init_set(bool walletIsPublic) {
settings_t *op = settings_init();
settings_set(op, walletIsPublic);
return op;
}
void settings_clear(settings_t *op) {
free(op);
}
void settings_print(settings_t *op) {
printf("SETTINGS\n Wallet is public: %d\n", op->walletIsPublic);
}

18
nasfaq/api/src/settings.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _SETTINGS_H_
#define _SETTINGS_H_
#include "common.h"
typedef struct settings_t {
bool walletIsPublic;
} settings_t;
settings_t *settings_init();
void settings_set(settings_t *, bool);
settings_t *settings_init_set(bool);
void settings_clear(settings_t *);
void settings_print(settings_t *);
#endif

26
nasfaq/api/src/userInfo.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef _USERINFO_H_
#define _USERINFO_H_
typedef struct userInfo_t {
bool admin;
float brokerFreeTotal;
char color[USERINFO_COLOR_SIZE];
char email[USERINFO_EMAIL_SIZE];
char hat[USERINFO_HAT_SIZE];
char icon[USERINFO_ICON_SIZE];
char id[USERINFO_ICON_SIZE];
bool isBookie;
items_t items;
bool loggedout;
muted_t muted;
performance_t performance;
settings_t settings;
float taxCredits;
char username[USERINFO_USERNAME_SIZE];
bool verified;
wallet_t wallet;
} userInfo_t;
#endif

8
nasfaq/api/test/compile.sh Executable file
View File

@ -0,0 +1,8 @@
gcc ../src/common.c \
../src/mute.c \
../src/performance.c \
../src/settings.c \
../src/items.c \
../src/cJSON/cJSON.c \
./main.c \
-Wall -g -o test && ./test

12
nasfaq/api/test/main.c Normal file
View File

@ -0,0 +1,12 @@
#include "../src/items.h"
#include "../src/mute.h"
int main() {
item_t *t = item_init();
item_print(t);
item_clear(t);
};

BIN
nasfaq/api/test/test Executable file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,171 @@
#include "http_connector.h"
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
//class http_connection {
//public:
// http_connection(int id, CURL *hdl, std::string uri)
// : m_id(id)
// ; m_hdl(hdl)
// ; m_uri(uri)
// ; m_response("N/A")
// {}
//
//private:
// int m_id;
// CURL *m_hdl;
// std::string m_uri;
// CURLcode m_status;
// std::string m_response;
// std::vector<std::string> m_payloads;
//}
//
//class http_endpoint {
//public:
// http_endpoint() {
//
// }
//private:
//}
std::string get_sid()
{
CURL *curl;
CURLcode res;
static const char *POLLING_URL_0 = "https://nasfaq.biz/socket/?EIO=4&transport=polling&t=Ny7z439";
static const char *POLLING_URL_1 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z472";
static const char *POLLING_URL_2 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4Bn&sid=";
static const char *POLLING_URL_3 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4Bp&sid=";
static const char *POLLING_URL_4 = "https://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=polling&t=Ny7z4EU&sid=";
long http_code = 0;
std::string sid_final = "";
std::string readBuffer = "";
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
/* Header */
struct curl_slist *slist1;
slist1 = NULL;
slist1 = curl_slist_append(slist1 , "Cookie: holosesh=s%3AxmS8xBlQk4kH_rXQOaNjHk_3OuaBDsfA.M0yi%2BZmkiq%2BAmJBRj%2FNg9S%2BaSQpsfHRJcEeYVHLiKXg");
slist1 =curl_slist_append(slist1, "referer : https://nasfaq.biz/market");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
if(curl) {
/************** ZEROTH STAGE ********/
readBuffer = "";
curl_easy_setopt(curl, CURLOPT_URL, POLLING_URL_0);
res = curl_easy_perform(curl);
/* Check for result */
if ( res != CURLE_ABORTED_BY_CALLBACK) {
std::cout << "Zeroth stage ok" << std::endl;
}else {
std::cout << "Failure" << std::endl;
return "";
}
/************** FIRST STAGE ********/
readBuffer = "";
curl_easy_setopt(curl, CURLOPT_URL, POLLING_URL_1);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for result */
if (res != CURLE_ABORTED_BY_CALLBACK) {
std::cout << "First stage ok" << std::endl;
}else {
std::cout << "Failure" << std::endl;
return "";
}
/* Get sid */
nlohmann::json r = nlohmann::json::parse(readBuffer.substr(1));
sid_final = r["sid"];
/************** SECOND STAGE ********/
readBuffer = "";
std::string polling_url_2 = POLLING_URL_2 + sid_final;
curl_easy_setopt(curl, CURLOPT_URL, polling_url_2.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "40");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for result */
if (res != CURLE_ABORTED_BY_CALLBACK && readBuffer == "ok") {
std::cout << "Second stage ok" << std::endl;
}else {
std::cout << "Failure" << std::endl;
return "";
}
/************** THIRD STAGE ********/
readBuffer = "";
// Format next url
std::string polling_url_3 = POLLING_URL_3 + sid_final;
curl_easy_setopt(curl, CURLOPT_URL, polling_url_3.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for result */
if (res != CURLE_ABORTED_BY_CALLBACK) {
nlohmann::json r2 = nlohmann::json::parse(readBuffer.substr(2));
std::cout << "Third stage ok" << std::endl;
}else {
std::cout << "Failure" << std::endl;
return "";
}
/************** FOURTH STAGE ********/
readBuffer = "";
std::string polling_url_4 = POLLING_URL_4 + sid_final;
curl_easy_setopt(curl, CURLOPT_URL, polling_url_4.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for result */
if ( res != CURLE_ABORTED_BY_CALLBACK) {
std::cout << "Fourth stage ok" << std::endl;
}else {
std::cout << "Failure" << std::endl;
return "";
}
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return sid_final;
}

View File

@ -0,0 +1,12 @@
#ifndef _HTTP_CONNECTOR_H_
#define _HTTP_CONNECTOR_H_
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
std::string get_sid();
#endif

View File

@ -0,0 +1,3 @@
clang++ ../http/http_connector.cpp \
./main_http.cpp \
-o main_http -lcurl && ./main_http

BIN
nasfaq/connections/test/main Executable file

Binary file not shown.

View File

@ -0,0 +1,7 @@
#include "../http/http_connector.h"
#include "../ws/my_ssl.h"
int main(void) {
connect_ws(get_sid());
}

BIN
nasfaq/connections/test/main_http Executable file

Binary file not shown.

View File

@ -0,0 +1,6 @@
#include "../http/http_connector.h"
int main(void) {
get_sid();
}

4
nasfaq/connections/test/run.sh Executable file
View File

@ -0,0 +1,4 @@
clang++ ../http/http_connector.cpp \
../ws/my_ssl.cpp \
./main.cpp \
-o main -lcurl -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./main

BIN
nasfaq/connections/ws/a.out Executable file

Binary file not shown.

View File

@ -0,0 +1,335 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// **NOTE:** This file is a snapshot of the WebSocket++ utility client tutorial.
// Additional related material can be found in the tutorials/utility_client
// directory of the WebSocket++ repository.
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
class connection_metadata {
public:
typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
: m_id(id)
, m_hdl(hdl)
, m_status("Connecting")
, m_uri(uri)
, m_server("N/A")
{}
void on_open(client * c, websocketpp::connection_hdl hdl) {
m_status = "Open";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
}
void on_fail(client * c, websocketpp::connection_hdl hdl) {
m_status = "Failed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void on_close(client * c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
void on_message(websocketpp::connection_hdl, client::message_ptr msg) {
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
m_messages.push_back("<< " + msg->get_payload());
} else {
m_messages.push_back("<< " + websocketpp::utility::to_hex(msg->get_payload()));
}
}
websocketpp::connection_hdl get_hdl() const {
return m_hdl;
}
int get_id() const {
return m_id;
}
std::string get_status() const {
return m_status;
}
void record_sent_message(std::string message) {
m_messages.push_back(">> " + message);
}
friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
private:
int m_id;
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
std::vector<std::string> m_messages;
};
std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
out << "> URI: " << data.m_uri << "\n"
<< "> Status: " << data.m_status << "\n"
<< "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
<< "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason) << "\n";
out << "> Messages Processed: (" << data.m_messages.size() << ") \n";
std::vector<std::string>::const_iterator it;
for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
out << *it << "\n";
}
return out;
}
class websocket_endpoint {
public:
websocket_endpoint () : m_next_id(0) {
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
m_endpoint.init_asio();
m_endpoint.start_perpetual();
m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run, &m_endpoint);
}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
if (it->second->get_status() != "Open") {
// Only close open connections
continue;
}
std::cout << "> Closing connection " << it->second->get_id() << std::endl;
websocketpp::lib::error_code ec;
m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
if (ec) {
std::cout << "> Error closing connection " << it->second->get_id() << ": "
<< ec.message() << std::endl;
}
}
m_thread->join();
}
int connect(std::string const & uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if (ec) {
std::cout << "> Connect initialization error: " << ec.message() << std::endl;
return -1;
}
int new_id = m_next_id++;
connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
m_connection_list[new_id] = metadata_ptr;
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_fail_handler(websocketpp::lib::bind(
&connection_metadata::on_fail,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_close_handler(websocketpp::lib::bind(
&connection_metadata::on_close,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_message_handler(websocketpp::lib::bind(
&connection_metadata::on_message,
metadata_ptr,
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2
));
m_endpoint.connect(con);
return new_id;
}
void close(int id, websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
if (ec) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
void send(int id, std::string message) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec);
if (ec) {
std::cout << "> Error sending message: " << ec.message() << std::endl;
return;
}
metadata_it->second->record_sent_message(message);
}
connection_metadata::ptr get_metadata(int id) const {
con_list::const_iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
return connection_metadata::ptr();
} else {
return metadata_it->second;
}
}
private:
typedef std::map<int,connection_metadata::ptr> con_list;
client m_endpoint;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
con_list m_connection_list;
int m_next_id;
};
int main() {
bool done = false;
std::string input;
websocket_endpoint endpoint;
while (!done) {
std::cout << "Enter Command: ";
std::getline(std::cin, input);
if (input == "quit") {
done = true;
} else if (input == "help") {
std::cout
<< "\nCommand List:\n"
<< "connect <ws uri>\n"
<< "send <connection id> <message>\n"
<< "close <connection id> [<close code:default=1000>] [<close reason>]\n"
<< "show <connection id>\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
} else if (input.substr(0,7) == "connect") {
int id = endpoint.connect(input.substr(8));
if (id != -1) {
std::cout << "> Created connection with id " << id << std::endl;
}
} else if (input.substr(0,4) == "send") {
std::stringstream ss(input);
std::string cmd;
int id;
std::string message;
ss >> cmd >> id;
std::getline(ss,message);
endpoint.send(id, message);
} else if (input.substr(0,5) == "close") {
std::stringstream ss(input);
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
endpoint.close(id, close_code, reason);
} else if (input.substr(0,4) == "show") {
int id = atoi(input.substr(5).c_str());
connection_metadata::ptr metadata = endpoint.get_metadata(id);
if (metadata) {
std::cout << *metadata << std::endl;
} else {
std::cout << "> Unknown connection id " << id << std::endl;
}
} else {
std::cout << "> Unrecognized Command" << std::endl;
}
}
return 0;
}
/*
clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a
clang++ -I/Users/zaphoyd/software/websocketpp/ -I/Users/zaphoyd/software/boost_1_55_0/ step4.cpp /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_system.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_thread.a /Users/zaphoyd/software/boost_1_55_0/stage/lib/libboost_random.a
clang++ -std=c++11 -stdlib=libc++ -I/Users/zaphoyd/Documents/websocketpp/ -I/Users/zaphoyd/Documents/boost_1_53_0_libcpp/ -D_WEBSOCKETPP_CPP11_STL_ step4.cpp /Users/zaphoyd/Documents/boost_1_53_0_libcpp/stage/lib/libboost_system.a
*/

@ -0,0 +1 @@
Subproject commit 2c9c5b46bc1f5d3b0ae7623bd66eea4d9426a6c5

View File

@ -0,0 +1,335 @@
#include <iostream>
#include <string>
#include <stdlib.h>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;
class connection_metadata {
public:
typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
connection_metadata(client *endpoint, int id, websocketpp::connection_hdl hdl, std::string uri)
: m_id(id)
, m_hdl(hdl)
, m_status("Connecting")
, m_uri(uri)
, m_server("N/A")
, m_endpoint(endpoint)
{}
void on_open(client *c, websocketpp::connection_hdl hdl) {
websocketpp::lib::error_code ec;
m_status = "Open";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
sleep(1); /* How to wait for status? */
m_endpoint->send(m_hdl, "2probe", websocketpp::frame::opcode::text, ec);
if (ec) {
std::cout << "> Error sending 2probe: " << ec.message() << std::endl;
return;
}
sleep(1); /* How to wait for status? */
m_endpoint->send(m_hdl, "5", websocketpp::frame::opcode::text, ec);
if (ec) {
std::cout << "> Error sending 5: " << ec.message() << std::endl;
return;
}
}
void on_fail(client *c, websocketpp::connection_hdl hdl) {
m_status = "Failed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void on_close(client *c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
std::string payload = msg->get_payload();
if (payload == "2") {
websocketpp::lib::error_code ec;
m_endpoint->send(m_hdl, "3", websocketpp::frame::opcode::text, ec);
if (ec) {
std::cout << "> Error sending pong 3: " << ec.message() << std::endl;
return;
}
} else {
m_messages.push_back(payload);
}
} else {
m_messages.push_back(websocketpp::utility::to_hex(msg->get_payload()));
}
}
websocketpp::connection_hdl get_hdl() const {
return m_hdl;
}
int get_id() const {
return m_id;
}
std::string get_status() const {
return m_status;
}
friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
void record_sent_message(std::string message) {
m_messages.push_back(">> " + message);
}
private:
int m_id;
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
std::vector<std::string> m_messages;
client *m_endpoint;
};
std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
out
<< "> URI: " << data.m_uri << "\n"
<< "> Status: " << data.m_status << "\n"
<< "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
<< "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
std::vector<std::string>::const_iterator it;
for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
out << *it << "\n";
}
return out;
}
static context_ptr on_tls_init() {
// establishes a SSL connection
context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3 |
boost::asio::ssl::context::single_dh_use);
} catch (std::exception &e) {
std::cout << "Error in context pointer: " << e.what() << std::endl;
}
return ctx;
}
class websocket_endpoint {
public:
websocket_endpoint() {
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
m_endpoint.init_asio();
m_endpoint.set_tls_init_handler(bind(&on_tls_init));
m_endpoint.start_perpetual();
m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint));
}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
if (it->second->get_status() != "Open") {
// Close only open connections
continue;
}
std::cout << "> Closing connection " << it->second->get_id() << std::endl;
websocketpp::lib::error_code ec;
m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
if (ec) {
std::cout << "> Error closing connection " << it->second->get_id() << ": "
<< ec.message() << std::endl;
}
}
}
int connect(std::string const &uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
con->append_header("Cookie", "holosesh=s%3AxmS8xBlQk4kH_rXQOaNjHk_3OuaBDsfA.M0yi%2BZmkiq%2BAmJBRj%2FNg9S%2BaSQpsfHRJcEeYVHLiKXg");
if(ec) {
std::cout << ">Connect initialization error: " << ec.message() << std::endl;
return -1;
}
int new_id = m_next_id++;
connection_metadata::ptr metadata_ptr(new connection_metadata(&m_endpoint, new_id, con->get_handle(), uri));
m_connection_list[new_id] = metadata_ptr;
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_fail_handler(websocketpp::lib::bind(
&connection_metadata::on_fail,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_message_handler(websocketpp::lib::bind(
&connection_metadata::on_message,
metadata_ptr,
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2
));
m_endpoint.connect(con);
return new_id;
}
void close(int id, websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.close(metadata_it->second->get_hdl(), code, "", ec);
if (ec) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
void send(int id, std::string message) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.send(metadata_it->second->get_hdl(), message, websocketpp::frame::opcode::text, ec);
if (ec) {
std::cout << "> Error sending message: " << ec.message() << std::endl;
return;
}
metadata_it->second->record_sent_message(message);
}
connection_metadata::ptr get_metadata(int id) const {
con_list::const_iterator metadata_it = m_connection_list.find(id);
if(metadata_it == m_connection_list.end()) {
return connection_metadata::ptr();
} else {
return metadata_it->second;
}
}
private:
typedef std::map<int, connection_metadata::ptr> con_list;
client m_endpoint;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
con_list m_connection_list;
int m_next_id;
};
int connect_ws(std::string sid) {
bool done = false;
std::string input;
websocket_endpoint endpoint;
while(!done) {
std::cout << "Enter command: ";
std::getline(std::cin, input);
if(input == "quit" || input == "q" ) done = true;
else if(input == "help") {
std::cout
<< "\nCommand List:\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
} else if (input.substr(0, 3) == "nsf") {
std::string uri = "wss://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=websocket&sid=" + sid;
int id = endpoint.connect(uri);
if(id != -1) {
std::cout << ">Created nsf connection with id " << id << std::endl;
}
} else if (input.substr(0, 7) == "connect") {
int id = endpoint.connect(input.substr(8));
if(id != -1) {
std::cout << ">Created connection with id " << id << std::endl;
}
} else if (input.substr(0,4) == "show") {
int id = atoi(input.substr(5).c_str());
connection_metadata::ptr metadata = endpoint.get_metadata(id);
if (metadata) {
std::cout << *metadata << std::endl;
} else {
std::cout << ">Unknown connection id " << id << std::endl;
}
} else if (input.substr(0, 5) == "close") {
std::stringstream ss(input);
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss, reason);
endpoint.close(id, close_code, reason);
} else if (input.substr(0,4) == "send") {
std::stringstream ss(input);
std::string cmd;
int id;
std::string message = "";
ss >> cmd >> id;
std::getline(ss,message);
endpoint.send(id, message);
} else {
std::cout << "Unrecognized command" << std::endl;
}
}
return 0;
}

View File

@ -0,0 +1,7 @@
#ifndef _MY_SSL_H_
#define _MY_SSL_H_
int connect_ws(std::string);
#endif

@ -0,0 +1 @@
Subproject commit 1b11fd301531e6df35a6107c1e8665b1e77a2d8e

1
nasfaq/connections/ws/run.sh Executable file
View File

@ -0,0 +1 @@
clang++ my_ssl.cpp -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./a.out

View File

@ -0,0 +1,227 @@
#include <iostream>
#include <string>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
typedef websocketpp::client<websocketpp::config::asio_client> client;
class connection_metadata {
public:
typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
: m_id(id)
, m_hdl(hdl)
, m_status("Connecting")
, m_uri(uri)
, m_server("N/A")
{}
void on_open(client *c, websocketpp::connection_hdl hdl) {
m_status = "Open";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
}
void on_fail(client *c, websocketpp::connection_hdl hdl) {
m_status = "Failed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void on_close(client *c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
websocketpp::connection_hdl get_hdl() const {
return m_hdl;
}
int get_id() const {
return m_id;
}
std::string get_status() const {
return m_status;
}
friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
private:
int m_id;
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
};
std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
out << "> URI: " << data.m_uri << "\n"
<< "> Status: " << data.m_status << "\n"
<< "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
<< "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
return out;
}
class websocket_endpoint {
public:
websocket_endpoint() {
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
m_endpoint.init_asio();
m_endpoint.start_perpetual();
m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint));
}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
if (it->second->get_status() != "Open") {
// Close only open connections
continue;
}
std::cout << "> Closing connection " << it->second->get_id() << std::endl;
websocketpp::lib::error_code ec;
m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
if (ec) {
std::cout << "> Error closing connection " << it->second->get_id() << ": "
<< ec.message() << std::endl;
}
}
}
int connect(std::string const &uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if(ec) {
std::cout << ">Connect initialization error: " << ec.message() << std::endl;
return -1;
}
int new_id = m_next_id++;
connection_metadata::ptr metadata_ptr(new connection_metadata(new_id, con->get_handle(), uri));
m_connection_list[new_id] = metadata_ptr;
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_fail_handler(websocketpp::lib::bind(
&connection_metadata::on_fail,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
m_endpoint.connect(con);
return new_id;
}
void close(int id, websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.close(metadata_it->second->get_hdl(), code, "", ec);
if (ec) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
connection_metadata::ptr get_metadata(int id) const {
con_list::const_iterator metadata_it = m_connection_list.find(id);
if(metadata_it == m_connection_list.end()) {
return connection_metadata::ptr();
} else {
return metadata_it->second;
}
}
private:
typedef std::map<int, connection_metadata::ptr> con_list;
client m_endpoint;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
con_list m_connection_list;
int m_next_id;
};
int main() {
bool done = false;
std::string input;
websocket_endpoint endpoint;
while(!done) {
std::cout << "Enter command: ";
std::getline(std::cin, input);
if(input == "quit" || input == "q" ) done = true;
else if(input == "help") {
std::cout
<< "\nCommand List:\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
} else if (input.substr(0, 7) == "connect") {
int id = endpoint.connect(input.substr(8));
if(id != -1) {
std::cout << ">Created connection with id " << id << std::endl;
}
} else if (input.substr(0,4) == "show") {
int id = atoi(input.substr(5).c_str());
connection_metadata::ptr metadata = endpoint.get_metadata(id);
if (metadata) {
std::cout << *metadata << std::endl;
} else {
std::cout << ">Unknown connection id " << id << std::endl;
}
} else if (input.substr(0, 5) == "close") {
std::stringstream ss(input);
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss, reason);
endpoint.close(id, close_code, reason);
} else {
std::cout << "Unrecognized command" << std::endl;
}
}
return 0;
}

View File

@ -0,0 +1 @@
clang++ test_ssl.cpp -lssl -lcrypto -lboost_system -lboost_random -lboost_thread -lpthread && ./a.out

View File

@ -0,0 +1,86 @@
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef std::shared_ptr<boost::asio::ssl::context> context_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
websocketpp::lib::error_code ec;
c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
static context_ptr on_tls_init() {
// establishes a SSL connection
context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3 |
boost::asio::ssl::context::single_dh_use);
} catch (std::exception &e) {
std::cout << "Error in context pointer: " << e.what() << std::endl;
}
return ctx;
}
int main(int argc, char* argv[]) {
// Create a client endpoint
client c;
std::string uri = "wss://nasfaq.biz/socket/?user=314d0bda-d7f0-4636-aed7-5ea02743604b&EIO=4&transport=websocket";
if (argc == 2) {
uri = argv[1];
}
try {
// Set logging to be pretty verbose (everything except message payloads)
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
c.init_asio();
c.set_tls_init_handler(bind(&on_tls_init));
// Register our message handler
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
// Note that connect here only requests a connection. No network messages are
// exchanged until the event loop starts running in the next line.
c.connect(con);
// Start the ASIO io_service run loop
// this will cause a single connection to be made to the server. c.run()
// will exit when this connection is closed.
c.run();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
}

@ -0,0 +1 @@
Subproject commit 1088642db8abbcf9f034bab61f2496864235c042

4
nasfaq/ui/test/compile.sh Executable file
View File

@ -0,0 +1,4 @@
gcc -lncurses \
nc.c \
utils.c \
-o nc && ./nc

BIN
nasfaq/ui/test/nc Executable file

Binary file not shown.

82
nasfaq/ui/test/nc.c Normal file
View File

@ -0,0 +1,82 @@
#include "nc.h"
int main(int argc, char *argv[])
{
WINDOW *my_win;
int startx, starty, width, height;
int ch;
int margin_x, margin_y;
int MAX_ROW, MAX_COL;
int MAX_WIN_ROW, MAX_WIN_COL;
initscr(); /* Start curses mode */
raw(); /* Line buffering disabled, Pass on*/
noecho();
keypad(stdscr, TRUE); /* I need that nifty F2 */
/* Window parameters */
height = 10;
width = 20;
starty = (LINES - height) / 2; /* Calculating for a center placement */
startx = (COLS - width) / 2; /* of the window */
/* Grid parameters */
getmaxyx(stdscr, MAX_ROW, MAX_COL); /* get the number of rows and columns */
margin_x = 1;
margin_y = 1;
MAX_WIN_ROW = MAX_ROW / (height + margin_y);
MAX_WIN_COL = MAX_COL / (width + margin_x);
WINDOW *win_array[MAX_WIN_COL][MAX_WIN_ROW];
printw("Press F2 to exit");
mvprintw(2, 0, "Nbr of win col: %d, nbr of win lines: %d", MAX_WIN_COL, MAX_WIN_ROW);
refresh();
do {
for( int i = 0; i < MAX_WIN_COL; i ++ ){
for( int j = 0; j < MAX_WIN_ROW; j ++ ){
win_array[i][j] = create_newwin(height, width, j * (height + margin_y), i * (width + margin_x));
refresh();
}
}
}while( (ch = getch()) != KEY_F(2));
//for( int i = 0; i <= MAX_WIN_COL; i ++ ){
// for( int j = 0; j <= MAX_WIN_ROW; j ++ ){
// destroy_win(win_array[i][j]);
// }
//}
//while((ch = getch()) != KEY_F(2))
//{
// switch(ch)
// {
// case KEY_LEFT:
// destroy_win(my_win);
// my_win = create_newwin(height, width, starty,--startx);
// break;
// case KEY_RIGHT:
// destroy_win(my_win);
// my_win = create_newwin(height, width, starty,++startx);
// break;
// case KEY_UP:
// destroy_win(my_win);
// my_win = create_newwin(height, width, --starty,startx);
// break;
// case KEY_DOWN:
// destroy_win(my_win);
// my_win = create_newwin(height, width, ++starty,startx);
// break;
// }
//}
endwin(); /* End curses mode */
return 0;
}

7
nasfaq/ui/test/nc.h Normal file
View File

@ -0,0 +1,7 @@
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"

39
nasfaq/ui/test/utils.c Normal file
View File

@ -0,0 +1,39 @@
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters
for the vertical and horizontal
lines */
wrefresh(local_win); /* Show that box */
return local_win;
}
void destroy_win(WINDOW *local_win)
{
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window. */
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window */
wrefresh(local_win);
delwin(local_win);
}

9
nasfaq/ui/test/utils.h Normal file
View File

@ -0,0 +1,9 @@
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);