Skip to content

Instantly share code, notes, and snippets.

@agam
Created November 26, 2013 22:49
Show Gist options
  • Select an option

  • Save agam/7667732 to your computer and use it in GitHub Desktop.

Select an option

Save agam/7667732 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Timespan.h"
using namespace std;
int getNextDigit() {
static int next_digit = -1;
++next_digit;
return next_digit;
}
int main() {
Poco::Net::ServerSocket srv(9090);
cout << "Waiting for clients ..." << endl;
// Arbitrary number of clients are supported. Each client is given the 'next'
// digit once it's done.
double mysum = 3.0; // used to sanity check
static const float kComparisonMultiplier = 10000.0;
for (;;) {
Poco::Net::StreamSocket ss = srv.acceptConnection();
ss.setReceiveTimeout(20000);
ss.setSendTimeout(20000);
int last_digit;
unsigned int computed_value;
char client_buf[10];
ss.receiveBytes(client_buf, 10);
sscanf(client_buf, "%d-%u", &last_digit, &computed_value);
cout << "Received " << last_digit
<< "th digit as " << hex << computed_value << dec << endl;
if (computed_value != 1000) {
mysum += (1.0 * computed_value / pow(16.0, (last_digit + 1)));
cout << " Divergence from PI "
<< "(to " << kComparisonMultiplier << " accuracy) so far = "
<< fabs((mysum - M_PI) * kComparisonMultiplier) << endl;
}
// Send next command
int next_digit = getNextDigit();
snprintf(client_buf, 10, "*%d*", next_digit);
ss.sendBytes(client_buf, 10);
cout << "Sent " << client_buf << endl;
}
return 0;
}
/*
* Input Description
* There is no formal input description, though this is the desired behavior:
* You launch your main dispatcher-application on Computer A. You then launch
* the computing-applications on Computer W, X, Y and Z. Computer A wants to
* compute the first four digits of Pi, and sends the appropriate network
* commands, one to each computer. Computer Y returns a result first, so the
* dispatcher receives the data, saves in your output file the line "0:2", and
* then gives the command to compute the 5th digit of Pi. Computers X, Z, W
* finish in that order, returning the results to the dispatcher, which in turn
* saves in the same format. They are then asked to compute digits 6, 7, and 8.
* This repeats until your dispatcher application sends a "stop" or "kill"
* command to the computing processes. It is up to you how many hexadecimal
* digits each process computes.
* Output Description
* For each computed base-16 (hexadecimal) digit of Pi, write to a file a line
* of text in the format of <Digit-Index>:<Computed-Digit>. The order does not
* matter, and you may skip digits. An example of the file, after eight computed
* digits, would be as follows:
* 0:2
* 1:4
* 2:3
* 3:F
* 4:6
* 5:A
* 6:8
* 7:8
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment