package session import ( "net" "time" "ripple/errmsgs" "ripple/handler" "ripple/state" "ripple/transport" "ripple/types" ) type CounterpartSession struct { st *state.State transport *transport.CounterpartTransport cmdHandlers [128]func([]byte) } func NewCounterpartSession( st *state.State, t *transport.CounterpartTransport, h *handler.CounterpartHandlers, ) *CounterpartSession { s := &CounterpartSession{st: st, transport: t} s.cmdHandlers[0x00] = h.FindPath s.cmdHandlers[0x01] = h.CommitPayment s.cmdHandlers[0x02] = h.SealPayment return s } func (s *CounterpartSession) Receive(tx []byte, addr *net.UDPAddr) (types.Instruction, error) { paymentID := s.st.Memory.GetPaymentID() _, ok := s.st.Memory.GetPathfinding(paymentID) if !ok { payment, ok := s.st.Storage.Payments[paymentID] if !ok || payment.Timeout < time.Now().Unix() { return types.Instruction{}, errmsgs.ErrKeyNotFound } } return s.transport.Receive(tx, addr) } func (d *CounterpartSession) Dispatch(instr types.Instruction) { if h := d.cmdHandlers[instr.Command]; h != nil { h(instr.Arguments) } }