September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,38 +0,0 @@
|
|||
module distributedserver ;
|
||||
import tango.net.ServerSocket, tango.text.convert.Integer,
|
||||
tango.text.Util, tango.io.Stdout ;
|
||||
|
||||
void main() {
|
||||
auto Ip = new InternetAddress("localhost", 12345) ;
|
||||
auto server = new ServerSocket(Ip) ;
|
||||
auto socket = server.accept ;
|
||||
auto buffer = new char[socket.bufferSize] ;
|
||||
|
||||
bool quit = false ;
|
||||
|
||||
while(!quit) {
|
||||
bool error = false ;
|
||||
|
||||
try {
|
||||
auto len = socket.input.read(buffer) ;
|
||||
auto cmd = (len > 0) ? delimit(buffer[0..len], " ") : [""] ;
|
||||
Stdout(cmd).newline.flush ;
|
||||
switch (cmd[0]) {
|
||||
case "square":
|
||||
socket.output.write(toString(toInt(cmd[1]) * toInt(cmd[1]))) ; break ;
|
||||
case"add":
|
||||
socket.output.write(toString(toInt(cmd[1]) + toInt(cmd[2]))) ; break ;
|
||||
case "quit":
|
||||
socket.output.write("Server Shut down") ;
|
||||
quit = true ; break ;
|
||||
default: error = true ;
|
||||
}
|
||||
} catch (Exception e)
|
||||
error = true ;
|
||||
if(error) socket.output.write("<Error>") ;
|
||||
if(socket) socket.close ;
|
||||
if(!quit) socket = server.accept ;
|
||||
}
|
||||
|
||||
if(socket) socket.close ;
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
module distributedclient ;
|
||||
import tango.net.SocketConduit, tango.net.InternetAddress,
|
||||
tango.text.Util, tango.io.Stdout ;
|
||||
|
||||
void main(char[][] args) {
|
||||
|
||||
if(args.length> 1) {
|
||||
try {
|
||||
auto Ip = new InternetAddress("localhost", 12345) ;
|
||||
auto socket = new SocketConduit ;
|
||||
socket.connect(Ip) ;
|
||||
auto buffer = new char[socket.bufferSize] ;
|
||||
|
||||
socket.output.write(join(args[1..$]," ")) ;
|
||||
auto len = socket.input.read(buffer) ;
|
||||
if(len > 0) Stdout(buffer[0..len]).newline ;
|
||||
|
||||
if(socket) socket.close ;
|
||||
} catch(Exception e)
|
||||
Stdout(e.msg).newline ;
|
||||
} else
|
||||
Stdout("usage: supply argument as,\n\tquit\n"
|
||||
"\tsquare <number>\n\tadd <number> <number>").newline ;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
syntax = "proto3";
|
||||
|
||||
service TaxComputer {
|
||||
rpc Tax(Amount) returns (Amount) {}
|
||||
}
|
||||
|
||||
message Amount {
|
||||
int32 cents = 1;
|
||||
}
|
||||
34
Task/Distributed-programming/Go/distributed-programming-4.go
Normal file
34
Task/Distributed-programming/Go/distributed-programming-4.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
|
||||
"taxcomputer"
|
||||
)
|
||||
|
||||
type taxServer struct {
|
||||
rate float64
|
||||
}
|
||||
|
||||
func (s *taxServer) Tax(ctx context.Context,
|
||||
amt *taxcomputer.Amount) (*taxcomputer.Amount, error) {
|
||||
if amt.Cents < 0 {
|
||||
return nil, errors.New("Negative amounts not allowed")
|
||||
}
|
||||
return &taxcomputer.Amount{int32(float64(amt.Cents)*s.rate + .5)}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
listener, err := net.Listen("tcp", ":1234")
|
||||
if err != nil {
|
||||
grpclog.Fatalf(err.Error())
|
||||
}
|
||||
grpcServer := grpc.NewServer()
|
||||
taxcomputer.RegisterTaxComputerServer(grpcServer, &taxServer{.05})
|
||||
grpcServer.Serve(listener)
|
||||
}
|
||||
26
Task/Distributed-programming/Go/distributed-programming-5.go
Normal file
26
Task/Distributed-programming/Go/distributed-programming-5.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
|
||||
"taxcomputer"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
|
||||
if err != nil {
|
||||
grpclog.Fatalf(err.Error())
|
||||
}
|
||||
defer conn.Close()
|
||||
client := taxcomputer.NewTaxComputerClient(conn)
|
||||
amt := &taxcomputer.Amount{300}
|
||||
tax, err := client.Tax(context.Background(), amt)
|
||||
if err != nil {
|
||||
grpclog.Fatalf(err.Error())
|
||||
}
|
||||
fmt.Println("Tax on", amt.Cents, "cents is", tax.Cents, "cents")
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
service TaxService {
|
||||
i32 tax(1: i32 amt)
|
||||
}
|
||||
33
Task/Distributed-programming/Go/distributed-programming-7.go
Normal file
33
Task/Distributed-programming/Go/distributed-programming-7.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"git.apache.org/thrift.git/lib/go/thrift"
|
||||
|
||||
"gen-go/tax"
|
||||
)
|
||||
|
||||
type taxHandler float64
|
||||
|
||||
func (r taxHandler) Tax(amt int32) (int32, error) {
|
||||
if amt < 0 {
|
||||
return 0, errors.New("Negative amounts not allowed")
|
||||
}
|
||||
return int32(float64(amt)*float64(r) + .5), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
transport, err := thrift.NewTServerSocket("localhost:3141")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
transFac := thrift.NewTTransportFactory()
|
||||
protoFac := thrift.NewTCompactProtocolFactory()
|
||||
proc := tax.NewTaxServiceProcessor(taxHandler(.05))
|
||||
s := thrift.NewTSimpleServer4(proc, transport, transFac, protoFac)
|
||||
if err := s.Serve(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
30
Task/Distributed-programming/Go/distributed-programming-8.go
Normal file
30
Task/Distributed-programming/Go/distributed-programming-8.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.apache.org/thrift.git/lib/go/thrift"
|
||||
|
||||
"gen-go/tax"
|
||||
)
|
||||
|
||||
func main() {
|
||||
transport, err := thrift.NewTSocket("localhost:3141")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := transport.Open(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
protoFac := thrift.NewTCompactProtocolFactory()
|
||||
client := tax.NewTaxServiceClientFactory(transport, protoFac)
|
||||
amt := int32(300)
|
||||
t, err := client.Tax(amt)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
fmt.Println("tax on", amt, "is", t)
|
||||
}
|
||||
transport.Close()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue