github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/examples/basic_publisher/basic_publisher.go (about)

     1  /*
     2  Copyright 2016 Stanislav Liberman
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"log"
    23  	"os"
    24  	"os/signal"
    25  	"syscall"
    26  	"time"
    27  
    28  	"github.com/lirm/aeron-go/aeron"
    29  	"github.com/lirm/aeron-go/aeron/atomic"
    30  	"github.com/lirm/aeron-go/aeron/logging"
    31  	"github.com/lirm/aeron-go/examples"
    32  )
    33  
    34  var logger = logging.MustGetLogger("basic_publisher")
    35  
    36  var interrupt = make(chan os.Signal, 1)
    37  
    38  func init() {
    39  	signal.Notify(interrupt, os.Interrupt)
    40  	signal.Notify(interrupt, syscall.SIGTERM)
    41  }
    42  
    43  func main() {
    44  	flag.Parse()
    45  
    46  	if !*examples.ExamplesConfig.LoggingOn {
    47  		logging.SetLevel(logging.INFO, "aeron")
    48  		logging.SetLevel(logging.INFO, "memmap")
    49  		logging.SetLevel(logging.INFO, "driver")
    50  		logging.SetLevel(logging.INFO, "counters")
    51  		logging.SetLevel(logging.INFO, "logbuffers")
    52  		logging.SetLevel(logging.INFO, "buffer")
    53  		logging.SetLevel(logging.INFO, "rb")
    54  	}
    55  
    56  	errorHandler := func(err error) {
    57  		logger.Warning(err)
    58  	}
    59  	to := time.Duration(time.Millisecond.Nanoseconds() * *examples.ExamplesConfig.DriverTo)
    60  	ctx := aeron.NewContext().AeronDir(*examples.ExamplesConfig.AeronPrefix).MediaDriverTimeout(to).ErrorHandler(errorHandler)
    61  
    62  	a, err := aeron.Connect(ctx)
    63  	if err != nil {
    64  		logger.Fatalf("Failed to connect to media driver: %s\n", err.Error())
    65  	}
    66  	defer a.Close()
    67  
    68  	publication, err := a.AddPublication(*examples.ExamplesConfig.Channel, int32(*examples.ExamplesConfig.StreamID))
    69  	if err != nil {
    70  		logger.Fatalf(err.Error())
    71  	}
    72  	defer publication.Close()
    73  	log.Printf("Publication found %v", publication)
    74  
    75  	for counter := 0; counter < *examples.ExamplesConfig.Messages; counter++ {
    76  		message := fmt.Sprintf("this is a message %d", counter)
    77  		srcBuffer := atomic.MakeBuffer(([]byte)(message))
    78  		ret := publication.Offer(srcBuffer, 0, int32(len(message)), nil)
    79  		switch ret {
    80  		case aeron.NotConnected:
    81  			log.Printf("%d: not connected yet", counter)
    82  		case aeron.BackPressured:
    83  			log.Printf("%d: back pressured", counter)
    84  		default:
    85  			if ret < 0 {
    86  				log.Printf("%d: Unrecognized code: %d", counter, ret)
    87  			} else {
    88  				log.Printf("%d: success!", counter)
    89  			}
    90  		}
    91  
    92  		if !publication.IsConnected() {
    93  			log.Printf("no subscribers detected")
    94  		}
    95  		select {
    96  		case <-interrupt:
    97  			return
    98  		default:
    99  			time.Sleep(time.Second)
   100  		}
   101  	}
   102  }