forked from Elara6331/itd
		
	Enable RPCX gateway
This commit is contained in:
		
							
								
								
									
										50
									
								
								socket.go
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								socket.go
									
									
									
									
									
								
							| @@ -46,6 +46,7 @@ var ( | ||||
| 	ErrDFUInvalidFile    = errors.New("provided file is invalid for given upgrade type") | ||||
| 	ErrDFUNotEnoughFiles = errors.New("not enough files provided for given upgrade type") | ||||
| 	ErrDFUInvalidUpgType = errors.New("invalid upgrade type") | ||||
| 	ErrRPCXUsingGateway  = errors.New("bidirectional requests are unsupported over gateway") | ||||
| ) | ||||
|  | ||||
| type DoneMap map[string]chan struct{} | ||||
| @@ -116,7 +117,7 @@ func startSocket(dev *infinitime.Device) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	go srv.ServeListener("unix", ln) | ||||
| 	go srv.ServeListener("tcp", ln) | ||||
|  | ||||
| 	// Log socket start | ||||
| 	log.Info().Str("path", k.String("socket.path")).Msg("Started control socket") | ||||
| @@ -136,7 +137,12 @@ func (i *ITD) HeartRate(_ context.Context, _ none, out *uint8) error { | ||||
| } | ||||
|  | ||||
| func (i *ITD) WatchHeartRate(ctx context.Context, _ none, out *string) error { | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if !ok { | ||||
| 		return ErrRPCXUsingGateway | ||||
| 	} | ||||
|  | ||||
| 	heartRateCh, cancel, err := i.dev.WatchHeartRate() | ||||
| 	if err != nil { | ||||
| @@ -178,7 +184,12 @@ func (i *ITD) BatteryLevel(_ context.Context, _ none, out *uint8) error { | ||||
| } | ||||
|  | ||||
| func (i *ITD) WatchBatteryLevel(ctx context.Context, _ none, out *string) error { | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if !ok { | ||||
| 		return ErrRPCXUsingGateway | ||||
| 	} | ||||
|  | ||||
| 	battLevelCh, cancel, err := i.dev.WatchBatteryLevel() | ||||
| 	if err != nil { | ||||
| @@ -220,7 +231,12 @@ func (i *ITD) Motion(_ context.Context, _ none, out *infinitime.MotionValues) er | ||||
| } | ||||
|  | ||||
| func (i *ITD) WatchMotion(ctx context.Context, _ none, out *string) error { | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if !ok { | ||||
| 		return ErrRPCXUsingGateway | ||||
| 	} | ||||
|  | ||||
| 	motionValsCh, cancel, err := i.dev.WatchMotion() | ||||
| 	if err != nil { | ||||
| @@ -262,7 +278,12 @@ func (i *ITD) StepCount(_ context.Context, _ none, out *uint32) error { | ||||
| } | ||||
|  | ||||
| func (i *ITD) WatchStepCount(ctx context.Context, _ none, out *string) error { | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if !ok { | ||||
| 		return ErrRPCXUsingGateway | ||||
| 	} | ||||
|  | ||||
| 	stepCountCh, cancel, err := i.dev.WatchStepCount() | ||||
| 	if err != nil { | ||||
| @@ -365,8 +386,10 @@ func (i *ITD) FirmwareUpgrade(ctx context.Context, reqData api.FwUpgradeData, ou | ||||
| 	id := uuid.New().String() | ||||
| 	*out = id | ||||
|  | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
|  | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if ok { | ||||
| 		go func() { | ||||
| 			// For every progress event | ||||
| 			for event := range i.dev.DFU.Progress() { | ||||
| @@ -382,6 +405,7 @@ func (i *ITD) FirmwareUpgrade(ctx context.Context, reqData api.FwUpgradeData, ou | ||||
| 			firmwareUpdating = false | ||||
| 			i.srv.SendMessage(clientConn, id, "Done", nil, nil) | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	// Set firmwareUpdating | ||||
| 	firmwareUpdating = true | ||||
| @@ -463,7 +487,6 @@ func (fs *FS) ReadDir(_ context.Context, dir string, out *[]api.FileInfo) error | ||||
|  | ||||
| func (fs *FS) Upload(ctx context.Context, paths [2]string, out *string) error { | ||||
| 	fs.updateFS() | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
|  | ||||
| 	localFile, err := os.Open(paths[1]) | ||||
| 	if err != nil { | ||||
| @@ -483,6 +506,10 @@ func (fs *FS) Upload(ctx context.Context, paths [2]string, out *string) error { | ||||
| 	id := uuid.New().String() | ||||
| 	*out = id | ||||
|  | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if ok { | ||||
| 		go func() { | ||||
| 			// For every progress event | ||||
| 			for sent := range remoteFile.Progress() { | ||||
| @@ -500,6 +527,7 @@ func (fs *FS) Upload(ctx context.Context, paths [2]string, out *string) error { | ||||
|  | ||||
| 			fs.srv.SendMessage(clientConn, id, "Done", nil, nil) | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	go func() { | ||||
| 		io.Copy(remoteFile, localFile) | ||||
| @@ -512,7 +540,6 @@ func (fs *FS) Upload(ctx context.Context, paths [2]string, out *string) error { | ||||
|  | ||||
| func (fs *FS) Download(ctx context.Context, paths [2]string, out *string) error { | ||||
| 	fs.updateFS() | ||||
| 	clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	 | ||||
| 	localFile, err := os.Create(paths[0]) | ||||
| 	if err != nil { | ||||
| @@ -527,6 +554,10 @@ func (fs *FS) Download(ctx context.Context, paths [2]string, out *string) error | ||||
| 	id := uuid.New().String() | ||||
| 	*out = id | ||||
| 	 | ||||
| 	// Get client's connection | ||||
| 	clientConn, ok := ctx.Value(server.RemoteConnContextKey).(net.Conn) | ||||
| 	// If user is using gateway, the client connection will not be available | ||||
| 	if ok { | ||||
| 		go func() { | ||||
| 			// For every progress event | ||||
| 			for rcvd := range remoteFile.Progress() { | ||||
| @@ -546,6 +577,7 @@ func (fs *FS) Download(ctx context.Context, paths [2]string, out *string) error | ||||
| 			localFile.Close() | ||||
| 			remoteFile.Close() | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	go io.Copy(localFile, remoteFile) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user