Changed the assets to 2 bytes; implemented a Expire Datetime with Unix; Changed README.md
This commit is contained in:
parent
75d8f343ea
commit
0c09cc0db0
@ -8,13 +8,15 @@ An Package to interact with an mock stock exchange
|
|||||||
| ----- | ------ |
|
| ----- | ------ |
|
||||||
| Key | 8 Bytes |
|
| Key | 8 Bytes |
|
||||||
| Operation | Byte |
|
| Operation | Byte |
|
||||||
| Asset | Byte |
|
| Asset | 2 Bytes |
|
||||||
| Price | Float64 |
|
| Price | Float64 |
|
||||||
| Volume | Float64 |
|
| Volume | Float64 |
|
||||||
|
| Expire (Unix) | uint64 |
|
||||||
| CRC | 2 Bytes |
|
| CRC | 2 Bytes |
|
||||||
|
|
||||||
## TODOS
|
## TODOS
|
||||||
- [ ] Test it
|
- [ ] Test it
|
||||||
- [ ] Make it configureable
|
- [ ] Make it configureable
|
||||||
- [ ] Implement expire date
|
- [x] Implement expire date
|
||||||
|
- [ ] Change Key to uuid
|
||||||
- [ ] Document it
|
- [ ] Document it
|
19
spf.go
19
spf.go
@ -12,20 +12,21 @@ import (
|
|||||||
type SimpleFinancePackage struct {
|
type SimpleFinancePackage struct {
|
||||||
Key [8]byte // 8 bytes for the key
|
Key [8]byte // 8 bytes for the key
|
||||||
Operation byte // 1 byte for operation
|
Operation byte // 1 byte for operation
|
||||||
Asset byte // 1 byte for asset
|
Asset uint16 // 2 bytes for asset
|
||||||
Price float64 // 8 bytes for price (float64 for precision)
|
Price float64 // 8 bytes for price (float64 for precision)
|
||||||
Volume float64 // 8 bytes for volume
|
Volume float64 // 8 bytes for volume
|
||||||
|
Expire uint64 // 8 bytes for expiration date
|
||||||
CRC uint16 // 2 bytes for CRC
|
CRC uint16 // 2 bytes for CRC
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
|
func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
|
||||||
if len(data) != 28 {
|
if len(data) != 37 {
|
||||||
return nil, errors.New("invalid packet size, expected 28 bytes")
|
return nil, errors.New("invalid packet size, expected 37 bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check CRC
|
//Check CRC
|
||||||
check := binary.BigEndian.Uint16(data[26:])
|
check := binary.BigEndian.Uint16(data[35:])
|
||||||
if check != crc.Calculate(data[:26]) {
|
if check != crc.Calculate(data[:35]) {
|
||||||
return nil, errors.New("CRC check failed")
|
return nil, errors.New("CRC check failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,9 +42,12 @@ func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
|
|||||||
if packet.Price < 0 || packet.Volume < 0 {
|
if packet.Price < 0 || packet.Volume < 0 {
|
||||||
return nil, errors.New("price and volume must be non-negative")
|
return nil, errors.New("price and volume must be non-negative")
|
||||||
}
|
}
|
||||||
if packet.Operation > 127 { // Example: Valid operation values are 0-127
|
if packet.Operation > 127 { // Valid operation values are 0-127
|
||||||
return nil, errors.New("invalid operation value")
|
return nil, errors.New("invalid operation value")
|
||||||
}
|
}
|
||||||
|
if packet.Expire < 946684800 { // Expire date must be after 2000-01-01
|
||||||
|
return nil, errors.New("invalid expiration date")
|
||||||
|
}
|
||||||
|
|
||||||
return &packet, nil
|
return &packet, nil
|
||||||
}
|
}
|
||||||
@ -67,6 +71,9 @@ func EncodeSimpleFinanacePackage(packet SimpleFinancePackage) ([]byte, error) {
|
|||||||
if err := binary.Write(buf, binary.BigEndian, packet.Volume); err != nil {
|
if err := binary.Write(buf, binary.BigEndian, packet.Volume); err != nil {
|
||||||
return nil, fmt.Errorf("failed to encode volume: %w", err)
|
return nil, fmt.Errorf("failed to encode volume: %w", err)
|
||||||
}
|
}
|
||||||
|
if err := binary.Write(buf, binary.BigEndian, packet.Expire); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to encode expiration date: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate and write CRC
|
// Calculate and write CRC
|
||||||
packet.CRC = crc.Calculate(buf.Bytes())
|
packet.CRC = crc.Calculate(buf.Bytes())
|
||||||
|
@ -15,6 +15,7 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
|
|||||||
Asset: 2,
|
Asset: 2,
|
||||||
Price: 1234.56,
|
Price: 1234.56,
|
||||||
Volume: 7890.12,
|
Volume: 7890.12,
|
||||||
|
Expire: 946684801,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode the packet
|
// Encode the packet
|
||||||
@ -24,8 +25,8 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the encoded length
|
// Check the encoded length
|
||||||
if len(encoded) != 28 {
|
if len(encoded) != 37 {
|
||||||
t.Fatalf("Encoded data has incorrect length: got %d, want 28", len(encoded))
|
t.Fatalf("Encoded data has incorrect length: got %d, want 37", len(encoded))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the packet
|
// Decode the packet
|
||||||
@ -76,6 +77,7 @@ func TestInvalidPacketValues(t *testing.T) {
|
|||||||
Asset: 2,
|
Asset: 2,
|
||||||
Price: 1234.56,
|
Price: 1234.56,
|
||||||
Volume: 7890.12,
|
Volume: 7890.12,
|
||||||
|
Expire: 946684801,
|
||||||
}
|
}
|
||||||
encoded, _ := EncodeSimpleFinanacePackage(validPacket)
|
encoded, _ := EncodeSimpleFinanacePackage(validPacket)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user