#include "talk.hh"
#include <unistd.h>
// Get the Id of the Server-Task
// If it doesn't exist yet, create it.
Pvm::Task GetServerTask ()
{
// Get all tasks in the pvm
Pvm::TaskSet AllTasks;
Pvm::Pvm ().Tasks (AllTasks);
// The absolute path of the server
std::string ServerPath = SERVERNAME;
// Check every task, if it is the server.
Pvm::TaskSet::iterator Current;
for (Current = AllTasks.begin (); Current != AllTasks.end (); ++Current)
{
// If so, return the coressponding Pvm::Task
if ((*Current).Name () == ServerPath)
return *Current;
}
// Else, start server on my host anew
return Pvm::Pvm ().I ().Host ().Spawn (ServerPath);
}
int
main (int argc, char **argv)
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " YourName." << std::endl;
exit (1);
}
std::cerr << "End the session with a single \".\" on a line" << std::endl;
Pvm::Task Server = GetServerTask ();
Register.Send (Server);
Message In;
Message Out;
Out.Name = argv[1];
Pvm::StructSet Awaited;
Awaited.insert (In);
Awaited.ReadFDs ().insert (STDIN_FILENO);
while (true)
{
if (0 == Awaited.ReceiveFrom (Server))
{
// New data written to stdin
std::getline (std::cin, Out.String);
if (Out.String == ".")
break;
Out.Send (Server);
}
else
std::cout << In.Name << ":" << In.String << std::endl;
}
Leaving.Send (Server);
}
syntax highlighted by Code2HTML, v. 0.9.1