Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.10 KB | None | 0 0
  1. import vibe.d;
  2.  
  3. enum port = 1234;
  4. enum address = "127.0.0.1";
  5.  
  6. shared static this()
  7. {
  8.     auto serverTask = runTask({
  9.         listenTCP(port, (con) {
  10.             auto server = Server(con);
  11.             server.start();
  12.         }, address);
  13.     });
  14.  
  15.     auto clientTask = runTask({
  16.         auto con = connectTCP(address, port);
  17.         auto client = Client(con);
  18.         client.start();
  19.     });
  20.  
  21.     serverTask.join();
  22.     clientTask.join();
  23. }
  24.  
  25. struct Server
  26. {
  27.     mixin ConnectionRunner;
  28.  
  29. private:
  30.  
  31.     void perform ()
  32.     {
  33.         dispatch(nextMessage());
  34.     }
  35.  
  36.     string nextMessage ()
  37.     {
  38.         return cast(string) connection.readLine();
  39.     }
  40.  
  41.     void dispatch (string message)
  42.     {
  43.         logInfo("Received message: '%s'", message);
  44.  
  45.         switch (message)
  46.         {
  47.             case Messages.stop: stop(); break;
  48.             case Messages.foo: foo(); break;
  49.             default: logInfo("Unrecognized message sent: '%s'", message);
  50.         }
  51.     }
  52.  
  53.     void foo ()
  54.     {
  55.         logInfo("This is foo");
  56.     }
  57. }
  58.  
  59. struct Client
  60. {
  61.     mixin ConnectionRunner;
  62.  
  63. private:
  64.  
  65.     void perform ()
  66.     {
  67.         writeLine("foo");
  68.         sleep(500.msecs);
  69.     }
  70.  
  71.     void writeLine (string message)
  72.     {
  73.         connection.write(message ~ "\r\n");
  74.     }
  75. }
  76.  
  77. private enum Messages
  78. {
  79.     stop = "stop",
  80.     foo = "foo"
  81. }
  82.  
  83. template ConnectionRunner ()
  84. {
  85.     private
  86.     {
  87.         bool isRunning;
  88.         TCPConnection connection;
  89.     }
  90.  
  91.     this (TCPConnection connection)
  92.     {
  93.         this.connection = connection;
  94.     }
  95.  
  96.     void start ()
  97.     {
  98.         logInfo("Starting %s", name);
  99.         isRunning = true;
  100.         run();
  101.     }
  102.  
  103. private:
  104.  
  105.     void run ()
  106.     {
  107.         logInfo("Running %s", name);
  108.  
  109.         // while (isRunning && connection.connected)
  110.             perform();
  111.     }
  112.  
  113.     void stop ()
  114.     {
  115.         logInfo("Stopping %s", name);
  116.         isRunning = false;
  117.  
  118.         if (connection.connected)
  119.             connection.close();
  120.     }
  121.  
  122.     string name ()
  123.     {
  124.         return typeid(typeof(this)).name;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement