github.com/ergo-services/ergo@v1.999.224/gen/pool_worker.go (about)

     1  package gen
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ergo-services/ergo/etf"
     7  	"github.com/ergo-services/ergo/lib"
     8  )
     9  
    10  type workerCastMessage struct {
    11  	message etf.Term
    12  }
    13  type workerCallMessage struct {
    14  	from    ServerFrom
    15  	message etf.Term
    16  }
    17  
    18  type PoolWorkerBehavior interface {
    19  	ServerBehavior
    20  	InitPoolWorker(process *PoolWorkerProcess, args ...etf.Term) error
    21  	HandleWorkerInfo(process *PoolWorkerProcess, message etf.Term)
    22  	HandleWorkerCast(process *PoolWorkerProcess, message etf.Term)
    23  	HandleWorkerCall(process *PoolWorkerProcess, message etf.Term) etf.Term
    24  }
    25  
    26  type PoolWorkerProcess struct {
    27  	ServerProcess
    28  }
    29  
    30  type PoolWorker struct {
    31  	Server
    32  }
    33  
    34  func (pw *PoolWorker) Init(process *ServerProcess, args ...etf.Term) error {
    35  	behavior, ok := process.Behavior().(PoolWorkerBehavior)
    36  
    37  	if !ok {
    38  		return fmt.Errorf("Pool: not a PoolWorkerBehavior")
    39  	}
    40  
    41  	worker := &PoolWorkerProcess{
    42  		ServerProcess: *process,
    43  	}
    44  
    45  	worker.State = nil
    46  
    47  	if err := behavior.InitPoolWorker(worker, args...); err != nil {
    48  		return err
    49  	}
    50  
    51  	process.State = worker
    52  	return nil
    53  }
    54  
    55  func (pw *PoolWorker) HandleInfo(process *ServerProcess, message etf.Term) ServerStatus {
    56  	worker := process.State.(*PoolWorkerProcess)
    57  	behavior := worker.Behavior().(PoolWorkerBehavior)
    58  	switch m := message.(type) {
    59  	case workerCallMessage:
    60  		result := behavior.HandleWorkerCall(worker, m.message)
    61  		process.SendReply(m.from, result)
    62  	case workerCastMessage:
    63  		behavior.HandleWorkerCast(worker, m.message)
    64  	default:
    65  		behavior.HandleWorkerInfo(worker, message)
    66  	}
    67  	return ServerStatusOK
    68  }
    69  
    70  // HandleWorkerInfo
    71  func (pw *PoolWorker) HandleWorkerInfo(process *PoolWorkerProcess, message etf.Term) {
    72  	lib.Warning("HandleWorkerInfo: unhandled message %#v", message)
    73  }
    74  
    75  // HandleWorkerCast
    76  func (pw *PoolWorker) HandleWorkerCast(process *PoolWorkerProcess, message etf.Term) {
    77  	lib.Warning("HandleWorkerCast: unhandled message %#v", message)
    78  }
    79  
    80  // HandleWorkerCall
    81  func (pw *PoolWorker) HandleWorkerCall(process *PoolWorkerProcess, message etf.Term) etf.Term {
    82  	lib.Warning("HandleWorkerCall: unhandled message %#v", message)
    83  	return nil
    84  }