Skip to content
Snippets Groups Projects
Select Git revision
  • a95bd13a2df2dcb3bf6f1dc084b3a482673be3b4
  • master default protected
2 results

pgcd.c

  • Forked from algorithmique / cours
    Source project has a limited visibility.
    client.go 2.20 KiB
    package main
    
    import (
    	"bufio"
    	"fmt"
    	"net"
    	"os"
    	"strings"
    )
    
    func client() {
    	println("Create your transaction: ")
    	println("ID of transaction:")
    	reader := bufio.NewReader(os.Stdin)
    	input, _ := reader.ReadString('\n')
    	input = strings.TrimSuffix(input, "\n")
    	message := input
    	println("Sender:")
    	reader = bufio.NewReader(os.Stdin)
    	input, _ = reader.ReadString('\n')
    	input = strings.TrimSuffix(input, "\n")
    	message = message + " " + input
    	println("Receiver:")
    	reader = bufio.NewReader(os.Stdin)
    	input, _ = reader.ReadString('\n')
    	input = strings.TrimSuffix(input, "\n")
    	message = message + " " + input
    	println("Amount:")
    	reader = bufio.NewReader(os.Stdin)
    	input, _ = reader.ReadString('\n')
    	input = strings.TrimSuffix(input, "\n")
    	message = message + " " + input
    	goodInput := false
    	for !goodInput {
    		println("Is it fake?(Y/N):")
    		reader = bufio.NewReader(os.Stdin)
    		char, _, err := reader.ReadRune()
    		if err != nil {
    			fmt.Println(err)
    		}
    		switch char {
    		case 'Y':
    			message = message + " " + "false"
    			goodInput = true
    		case 'N':
    			message = message + " " + "true"
    			goodInput = true
    		default:
    			println("type Y or N:")
    		}
    	}
    	goodInput = false
    	for !goodInput {
    		println("What do you want to do with it? creantTrans or Rate?(C/R):")
    		reader = bufio.NewReader(os.Stdin)
    		char, _, err := reader.ReadRune()
    		if err != nil {
    			fmt.Println(err)
    		}
    		switch char {
    		case 'C':
    			create_transaction("M" + message)
    			goodInput = true
    		case 'R':
    			rate("N" + message)
    			goodInput = true
    		default:
    			println("type C or R:")
    		}
    	}
    }
    
    func rate(trans string) {
    	conn, err := net.Dial("tcp", "34.67.47.51:8000")
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	// what to send
    	fmt.Fprintf(conn, trans+"\n")
    	conn.Close()
    	ln, err := net.Listen("tcp", ":9000") //marche pas en cloud
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	con, err := ln.Accept()
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	message, _ := bufio.NewReader(con).ReadString('\n')
    	fmt.Print("Message from server: " + message)
    }
    
    func create_transaction(trans string) {
    	conn, err := net.Dial("tcp", "34.67.47.51:8000")
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	// what to send
    	fmt.Fprintf(conn, trans+"\n")
    	conn.Close()
    }