github.com/LQR471814/websocket-ftp/server@v0.4.0/types.go (about)

     1  package server
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"strconv"
     7  
     8  	"github.com/gorilla/websocket"
     9  )
    10  
    11  type Signal struct {
    12  	Type string
    13  }
    14  
    15  type FileRequests struct {
    16  	Type  string
    17  	Files []File
    18  }
    19  
    20  type File struct {
    21  	Name string
    22  	Size int64
    23  	Type string
    24  }
    25  
    26  func (f File) ID() string {
    27  	return f.Name + strconv.FormatInt(f.Size, 10) + f.Type
    28  }
    29  
    30  type TransferMetadata struct {
    31  	From  net.IP
    32  	Files []File
    33  }
    34  
    35  type TransferState struct {
    36  	Number      StateID
    37  	CurrentFile int
    38  	Received    int64
    39  }
    40  
    41  type Transfer struct {
    42  	Data   TransferMetadata
    43  	State  TransferState
    44  	ID     uint64
    45  	Output map[string]io.Writer
    46  
    47  	dataChan chan []byte
    48  	conn     *websocket.Conn
    49  }
    50  
    51  //? Note: WriteChunk / recvfilecontents is not present here
    52  //?  because it would make things unnecessarily complex and inefficient
    53  
    54  //?  The logic is still implemented, it's just not through the handlers
    55  
    56  type StateID int
    57  
    58  const (
    59  	INITIAL StateID = iota
    60  	LISTENING_FOR_FILE_REQUESTS
    61  	WAITING_FOR_USER_CONFIRMATION
    62  	RECEIVING
    63  )
    64  
    65  type Event int
    66  
    67  const (
    68  	peerConnect Event = iota
    69  	recvRequests
    70  	userAccept
    71  	userDeny
    72  	recvDone
    73  )
    74  
    75  type Action int
    76  
    77  const (
    78  	DisplayFileRequests Action = iota
    79  	IncrementFileIndex
    80  	SendClientAllow
    81  
    82  	SendStartSignal
    83  	SendExitSignal
    84  	SendFinishedSignal
    85  
    86  	StartFileWriter
    87  	StopFileWriter
    88  
    89  	RecvDoneHandler
    90  )
    91  
    92  type StateMatrix map[Event]map[StateID]struct {
    93  	Actions  []Action
    94  	NewState StateID
    95  }
    96  
    97  var EventStateMatrix = StateMatrix{
    98  	peerConnect: {
    99  		INITIAL: {
   100  			Actions:  []Action{},
   101  			NewState: LISTENING_FOR_FILE_REQUESTS,
   102  		},
   103  	},
   104  	recvRequests: {
   105  		LISTENING_FOR_FILE_REQUESTS: {
   106  			Actions:  []Action{DisplayFileRequests},
   107  			NewState: WAITING_FOR_USER_CONFIRMATION,
   108  		},
   109  	},
   110  	userAccept: {
   111  		WAITING_FOR_USER_CONFIRMATION: {
   112  			Actions:  []Action{StartFileWriter, SendStartSignal},
   113  			NewState: RECEIVING,
   114  		},
   115  	},
   116  	userDeny: {
   117  		WAITING_FOR_USER_CONFIRMATION: {
   118  			Actions:  []Action{SendExitSignal},
   119  			NewState: INITIAL,
   120  		},
   121  	},
   122  	recvDone: {
   123  		RECEIVING: {
   124  			Actions: []Action{
   125  				StopFileWriter,
   126  				IncrementFileIndex,
   127  				SendFinishedSignal,
   128  				RecvDoneHandler,
   129  			},
   130  			NewState: RECEIVING,
   131  		},
   132  	},
   133  }