Skip to content

Commit

Permalink
sync works
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Nov 13, 2024
1 parent 1db2091 commit 10dbd31
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ sync.o: sync.cc sync.h server.o

server.o: server.cc server.h client.cc client.h
$(CPP) -c server.cc client.cc $(GTK) -Wall

echo-client: echo-client.cc server.o
$(CPP) -o echo-client echo-client.cc client.cc $(GTK) -Wall

Binary file modified amprack
Binary file not shown.
19 changes: 13 additions & 6 deletions client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Client::send_preset(json j) {
// get a response
string response = "";
// read until we get a newline
while (response.find("}}") == string::npos) {
while (response.find("}}}") == string::npos) {
int nread = recv(server_,buf_,1024,0);
if (nread < 0) {
if (errno == EINTR) {
Expand All @@ -120,17 +120,22 @@ Client::send_preset(json j) {
HERE LOGD ("[92] break if an error occurred\n");
}

close_socket();
//~ close_socket();
OUT
return response ;
}

bool
Client::send_request(string request) {
//~ printf ("----------------------- -|prepare to send request: %s |- ---------------------------\n",
//~ request.c_str ());
IN
///| WARNING: we append another } to the message so that we know when to stop reading
///| HACKETY HACKETY HACK
request.append ("}");
printf ("----------------------- -|prepare to send request: %s |- ---------------------------\n",
request.c_str ());
const char* ptr = request.c_str();
int nleft = request.length();
int nleft_ = request.length();
int nwritten;
// loop to be sure it is all sent
while (nleft) {
Expand All @@ -150,6 +155,8 @@ Client::send_request(string request) {
nleft -= nwritten;
ptr += nwritten;
}
LOGD ("left %d / %d bytes to write\n", nleft, nleft_);
OUT
return true;
}

Expand All @@ -158,15 +165,15 @@ Client::get_response() {
IN
string response = "";
// read until we get a newline
while (response.find("}}") == string::npos) {
while (response.find("}}}") == string::npos) {
int nread = recv(server_,buf_,1024,0);
if (nread < 0) {
if (errno == EINTR) {
printf ("the socket call was interrupted -- try again\n");
continue;
}
else {
HERE LOGD ("an error occurred, so break out\n");
HERE LOGD ("[%s] an error occurred, so break out\n", strerror (errno));
return "";
}
} else if (nread == 0) {
Expand Down
4 changes: 2 additions & 2 deletions client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class Client {
std::string send_preset(json j) ;
virtual void create();
virtual void close_socket();
void echo();
bool get_response();

private:
void echo();
bool send_request(string);
bool get_response();

string host_;
int port_;
Expand Down
Binary file modified client.o
Binary file not shown.
Binary file added echo-client
Binary file not shown.
43 changes: 43 additions & 0 deletions echo-client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdlib.h>
#include <unistd.h>

#include <iostream>

#include "client.h"

using namespace std;

int
main(int argc, char **argv)
{
int option;

// setup default arguments
int port = 6906;
string host = "localhost";

// process command line options using getopt()
// see "man 3 getopt"
while ((option = getopt(argc,argv,"h:p:")) != -1) {
switch (option) {
case 'p':
port = atoi(optarg);
break;
case 'h':
host = optarg;
break;
default:
cout << "client [-h host] [-p port]" << endl;
exit(EXIT_FAILURE);
}
}

Client client = Client(host, port);
std::ifstream ifs(argv [1]);
json j = json::parse(ifs);
client.create ();
cout << client.send_preset (j) << endl;

//~ client.get_response ();
}

6 changes: 6 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "main.h"
#include "sync.h"

const char * renderers [7] = {
"auto",
"cairo",
Expand Down Expand Up @@ -149,10 +151,14 @@ hotkeys (MyWindow *window,
GtkEventControllerKey *event_controller)
{
//~ printf ("[keypress] %d\n", keyval);
Sync * sync ;
switch (keyval) {
case 65365:
window -> rack -> next_preset ();
break ;
case 115: // 's'
sync = new Sync (window -> rack);
break ;
case 65366:
window -> rack -> prev_preset ();
break ;
Expand Down
Binary file modified main.o
Binary file not shown.
3 changes: 2 additions & 1 deletion presets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void Presets::add_preset_multi (json j, int which) {
OUT
}

void Presets::import_presets_from_json (json j) {
int Presets::import_presets_from_json (json j) {
IN
int how_many = 0 ;
LOGV (presets_dir->c_str());
Expand All @@ -514,6 +514,7 @@ void Presets::import_presets_from_json (json j) {
}

msg (std::string ("Imported ").append (std::to_string (how_many)).append (" presets successfully."));
return how_many ;
}

void Presets::save_presets_to_json (std::string filename) {
Expand Down
2 changes: 1 addition & 1 deletion presets.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Presets {
void load_user (bool);
void add_preset_multi (json, int) ;
void add_preset_multi (std::string, int) ;
void import_presets_from_json (json);
int import_presets_from_json (json);
void save_presets_to_json (std::string);
std::string dir, * presets_dir, favs_dir ;
std::vector <json> * list_of_presets [4] ;
Expand Down
Binary file modified presets.o
Binary file not shown.
Binary file modified rack.o
Binary file not shown.
28 changes: 21 additions & 7 deletions server.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "server.h"
#include "sync.h"

Server::Server() {
IN
Expand Down Expand Up @@ -106,8 +107,16 @@ Server::handle(int client) {
break;
// send response
LOGD ("[server] request: %s\n", request.c_str ());
//~ json j = json::parse (request);
//~ presets->import_presets_from_json (j);
request.pop_back () ; ///| aargh!
json j = json::parse (request);
int how_many = presets->import_presets_from_json (j);
char * ss = g_strdup_printf ("<span foreground=\"green\" weight=\"bold\" size=\"x-large\">Imported %d presets successfully</span>", how_many);
Sync * _sync = (Sync *) sync ;
gtk_label_set_markup (_sync -> header, ss);
g_free (ss);

LOGD ("[server] synced %d presets\n", how_many);

bool success = send_response(client,request);
// break if an error occurred
if (not success) {
Expand All @@ -122,10 +131,11 @@ Server::handle(int client) {

string
Server::get_request(int client) {
IN
string request = "";
// read until we get a newline
int count = -1 ;
while (request.find("}}") == string::npos) {
while (request.find("}}}") == string::npos) {
count ++ ;

int nread = recv(client,buf_,1024,0);
Expand All @@ -135,7 +145,7 @@ Server::get_request(int client) {
continue;
}
else {
HERE LOGD ("an error occurred, so break out\n");
HERE LOGD ("[%s] an error occurred, so break out\n", strerror (errno));
return "";
}
} else if (nread == 0) {
Expand All @@ -151,15 +161,18 @@ Server::get_request(int client) {
LOGD ("[server] read ended: \n");
// a better server would cut off anything after the newline and
// save it in a cache
OUT
return request;
}

bool
Server::send_response(int client, string response) {
IN
// prepare to send response
response.append ("}");
const char* ptr = response.c_str();
int nleft = response.length();
int nleft_ = response.length();
int nwritten;
// loop to be sure it is all sent
while (nleft) {
Expand All @@ -169,7 +182,7 @@ Server::send_response(int client, string response) {
LOGD ("the socket call was interrupted -- try again\n");
continue;
} else {
HERE LOGD ("an error occurred, so break out\n");
HERE LOGD ("[%s] an error occurred, so break out\n", strerror (errno));
perror("write");
OUT
return false;
Expand All @@ -182,8 +195,9 @@ Server::send_response(int client, string response) {
nleft -= nwritten;
ptr += nwritten;
}
OUT


LOGD ("[send response] left %d of %d bytes\n", nleft, nleft_);
OUT
//~ close_socket ();
return true;
}
1 change: 1 addition & 0 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Server {
~Server();

Presets * presets ;
void * sync ;
void run();

GMainContext * context ;
Expand Down
Binary file modified server.o
Binary file not shown.
13 changes: 11 additions & 2 deletions sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ void sync_send (Sync * sync) {
std::string response = client -> send_preset (j);
LOGD ("[client] response: %s\n", response.c_str ());
LOGD ("ip: %s, port: %s, key: %s\n", ip.c_str(), port.c_str (), key.c_str ());

int how_many = p -> import_presets_from_json (j);
char * ss = g_strdup_printf ("<span foreground=\"green\" weight=\"bold\" size=\"x-large\">Imported %d presets successfully</span>", how_many);
gtk_label_set_markup (sync -> header, ss);
g_free (ss);

LOGD ("[client] synced %d presets\n", how_many);
client -> close_socket ();
OUT
}

void run_server (Sync * sync) {
IN
sync -> server = new Server();
sync -> server -> sync = sync ;
sync -> server -> presets = (Presets *) sync -> rack -> presets ;
sync -> server->run();

Expand Down Expand Up @@ -56,7 +65,7 @@ Sync::Sync (Rack * r) {
gtk_widget_set_margin_bottom (GW grid, 10);
gtk_widget_set_margin_end (GW grid, 10);

GtkLabel * header = (GtkLabel *) gtk_label_new ("Sync Presets");
header = (GtkLabel *) gtk_label_new ("Sync Presets");
gtk_label_set_justify (header, GTK_JUSTIFY_CENTER);
gtk_label_set_markup (header, "<span weight='bold' size='xx-large'>Sync Amp Rack Presets</span>");
// col row w h
Expand All @@ -82,7 +91,7 @@ Sync::Sync (Rack * r) {

g_signal_connect_swapped (btn, "clicked", (GCallback) sync_send, this);

GtkLabel * status = (GtkLabel *) gtk_label_new ("IP:PORT:KEY") ;
status = (GtkLabel *) gtk_label_new ("IP:PORT:KEY") ;

gtk_grid_attach (grid, GW entry, 0, 6, 2, 1);
gtk_grid_attach (grid, GW btn, 2, 6, 1, 1);
Expand Down
2 changes: 2 additions & 0 deletions sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Sync {
public:
GtkEntry * entry ;
Server * server ;
GtkLabel * status ;
GtkLabel * header ;
GtkWindow * window ;
std::thread * t;
Rack * rack;
Expand Down
Binary file modified sync.o
Binary file not shown.
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define VERSION 153
#define VERSION 154

0 comments on commit 10dbd31

Please sign in to comment.