nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/examples/raw/main.go (about)

     1  // Copyright 2015 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // raw implements an example concurrent request/reply server, using
    16  // the raw server socket.  (The clients are run using multiple cooked
    17  // sockets.)
    18  //
    19  // To use:
    20  //
    21  //   $ go build .
    22  //   $ url=tcp://127.0.0.1:40899
    23  //   $ nservers=20
    24  //   $ nclients=10
    25  //   $ ./raw server $url $nservers & pid=$! && sleep 1
    26  //   $ ./raw client $url $nclients
    27  //   $ kill $pid
    28  //
    29  package main
    30  
    31  import (
    32  	"fmt"
    33  	"os"
    34  	"strconv"
    35  )
    36  
    37  func die(format string, v ...interface{}) {
    38  	fmt.Fprintln(os.Stderr, fmt.Sprintf(format, v...))
    39  	os.Exit(1)
    40  }
    41  
    42  func main() {
    43  	if len(os.Args) > 2 && os.Args[1] == "server" {
    44  		nworkers := 16
    45  		if len(os.Args) > 3 {
    46  			var err error
    47  			nworkers, err = strconv.Atoi(os.Args[3])
    48  			if err != nil || nworkers < 1 {
    49  				die("bad worker count")
    50  			}
    51  		}
    52  		server(os.Args[2], nworkers)
    53  		os.Exit(0)
    54  	}
    55  	if len(os.Args) > 2 && os.Args[1] == "client" {
    56  		nworkers := 1
    57  		if len(os.Args) > 3 {
    58  			var err error
    59  			nworkers, err = strconv.Atoi(os.Args[3])
    60  			if err != nil || nworkers < 1 {
    61  				die("bad worker count")
    62  			}
    63  		}
    64  		client(os.Args[2], nworkers)
    65  		os.Exit(0)
    66  	}
    67  	fmt.Fprintf(os.Stderr, "Usage: %s server|client <URL> [<workers>]\n", os.Args[0])
    68  	os.Exit(1)
    69  }