Distributed programming: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 1,150:
=={{header|Perl}}==
Using Data::Dumper and Safe to transmit arbitrary data structures as serialized text between hosts. Same code works as both sender and receiver.
<syntaxhighlight lang="perl">use Data::Dumperstrict;
use warnings;
use Data::Dumper;
use IO::Socket::INET;
use Safe;
 
sub get_data {
my $sock = new IO::Socket::INET->new(
LocalHost => "localhost",
LocalPort => "10000",
Proto => "tcp",
Listen => 1,
Reuse => 1);
unless ($sock) { die "Socket creation failure" }
my $cli = $sock->accept();
Line 1,166 ⟶ 1,168:
# of course someone may be tempted to send you 'system("rm -rf /")',
# to be safe(r), use Safe::
my $safe = new Safe->new;
my $x = $safe->reval(join("", <$cli>));
close $cli;
Line 1,176 ⟶ 1,178:
my $host = shift;
my $data = shift;
my $sock = new IO::Socket::INET->new(
PeerAddr => "$host:10000",
Proto => "tcp",
Reuse => 1);
 
unless ($sock) { die "Socket creation failure" }
Line 1,929 ⟶ 1,931:
<br>
We need two Wren scripts one for each VM:
<syntaxhighlight lang="ecmascriptwren">/* distributed_programming_serverDistributed_programming_server.wren */
 
class Rpc {
Line 1,950 ⟶ 1,952:
HTTP.serve(listener)</syntaxhighlight>
<br>
<syntaxhighlight lang="ecmascriptwren">/* distributed_programming_server2Distributed_programming_server_2.wren */
 
class TaxComputer {
Line 1,960 ⟶ 1,962:
<br>
We now embed these scripts in the following Go program and run it on one terminal.
<syntaxhighlight lang="go">/* go run distributed_programming_serverDistributed_programming_server.go */
 
package main
Line 1,978 ⟶ 1,980:
var vm2 *wren.VM
 
var fileName = "distributed_programming_serverDistributed_programming_server.wren"
var fileName2 = "distributed_programming_server2Distributed_programming_server_2.wren"
 
func (taxRate TaxComputer) Tax(x float64, r *float64) error {
Line 2,049 ⟶ 2,051:
<br>
Just one Wren script needed here:
<syntaxhighlight lang="ecmascriptwren">/* distributed_programming_clientDistributed_programming_client.wren */
 
import "./fmt" for Fmt
Line 2,067 ⟶ 2,069:
<br>
which we embed in the following Go program and run it on a different terminal.
<syntaxhighlight lang="go">/* go run distributed_programming_clientDistributed_programming_client.go */
 
package main
Line 2,115 ⟶ 2,117:
cfg.LoadModuleFn = moduleFn
vm := cfg.NewVM()
fileName := "distributed_programming_clientDistributed_programming_client.wren"
clientMethodMap := wren.MethodMap { "call(_,_)": call }
classMap := wren.ClassMap { "Client": wren.NewClass(dialHTTP, nil, clientMethodMap) }
9,483

edits