github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/object/service-save.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package object
     4  
     5  import (
     6  	"../protocol"
     7  	"../srpc"
     8  	"../syllab"
     9  )
    10  
    11  func (ser *saveService) ServeSRPC(st protocol.Stream, srpcReq protocol.SRPCRequest) (res protocol.Syllab, err protocol.Error) {
    12  	var srpcRequestPayload = srpcReq.Payload()
    13  	var reqAsSyllab = saveRequestSyllab(srpcRequestPayload)
    14  	err = reqAsSyllab.CheckSyllab(srpcRequestPayload)
    15  	if err != nil {
    16  		return
    17  	}
    18  
    19  	if reqAsSyllab.RequestType() == RequestTypeBroadcast {
    20  		// tell other node that this node handle request and don't send this request to other nodes!
    21  		reqAsSyllab.SetRequestType(RequestTypeStandalone)
    22  
    23  		var replicatedLocalNodes = protocol.App.ReplicatedLocalNode()
    24  		// send request to other related nodes
    25  		for i := 0; i < len(replicatedLocalNodes); i++ {
    26  			var conn = replicatedLocalNodes[i].Conn()
    27  
    28  			// Make new request-response streams
    29  			var stream protocol.Stream
    30  			stream, err = conn.OutcomeStream(ser)
    31  			if err != nil {
    32  				// TODO::: Can we easily return error if some nodes did their job and not have enough resource to send request to other nodes??
    33  				return
    34  			}
    35  			stream.SetOutcomeData(srpcReq)
    36  
    37  			err = conn.Send(stream)
    38  			if err != nil {
    39  				return
    40  			}
    41  
    42  			var srpcRes = srpc.Response(stream.IncomeData().Marshal())
    43  			err = srpcRes.Error()
    44  			if err != nil {
    45  				// TODO::: Can we easily return error if some nodes do their job and just one node connection lost??
    46  				return
    47  			}
    48  		}
    49  	}
    50  
    51  	// Do for local node
    52  	err = protocol.OS.ObjectDirectory().SaveRaw(reqAsSyllab.Object())
    53  	return
    54  }
    55  
    56  func save(req SaveRequest) (err protocol.Error) {
    57  	if req.RequestType() == RequestTypeBroadcast {
    58  		// tell other node that this node handle request and don't send this request to other nodes!
    59  		req.SetRequestType(RequestTypeStandalone)
    60  		var reqAsSyllab = syllab.NewCodec(&req)
    61  
    62  		var replicatedLocalNodes = protocol.App.ReplicatedLocalNode()
    63  		// send request to other related nodes
    64  		for i := 0; i < len(replicatedLocalNodes); i++ {
    65  			var conn = replicatedLocalNodes[i].Conn()
    66  
    67  			// Make new request-response streams
    68  			var stream protocol.Stream
    69  			stream, err = conn.OutcomeStream(&DeleteService)
    70  			if err != nil {
    71  				// TODO::: Can we easily return error if some nodes did their job and not have enough resource to send request to other nodes??
    72  				return
    73  			}
    74  			stream.SetOutcomeData(&reqAsSyllab)
    75  
    76  			err = conn.Send(stream)
    77  			if err != nil {
    78  				return
    79  			}
    80  
    81  			var srpcRes = srpc.Response(stream.IncomeData().Marshal())
    82  			err = srpcRes.Error()
    83  			if err != nil {
    84  				// TODO::: Can we easily return error if some nodes do their job and just one node connection lost??
    85  				return
    86  			}
    87  		}
    88  	}
    89  
    90  	// Do for local node
    91  	err = protocol.OS.ObjectDirectory().SaveRaw(req.object)
    92  	return
    93  }