Signed-off-by: Lin Ma <lma@xxxxxxxx> --- tools/virsh-network.c | 17 ++++++++++++++--- tools/virsh-network.h | 8 ++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tools/virsh-network.c b/tools/virsh-network.c index 9b86ef8071..d5b3649050 100644 --- a/tools/virsh-network.c +++ b/tools/virsh-network.c @@ -1156,6 +1156,7 @@ struct virshNetEventData { bool loop; bool timestamp; int count; + virshNetworkEventCallback *cb; }; typedef struct virshNetEventData virshNetEventData; @@ -1195,6 +1196,12 @@ vshEventLifecyclePrint(virConnectPtr conn ATTRIBUTE_UNUSED, vshEventDone(data->ctl); } +virshNetworkEventCallback virshNetworkEventCallbacks[] = { + { "lifecycle", + VIR_NETWORK_EVENT_CALLBACK(vshEventLifecyclePrint), }, +}; +verify(VIR_NETWORK_EVENT_ID_LAST == ARRAY_CARDINALITY(virshNetworkEventCallbacks)); + static const vshCmdInfo info_network_event[] = { {.name = "help", .data = N_("Network Events") @@ -1246,7 +1253,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd) size_t i; for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++) - vshPrint(ctl, "%s\n", virshNetworkEventIdTypeToString(i)); + vshPrint(ctl, "%s\n", virshNetworkEventCallbacks[i].name); return true; } @@ -1256,7 +1263,10 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd) vshError(ctl, "%s", _("either --list or --event <type> is required")); return false; } - if ((event = virshNetworkEventIdTypeFromString(eventName)) < 0) { + for (event = 0; event < VIR_NETWORK_EVENT_ID_LAST; event++) + if (STREQ(eventName, virshNetworkEventCallbacks[event].name)) + break; + if (event == VIR_NETWORK_EVENT_ID_LAST) { vshError(ctl, _("unknown event type %s"), eventName); return false; } @@ -1265,6 +1275,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd) data.loop = vshCommandOptBool(cmd, "loop"); data.timestamp = vshCommandOptBool(cmd, "timestamp"); data.count = 0; + data.cb = &virshNetworkEventCallbacks[event]; if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0) return false; @@ -1274,7 +1285,7 @@ cmdNetworkEvent(vshControl *ctl, const vshCmd *cmd) goto cleanup; if ((eventId = virConnectNetworkEventRegisterAny(priv->conn, net, event, - VIR_NETWORK_EVENT_CALLBACK(vshEventLifecyclePrint), + data.cb->cb, &data, NULL)) < 0) goto cleanup; switch (vshEventWait(ctl)) { diff --git a/tools/virsh-network.h b/tools/virsh-network.h index 0fff4b7748..9c86eb5bc9 100644 --- a/tools/virsh-network.h +++ b/tools/virsh-network.h @@ -32,6 +32,14 @@ virshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd, virshCommandOptNetworkBy(_ctl, _cmd, _name, \ VIRSH_BYUUID | VIRSH_BYNAME) +struct virshNetworkEventCallback { + const char *name; + virConnectNetworkEventGenericCallback cb; +}; +typedef struct virshNetworkEventCallback virshNetworkEventCallback; + +extern virshNetworkEventCallback virshNetworkEventCallbacks[]; + extern const vshCmdDef networkCmds[]; #endif /* LIBVIRT_VIRSH_NETWORK_H */ -- 2.19.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list