Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update udp_server to check for bad key in json #3

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
udp_server
udp_client
udp_server_monitor
udp_server.json
*.log
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cat > udp_server.json << EOF
"SendTimeout": 10,
"RecvTimeout": 10,
"HeartBeatGracePeriod": 1.0,
"stompURI": "zzz:port"
"stompURI": "zzz:port",
"endpoint": "/abc/xyz",
"contentType": "application/json",
"verbose": false
Expand Down
1 change: 1 addition & 0 deletions udp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func record(seed, user, host string) map[string]interface{} {
data["server_host"] = "cmshdp-d019"
data["read_vector_operations"] = 17
data["read_single_bytes"] = 15401826
data["type"] = "should be read type"
data["app_info"] = "something"
data["client_domain"] = host
data["start_time"] = 1395960729
Expand Down
35 changes: 32 additions & 3 deletions udp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func sendDataToStomp(data []byte) {

// udp server implementation
func udpServer() {
const maxFailedPacketLength = 1000 // maximum length of the failed packet to be printed
udpAddr := &net.UDPAddr{Port: Config.Port}
// if configuration provides explicitly IPAddr to bind use it here
if Config.IPAddr != "" {
Expand Down Expand Up @@ -235,8 +236,13 @@ func udpServer() {
log.Printf("unable to unmarshal UDP packet into JSON, error %v\n", err)
e := string(err.Error())
if strings.Contains(e, "invalid character") {
// nothing we can do about mailformed JSON, let's dump it
log.Println(string(data))
// truncate the malformed JSON if it exceeds the maximum length
// and dump it
failedData := string(data)
if len(failedData) > maxFailedPacketLength {
failedData = failedData[:maxFailedPacketLength] + "..."
}
log.Println(failedData)
} else if strings.Contains(e, "unexpected end of JSON input") {
// let's increse buf size to adjust to the packet size
bufSize = bufSize * 2
Expand All @@ -257,9 +263,32 @@ func udpServer() {
log.Printf("received: %s from %s\n", sdata, remote)
}

// check if the message has a key named "type" and rename it to "read_type"
if val, ok := packet["type"]; ok {
packet["read_type"] = val
delete(packet, "type")
}

// send data to Stomp endpoint
if Config.Endpoint != "" && stompConn != nil {
sendDataToStomp(data)
newData, err := json.Marshal(packet)
if err != nil {
log.Printf("unable to marshal UDP packet into JSON, error %v\n", err)
nikodemas marked this conversation as resolved.
Show resolved Hide resolved
// truncate the failed packet if it exceeds the maximum length
failedPacket := fmt.Sprint(packet)
if len(failedPacket) > maxFailedPacketLength {
failedPacket = failedPacket[:maxFailedPacketLength] + "..."
}
log.Println(failedPacket) // dump the truncated message to the log
// clear-up our buffer
buffer = buffer[:0]
continue
}
if Config.Verbose {
sNewData := strings.TrimSpace(string(newData))
log.Printf("sent to AMQ: %s\n", sNewData)
}
sendDataToStomp(newData)
}

// clear-up our buffer
Expand Down