219 lines
6.9 KiB
Julia
219 lines
6.9 KiB
Julia
using NATSBridge
|
|
using UUIDs
|
|
using JSON
|
|
using DataFrames
|
|
using Random
|
|
|
|
# Include the NATSBridge module
|
|
include("src/NATSBridge.jl")
|
|
|
|
# Constants
|
|
const NATS_URL = "nats://localhost:4222"
|
|
const FILESERVER_URL = "http://localhost:8080"
|
|
|
|
# Chat message types for scenario 6
|
|
const CHAT_TYPES = ["text", "dictionary", "table", "image", "audio", "video", "binary"]
|
|
|
|
# Helper function to create sample text data
|
|
function create_text_payload()
|
|
texts = [
|
|
"Hello!",
|
|
"How are you doing today?",
|
|
"This is a test message.",
|
|
"Chat with mixed content is fun!",
|
|
"Short text payload."
|
|
]
|
|
return (rand(texts), "text")
|
|
end
|
|
|
|
# Helper function to create sample dictionary data
|
|
function create_dictionary_payload()
|
|
dictionaries = [
|
|
Dict("greeting" => "Hello", "status" => "active", "count" => 42),
|
|
Dict("user" => "alice", "message_id" => string(uuid4()), "timestamp" => Dates.now().iso8601),
|
|
Dict("config" => Dict("theme" => "dark", "notifications" => true))
|
|
]
|
|
return (rand(dictionaries), "dictionary")
|
|
end
|
|
|
|
# Helper function to create sample table data (DataFrame)
|
|
function create_table_payload()
|
|
# Small DataFrame
|
|
df_small = DataFrame(
|
|
id = 1:5,
|
|
name = ["Alice", "Bob", "Charlie", "Diana", "Eve"],
|
|
score = [95, 88, 92, 78, 85],
|
|
status = ["active", "active", "inactive", "active", "pending"]
|
|
)
|
|
|
|
# Large DataFrame (> 1MB)
|
|
df_large = DataFrame(
|
|
id = 1:50000,
|
|
name = ["User_$i" for i in 1:50000],
|
|
value = rand(50000) .* 100,
|
|
status = ["active", "inactive", "pending"][rand(1:3, 50000)]
|
|
)
|
|
|
|
# Randomly choose small or large
|
|
return (rand([df_small, df_large]), "table")
|
|
end
|
|
|
|
# Helper function to create sample image data (Vector{UInt8})
|
|
function create_image_payload()
|
|
# Create random image bytes (small)
|
|
small_image = rand(UInt8, 100_000) # ~100KB
|
|
# Large image (> 1MB)
|
|
large_image = rand(UInt8, 2_000_000) # ~2MB
|
|
|
|
return (rand([small_image, large_image]), "image")
|
|
end
|
|
|
|
# Helper function to create sample audio data (Vector{UInt8})
|
|
function create_audio_payload()
|
|
# Create random audio bytes (small)
|
|
small_audio = rand(UInt8, 150_000) # ~150KB
|
|
# Large audio (> 1MB)
|
|
large_audio = rand(UInt8, 3_000_000) # ~3MB
|
|
|
|
return (rand([small_audio, large_audio]), "audio")
|
|
end
|
|
|
|
# Helper function to create sample video data (Vector{UInt8})
|
|
function create_video_payload()
|
|
# Create random video bytes (small)
|
|
small_video = rand(UInt8, 200_000) # ~200KB
|
|
# Large video (> 1MB)
|
|
large_video = rand(UInt8, 5_000_000) # ~5MB
|
|
|
|
return (rand([small_video, large_video]), "video")
|
|
end
|
|
|
|
# Helper function to create sample binary data (Vector{UInt8})
|
|
function create_binary_payload()
|
|
# Create random binary bytes (small)
|
|
small_binary = rand(UInt8, 50_000) # ~50KB
|
|
# Large binary (> 1MB)
|
|
large_binary = rand(UInt8, 1_500_000) # ~1.5MB
|
|
|
|
return (rand([small_binary, large_binary]), "binary")
|
|
end
|
|
|
|
# Main chat sender function for scenario 6
|
|
function chat_sender(
|
|
subject::String = "/chat/test",
|
|
num_messages::Int = 10;
|
|
nats_url::String = NATS_URL,
|
|
fileserver_url::String = FILESERVER_URL
|
|
)
|
|
println("\n=== Chat Sender (ServiceA) ===")
|
|
println("Subject: $subject")
|
|
println("Number of messages: $num_messages")
|
|
println("NATS URL: $nats_url")
|
|
println("Fileserver URL: $fileserver_url")
|
|
println("="^50)
|
|
|
|
# Create a handler for the fileserver upload
|
|
# This will be passed to smartsend as fileserverUploadHandler parameter
|
|
fileserverUploadHandler = (url, dataname, data) -> NATSBridge.plik_oneshot_upload(url, dataname, data)
|
|
|
|
for i in 1:num_messages
|
|
# Generate random chat message with mixed content
|
|
# Each message can have 1-5 payloads with different types
|
|
num_payloads = rand(1:5)
|
|
|
|
# Create payloads list
|
|
payloads = Tuple{String, Any, String}[]
|
|
|
|
# Track if we need to include text (required for chat)
|
|
has_text = false
|
|
|
|
# Create random payloads
|
|
for j in 1:num_payloads
|
|
# Randomly select a payload type
|
|
payload_type = rand(CHAT_TYPES)
|
|
|
|
# Create the payload based on type
|
|
payload_data, payload_type = if payload_type == "text"
|
|
create_text_payload()
|
|
elseif payload_type == "dictionary"
|
|
create_dictionary_payload()
|
|
elseif payload_type == "table"
|
|
create_table_payload()
|
|
elseif payload_type == "image"
|
|
create_image_payload()
|
|
elseif payload_type == "audio"
|
|
create_audio_payload()
|
|
elseif payload_type == "video"
|
|
create_video_payload()
|
|
elseif payload_type == "binary"
|
|
create_binary_payload()
|
|
end
|
|
|
|
# Ensure at least one text payload
|
|
if payload_type == "text"
|
|
has_text = true
|
|
end
|
|
|
|
push!(payloads, ("payload_$j", payload_data, payload_type))
|
|
end
|
|
|
|
# Ensure at least one text payload exists
|
|
if !has_text
|
|
text_data, text_type = create_text_payload()
|
|
push!(payloads, ("message_text", text_data, text_type))
|
|
end
|
|
|
|
# Generate chat message metadata
|
|
chat_metadata = Dict(
|
|
"message_index" => i,
|
|
"timestamp" => Dates.now().iso8601,
|
|
"sender" => "serviceA",
|
|
"payload_count" => length(payloads)
|
|
)
|
|
|
|
# Send the chat message with mixed content
|
|
println("\n--- Message $i ---")
|
|
println("Payloads: $(length(payloads))")
|
|
for (dataname, data, type) in payloads
|
|
println(" - $dataname (type: $type)")
|
|
end
|
|
|
|
env = NATSBridge.smartsend(
|
|
subject,
|
|
payloads;
|
|
nats_url = nats_url,
|
|
fileserver_url = fileserver_url,
|
|
fileserverUploadHandler = fileserverUploadHandler,
|
|
size_threshold = 1_000_000, # 1MB threshold
|
|
correlation_id = string(uuid4()),
|
|
msg_purpose = "chat",
|
|
sender_name = "serviceA",
|
|
receiver_name = "serviceB",
|
|
reply_to = "/chat/reply",
|
|
reply_to_msg_id = ""
|
|
)
|
|
|
|
println("Envelope created with correlationId: $(env.correlationId)")
|
|
println("Message published successfully!")
|
|
|
|
# Wait a bit between messages
|
|
sleep(rand(0.1:0.3))
|
|
end
|
|
|
|
println("\n=== Chat Sender Complete ===")
|
|
return true
|
|
end
|
|
|
|
# Example usage
|
|
if abspath(PROGRAM_FILE) == @__FILE__
|
|
# Parse command line arguments
|
|
if length(ARGS) >= 2
|
|
subject = ARGS[1]
|
|
num_messages = parse(Int, ARGS[2])
|
|
else
|
|
subject = "/chat/test"
|
|
num_messages = 5
|
|
end
|
|
|
|
chat_sender(subject, num_messages)
|
|
end |