nwaku/library/events/json_message_event.nim
Ivan Folgueira Bande 29614e2e52
Refactoring of libwaku to export only C types (#1845)
* Simplifying libwaku.nim by extracting config parser to config.nim
* Adding json_base_event.nim
* Starting to control-version the libwaku.h

  We are creating this libwaku.h inspired by the one that is automatically
  generated by the nim compiler when `make libwaku` is invoked. Therefore,
  the self-generated header is then placed in:

  nimcache/release/libwaku/libwaku.h

* Better waku_example.c organization
* libwaku.nim: better memory management

  We need to create a 'cstring' internally from the 'const char*' passed
  from outside the library.

  We invoke 'allocShared' in order to create the internal 'cstring',
  and invoke 'deallocShared' in order to manually free the memory.
2023-07-07 10:53:00 +02:00

43 lines
1.0 KiB
Nim

import
std/json
import
../../waku/v2/waku_core/message/message,
./json_base_event
type JsonMessage = ref object
# https://rfc.vac.dev/spec/36/#jsonmessage-type
payload: string
contentTopic: string
version: uint
timestamp: int64
type JsonMessageEvent* = ref object of JsonEvent
pubsubTopic*: string
messageId*: string
wakuMessage*: JsonMessage
proc new*(T: type JsonMessageEvent,
pubSubTopic: string,
msg: WakuMessage): T =
# Returns a WakuMessage event as indicated in
# https://rfc.vac.dev/spec/36/#jsonmessageevent-type
var payload = newString(len(msg.payload))
copyMem(addr payload[0], unsafeAddr msg.payload[0], len(msg.payload))
return JsonMessageEvent(
eventType: "message",
pubSubTopic: pubSubTopic,
messageId: "TODO",
wakuMessage: JsonMessage(
payload: payload,
contentTopic: msg.contentTopic,
version: msg.version,
timestamp: int64(msg.timestamp)
)
)
method `$`*(jsonMessage: JsonMessageEvent): string =
$( %* jsonMessage )