hammer2 - spanning tree and messaging work
[dragonfly.git] / sbin / hammer2 / cmd_service.c
index fb4344d..848a1e6 100644 (file)
@@ -88,10 +88,14 @@ cmd_service(void)
        lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
        if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
                close(lfd);
-               fprintf(stderr, "master listen: daemon already running\n");
+               if (QuietOpt == 0) {
+                       fprintf(stderr,
+                               "master listen: daemon already running\n");
+               }
                return 0;
        }
-       fprintf(stderr, "master listen: startup\n");
+       if (QuietOpt == 0)
+               fprintf(stderr, "master listen: startup\n");
        listen(lfd, 50);
 
        /*
@@ -174,6 +178,8 @@ master_service(void *data)
  *                         AUTHENTICATION                              *
  ************************************************************************
  *
+ * Callback via hammer2_iocom_core().
+ *
  * Additional messaging-based authentication must occur before normal
  * message operation.  The connection has already been encrypted at
  * this point.
@@ -196,31 +202,60 @@ master_auth_tx(hammer2_iocom_t *iocom __unused)
        iocom->sendmsg_callback = master_link_tx;
 }
 
-/*
- * Callback from hammer2_iocom_core() when messages might be present
- * on the socket.
+/************************************************************************
+ *                     POST-AUTHENTICATION SERVICE MSGS                *
+ ************************************************************************
+ *
+ * Callback via hammer2_iocom_core().
  */
 static
 void
 master_link_rx(hammer2_iocom_t *iocom)
 {
        hammer2_msg_t *msg;
+       hammer2_state_t *state;
+       uint32_t cmd;
 
        while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
               (msg = hammer2_ioq_read(iocom)) != NULL) {
-               fprintf(stderr, "MSG RECEIVED: %08x error %d\n",
-                       msg->any.head.cmd, msg->any.head.error);
-               switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
-               case HAMMER2_LNK_ERROR:
-                       break;
-               case HAMMER2_DBG_SHELL:
-               case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
-                       hammer2_debug_remote(msg);
-                       break;
-               default:
-                       hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
-                       break;
+               /*
+                * If the message state has a function established we just
+                * call the function, otherwise we call the appropriate
+                * link-level protocol related to the original command and
+                * let it sort it out.
+                *
+                * Non-transactional one-off messages, on the otherhand,
+                * might have REPLY set.
+                */
+               state = msg->state;
+               if (state) {
+                       cmd = state->msg->any.head.cmd;
+                       fprintf(stderr,
+                               "MSGRX persist=%08x cmd=%08x error %d\n",
+                               cmd, msg->any.head.cmd, msg->any.head.error);
+               } else {
+                       cmd = msg->any.head.cmd;
+                       fprintf(stderr,
+                               "MSGRX persist=-------- cmd=%08x error %d\n",
+                               cmd, msg->any.head.error);
+               }
+               if (state && state->func) {
+                       state->func(state, msg);
+               } else {
+                       switch(cmd & HAMMER2_MSGF_PROTOS) {
+                       case HAMMER2_MSG_PROTO_LNK:
+                               hammer2_msg_lnk(iocom, msg);
+                               break;
+                       case HAMMER2_MSG_PROTO_DBG:
+                               hammer2_msg_dbg(iocom, msg);
+                               break;
+                       default:
+                               hammer2_msg_reply(iocom, msg,
+                                                 HAMMER2_MSG_ERR_UNKNOWN);
+                               break;
+                       }
                }
+               hammer2_state_cleanuprx(iocom, msg);
        }
        if (iocom->ioq_rx.error) {
                fprintf(stderr,
@@ -237,5 +272,5 @@ static
 void
 master_link_tx(hammer2_iocom_t *iocom)
 {
-       hammer2_iocom_flush(iocom);
+       hammer2_iocom_flush1(iocom);
 }