Sync
- Implemented the high-level communications, still without TLS. - Includes temporary diagnostics, and unlocalized error messages. These will converge upon proper and useful error messages as the command matures.
This commit is contained in:
@@ -50,6 +50,8 @@ CmdSync::CmdSync ()
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int CmdSync::execute (std::string& output)
|
int CmdSync::execute (std::string& output)
|
||||||
{
|
{
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
// If no server is set up, quit.
|
// If no server is set up, quit.
|
||||||
std::string connection = context.config.get ("taskd.server");
|
std::string connection = context.config.get ("taskd.server");
|
||||||
if (connection == "" ||
|
if (connection == "" ||
|
||||||
@@ -59,22 +61,73 @@ int CmdSync::execute (std::string& output)
|
|||||||
// Obtain credentials.
|
// Obtain credentials.
|
||||||
std::string credentials = context.config.get ("taskd.credentials");
|
std::string credentials = context.config.get ("taskd.credentials");
|
||||||
|
|
||||||
// TODO Read backlog.data.
|
// Read backlog.data.
|
||||||
// TODO Send backlog.data in 'sync' request..
|
std::string payload = "";
|
||||||
|
File backlog (context.config.get ("data.location") + "/backlog.data");
|
||||||
|
if (backlog.exists ())
|
||||||
|
backlog.read (payload);
|
||||||
|
|
||||||
// TODO Receive response.
|
// Send 'sync' + payload.
|
||||||
|
Msg request, response;
|
||||||
|
request.set ("type", "sync");
|
||||||
|
// TODO Add the other header fields.
|
||||||
|
|
||||||
|
request.setPayload (payload);
|
||||||
|
std::cout << "# request:\n"
|
||||||
|
<< request.serialize ();
|
||||||
|
|
||||||
|
context.debug ("sync with " + connection);
|
||||||
|
if (send (connection, request, response))
|
||||||
|
{
|
||||||
|
std::cout << "# response:\n"
|
||||||
|
<< response.serialize ();
|
||||||
|
if (response.get ("code") == "200")
|
||||||
|
{
|
||||||
|
payload = response.getPayload ();
|
||||||
|
std::vector <std::string> lines;
|
||||||
|
split (lines, payload, '\n');
|
||||||
|
|
||||||
|
std::string synch_key;
|
||||||
|
std::vector <std::string>::iterator line;
|
||||||
|
for (line = lines.begin (); line != lines.end (); ++line)
|
||||||
|
{
|
||||||
|
if ((*line)[0] == '[')
|
||||||
|
{
|
||||||
// TODO Apply tasks.
|
// TODO Apply tasks.
|
||||||
|
std::cout << "# task: " << *line << "\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
synch_key = *line;
|
||||||
|
context.debug ("Synch key " + synch_key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Truncate backlog.data.
|
// TODO Truncate backlog.data.
|
||||||
// TODO Store new synch key.
|
// TODO Store new synch key.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.error ("Task Server problem.");
|
||||||
|
status = 2;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
// TODO Display all errors returned.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.error ("Could not connect to Task Server.");
|
||||||
|
status = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool CmdSync::send (
|
bool CmdSync::send (
|
||||||
const std::string& to,
|
const std::string& to,
|
||||||
const Msg& out,
|
const Msg& request,
|
||||||
Msg& in)
|
Msg& response)
|
||||||
{
|
{
|
||||||
std::string::size_type colon = to.find (':');
|
std::string::size_type colon = to.find (':');
|
||||||
if (colon == std::string::npos)
|
if (colon == std::string::npos)
|
||||||
@@ -87,21 +140,22 @@ bool CmdSync::send (
|
|||||||
{
|
{
|
||||||
Socket s (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
Socket s (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
s.connect (server, port);
|
s.connect (server, port);
|
||||||
s.write (out.serialize () + "\r\n");
|
s.write (request.serialize () + "\r\n");
|
||||||
|
|
||||||
std::string response;
|
std::string incoming;
|
||||||
s.read (response);
|
s.read (incoming);
|
||||||
s.close ();
|
s.close ();
|
||||||
|
|
||||||
in.parse (response);
|
response.parse (incoming);
|
||||||
|
|
||||||
// Indicate message sent.
|
// Indicate message sent.
|
||||||
|
context.debug ("sync tx complete");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (std::string& error)
|
catch (std::string& error)
|
||||||
{
|
{
|
||||||
// TODO Report as diagnostics?
|
context.error (error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indicate message failed.
|
// Indicate message failed.
|
||||||
|
|||||||
Reference in New Issue
Block a user