github.com/uuosio/chaintester@v0.0.0-20230731100329-1f6fad7372e5/interfaces/apply_request-remote/apply_request-remote.go (about)

     1  // Code generated by Thrift Compiler (0.15.0). DO NOT EDIT.
     2  
     3  package main
     4  
     5  import (
     6  	"context"
     7  	"flag"
     8  	"fmt"
     9  	"math"
    10  	"net"
    11  	"net/url"
    12  	"os"
    13  	"strconv"
    14  	"strings"
    15  	thrift "github.com/apache/thrift/lib/go/thrift"
    16  	"interfaces"
    17  )
    18  
    19  var _ = interfaces.GoUnusedProtection__
    20  
    21  func Usage() {
    22    fmt.Fprintln(os.Stderr, "Usage of ", os.Args[0], " [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:")
    23    flag.PrintDefaults()
    24    fmt.Fprintln(os.Stderr, "\nFunctions:")
    25    fmt.Fprintln(os.Stderr, "  i32 apply_request(Uint64 receiver, Uint64 firstReceiver, Uint64 action, i32 chainTesterId)")
    26    fmt.Fprintln(os.Stderr, "  i32 apply_end(i32 chainTesterId)")
    27    fmt.Fprintln(os.Stderr)
    28    os.Exit(0)
    29  }
    30  
    31  type httpHeaders map[string]string
    32  
    33  func (h httpHeaders) String() string {
    34    var m map[string]string = h
    35    return fmt.Sprintf("%s", m)
    36  }
    37  
    38  func (h httpHeaders) Set(value string) error {
    39    parts := strings.Split(value, ": ")
    40    if len(parts) != 2 {
    41      return fmt.Errorf("header should be of format 'Key: Value'")
    42    }
    43    h[parts[0]] = parts[1]
    44    return nil
    45  }
    46  
    47  func main() {
    48    flag.Usage = Usage
    49    var host string
    50    var port int
    51    var protocol string
    52    var urlString string
    53    var framed bool
    54    var useHttp bool
    55    headers := make(httpHeaders)
    56    var parsedUrl *url.URL
    57    var trans thrift.TTransport
    58    _ = strconv.Atoi
    59    _ = math.Abs
    60    flag.Usage = Usage
    61    flag.StringVar(&host, "h", "localhost", "Specify host and port")
    62    flag.IntVar(&port, "p", 9090, "Specify port")
    63    flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
    64    flag.StringVar(&urlString, "u", "", "Specify the url")
    65    flag.BoolVar(&framed, "framed", false, "Use framed transport")
    66    flag.BoolVar(&useHttp, "http", false, "Use http")
    67    flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")")
    68    flag.Parse()
    69    
    70    if len(urlString) > 0 {
    71      var err error
    72      parsedUrl, err = url.Parse(urlString)
    73      if err != nil {
    74        fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
    75        flag.Usage()
    76      }
    77      host = parsedUrl.Host
    78      useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https"
    79    } else if useHttp {
    80      _, err := url.Parse(fmt.Sprint("http://", host, ":", port))
    81      if err != nil {
    82        fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
    83        flag.Usage()
    84      }
    85    }
    86    
    87    cmd := flag.Arg(0)
    88    var err error
    89    var cfg *thrift.TConfiguration = nil
    90    if useHttp {
    91      trans, err = thrift.NewTHttpClient(parsedUrl.String())
    92      if len(headers) > 0 {
    93        httptrans := trans.(*thrift.THttpClient)
    94        for key, value := range headers {
    95          httptrans.SetHeader(key, value)
    96        }
    97      }
    98    } else {
    99      portStr := fmt.Sprint(port)
   100      if strings.Contains(host, ":") {
   101             host, portStr, err = net.SplitHostPort(host)
   102             if err != nil {
   103                     fmt.Fprintln(os.Stderr, "error with host:", err)
   104                     os.Exit(1)
   105             }
   106      }
   107      trans = thrift.NewTSocketConf(net.JoinHostPort(host, portStr), cfg)
   108      if err != nil {
   109        fmt.Fprintln(os.Stderr, "error resolving address:", err)
   110        os.Exit(1)
   111      }
   112      if framed {
   113        trans = thrift.NewTFramedTransportConf(trans, cfg)
   114      }
   115    }
   116    if err != nil {
   117      fmt.Fprintln(os.Stderr, "Error creating transport", err)
   118      os.Exit(1)
   119    }
   120    defer trans.Close()
   121    var protocolFactory thrift.TProtocolFactory
   122    switch protocol {
   123    case "compact":
   124      protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
   125      break
   126    case "simplejson":
   127      protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
   128      break
   129    case "json":
   130      protocolFactory = thrift.NewTJSONProtocolFactory()
   131      break
   132    case "binary", "":
   133      protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg)
   134      break
   135    default:
   136      fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
   137      Usage()
   138      os.Exit(1)
   139    }
   140    iprot := protocolFactory.GetProtocol(trans)
   141    oprot := protocolFactory.GetProtocol(trans)
   142    client := interfaces.NewApplyRequestClient(thrift.NewTStandardClient(iprot, oprot))
   143    if err := trans.Open(); err != nil {
   144      fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
   145      os.Exit(1)
   146    }
   147    
   148    switch cmd {
   149    case "apply_request":
   150      if flag.NArg() - 1 != 4 {
   151        fmt.Fprintln(os.Stderr, "ApplyRequest requires 4 args")
   152        flag.Usage()
   153      }
   154      arg165 := flag.Arg(1)
   155      mbTrans166 := thrift.NewTMemoryBufferLen(len(arg165))
   156      defer mbTrans166.Close()
   157      _, err167 := mbTrans166.WriteString(arg165)
   158      if err167 != nil {
   159        Usage()
   160        return
   161      }
   162      factory168 := thrift.NewTJSONProtocolFactory()
   163      jsProt169 := factory168.GetProtocol(mbTrans166)
   164      argvalue0 := interfaces.NewUint64()
   165      err170 := argvalue0.Read(context.Background(), jsProt169)
   166      if err170 != nil {
   167        Usage()
   168        return
   169      }
   170      value0 := argvalue0
   171      arg171 := flag.Arg(2)
   172      mbTrans172 := thrift.NewTMemoryBufferLen(len(arg171))
   173      defer mbTrans172.Close()
   174      _, err173 := mbTrans172.WriteString(arg171)
   175      if err173 != nil {
   176        Usage()
   177        return
   178      }
   179      factory174 := thrift.NewTJSONProtocolFactory()
   180      jsProt175 := factory174.GetProtocol(mbTrans172)
   181      argvalue1 := interfaces.NewUint64()
   182      err176 := argvalue1.Read(context.Background(), jsProt175)
   183      if err176 != nil {
   184        Usage()
   185        return
   186      }
   187      value1 := argvalue1
   188      arg177 := flag.Arg(3)
   189      mbTrans178 := thrift.NewTMemoryBufferLen(len(arg177))
   190      defer mbTrans178.Close()
   191      _, err179 := mbTrans178.WriteString(arg177)
   192      if err179 != nil {
   193        Usage()
   194        return
   195      }
   196      factory180 := thrift.NewTJSONProtocolFactory()
   197      jsProt181 := factory180.GetProtocol(mbTrans178)
   198      argvalue2 := interfaces.NewUint64()
   199      err182 := argvalue2.Read(context.Background(), jsProt181)
   200      if err182 != nil {
   201        Usage()
   202        return
   203      }
   204      value2 := argvalue2
   205      tmp3, err183 := (strconv.Atoi(flag.Arg(4)))
   206      if err183 != nil {
   207        Usage()
   208        return
   209      }
   210      argvalue3 := int32(tmp3)
   211      value3 := argvalue3
   212      fmt.Print(client.ApplyRequest(context.Background(), value0, value1, value2, value3))
   213      fmt.Print("\n")
   214      break
   215    case "apply_end":
   216      if flag.NArg() - 1 != 1 {
   217        fmt.Fprintln(os.Stderr, "ApplyEnd requires 1 args")
   218        flag.Usage()
   219      }
   220      tmp0, err184 := (strconv.Atoi(flag.Arg(1)))
   221      if err184 != nil {
   222        Usage()
   223        return
   224      }
   225      argvalue0 := int32(tmp0)
   226      value0 := argvalue0
   227      fmt.Print(client.ApplyEnd(context.Background(), value0))
   228      fmt.Print("\n")
   229      break
   230    case "":
   231      Usage()
   232      break
   233    default:
   234      fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
   235    }
   236  }