]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
auth: generalize mount-like ops in MonClient
authorYehuda Sadeh <yehuda@hq.newdream.net>
Tue, 25 Aug 2009 17:15:33 +0000 (10:15 -0700)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Tue, 25 Aug 2009 17:15:33 +0000 (10:15 -0700)
src/mon/MonClient.cc
src/mon/MonClient.h

index e81d6626787d498b6539b2324acfc62642864890..e10daf7edcc27c910084427fca77098fa4f84456 100644 (file)
@@ -312,78 +312,87 @@ void MonClient::pick_new_mon()
   monmap.pick_mon(true);
 }
 
-void MonClient::_try_do_op(MonClientOpCtx& ctx, double timeout)
+void MonClient::_try_do_op(MonClientOpHandler *ctx, double timeout)
 {
   dout(10) << "_try_do_op" << dendl;
   int mon = monmap.pick_mon();
   dout(2) << "sending client_mount to mon" << mon << dendl;
 
-  MAuth *msg = new MAuth;
-  if (!msg)
-    return;
-
-  bufferlist& bl = msg->get_auth_payload();
-
-  EntityName name;
-  entity_addr_t my_addr;
-
-  build_authenticate_request(name, my_addr, bl);
+  Message *msg = ctx->build_request();
   
   messenger->set_dispatcher(this);
   messenger->send_message(msg, monmap.get_inst(mon));
 
   // schedule timeout?
-  assert(ctx.timeout_event == 0);
-  ctx.timeout_event = new C_OpTimeout(this, &ctx, timeout);
-  timer.add_event_after(timeout, ctx.timeout_event);
+  assert(ctx->timeout_event == 0);
+  ctx->timeout_event = new C_OpTimeout(this, ctx, timeout);
+  timer.add_event_after(timeout, ctx->timeout_event);
 }
 
-/*
-  get ticket-granting-ticket for this principle
-*/
-int MonClient::do_op(MonClientOpCtx& ctx, double timeout)
+int MonClient::do_op(MonClientOpHandler *ctx, double timeout)
 {
   Mutex::Locker lock(monc_lock);
 
-  if (ctx.done) {
+  if (ctx->done) {
     dout(5) << "op already done" << dendl;;
     return 0;
   }
 
   // only the first does the work
   bool itsme = false;
-  if (!ctx.num_waiters) {
+  if (!ctx->num_waiters) {
     itsme = true;
     _try_do_op(ctx, timeout);
   } else {
     dout(5) << "additional get_tgt" << dendl;
   }
-  ctx.num_waiters++;
+  ctx->num_waiters++;
 
-  while (!ctx.got_data ||
-        (!itsme && !ctx.done)) // non-doers wait a little longer
-       ctx.cond.Wait(monc_lock);
+  while (!ctx->got_data() ||
+        (!itsme && !ctx->done)) // non-doers wait a little longer
+       ctx->cond.Wait(monc_lock);
 
   if (!itsme) {
     dout(5) << "additional get_tgt returning" << dendl;
-    assert(ctx.got_data);
+    assert(ctx->got_data());
     return 0;
   }
 
   // finish.
-  timer.cancel_event(ctx.timeout_event);
-  ctx.timeout_event = 0;
+  timer.cancel_event(ctx->timeout_event);
+  ctx->timeout_event = 0;
 
-  ctx.got_data = true;
-  ctx.cond.SignalAll(); // wake up non-doers
+  ctx->cond.SignalAll(); // wake up non-doers
 
   return 0;
 }
 
-void MonClient::_op_timeout(MonClientOpCtx& ctx, double timeout)
+void MonClient::_op_timeout(MonClientOpHandler *ctx, double timeout)
 {
   dout(10) << "_op_timeout" << dendl;
-  ctx.timeout_event = 0;
+  ctx->timeout_event = 0;
   _try_do_op(ctx, timeout);
 }
 
+Message *MonClient::MonClientMountHandler::build_request()
+{
+  return new MClientMount;
+}
+
+
+Message *MonClient::MonClientGetTGTHandler::build_request()
+{
+  MAuth *msg = new MAuth;
+  if (!msg)
+    return NULL;
+
+  bufferlist& bl = msg->get_auth_payload();
+
+  EntityName name;
+  entity_addr_t my_addr;
+
+  build_authenticate_request(name, my_addr, bl);
+
+  return msg;
+}
+
index 3e2383d1bcd5b40cc04957f7070ba80391943f4d..b8a03cec89b8e86e848d8318b7463d21e12ca781 100644 (file)
@@ -60,42 +60,67 @@ private:
     }
   };
 
-  class MonClientOpCtx {
+  class MonClientOpHandler {
+  protected:
+    MonClient *client;
   public:
     bool done;
     int num_waiters;
     Cond cond;
     Context *timeout_event;
-    bool got_data;
 
-    MonClientOpCtx() {
+    MonClientOpHandler(MonClient *c) : client(c) {
       done = false;
       num_waiters = 0;
-      got_data = false;
     }
+
+    virtual ~MonClientOpHandler() {}
+
+    virtual Message *build_request() = 0;
+    virtual bool got_data() = 0;
   };
   
   class C_OpTimeout : public Context {
+  protected:
     MonClient *client;
-    MonClientOpCtx *op_ctx;
+    MonClientOpHandler *op_ctx;
     double timeout;
   public:
-    C_OpTimeout(MonClient *c, MonClientOpCtx *opc, double to) :
+    C_OpTimeout(MonClient *c, MonClientOpHandler *opc, double to) :
                                         client(c), op_ctx(opc), timeout(to) {
     }
     void finish(int r) {
-      if (r >= 0) client->_op_timeout(*op_ctx, timeout);
+      if (r >= 0) client->_op_timeout(op_ctx, timeout);
     }
   };
 
+  class MonClientMountHandler : public MonClientOpHandler {
+  public:
+    MonClientMountHandler(MonClient *c) : MonClientOpHandler(c) {}
+    ~MonClientMountHandler() {}
+
+    Message *build_request();
+    bool got_data() { return client->signed_ticket.length() != 0; }
+  };
+
+  class MonClientGetTGTHandler : public MonClientOpHandler {
+    bool has_data;
+  public:
+    MonClientGetTGTHandler(MonClient *c) : MonClientOpHandler(c) {}
+    ~MonClientGetTGTHandler() {}
+
+    Message *build_request();
+    bool got_data() { return client->tgt.length() != 0; }
+  };
+
   void _try_mount(double timeout);
   void _mount_timeout(double timeout);
   void handle_mount_ack(MClientMountAck* m);
   void handle_unmount(Message* m);
-  void _op_timeout(MonClientOpCtx& ctx, double timeout);
+  void _op_timeout(MonClientOpHandler *ctx, double timeout);
 
-  void _try_do_op(MonClientOpCtx& ctx, double timeout);
-  int do_op(MonClientOpCtx& ctx, double timeout);
+  void _try_do_op(MonClientOpHandler *ctx, double timeout);
+  int do_op(MonClientOpHandler* ctx, double timeout);
 
  public:
   MonClient() : messenger(NULL),
@@ -136,6 +161,22 @@ private:
     return entity_inst_t();
   }
   int get_num_mon() {
+  class MonClientOpCtx {
+  public:
+    bool done;
+    int num_waiters;
+    Cond cond;
+    Context *timeout_event;
+    bool got_data;
+
+    MonClientOpCtx() {
+      done = false;
+      num_waiters = 0;
+      got_data = false;
+    }
+  };
+  
+
     Mutex::Locker l(monc_lock);
     return monmap.size();
   }