1 // Copyright (c) 2013, Peter Wood. 2 // See license.txt for licensing details. 3 module stalkd.server; 4 5 import stalkd.connection; 6 import stalkd.exceptions; 7 import stalkd.tube; 8 9 /** 10 * This class encapsulates the details for a server running the Beanstalkd 11 * application and provides a main interaction point for users to obtain 12 * Tube objects. 13 */ 14 struct Server { 15 /** 16 * A constant defining the default Beanstalkd port. 17 */ 18 static const DEFAULT_BEANSTALKD_PORT = 11300; 19 20 /** 21 * Constructor for the Server class. 22 * 23 * Params: 24 * host = The name or IP address of the Beanstalkd server. 25 * port = The port number that Beanstalkd is listening on. Defaults to 26 * DEFAULT_BEANSTALKD_PORT. 27 */ 28 this(string host, ushort port=DEFAULT_BEANSTALKD_PORT) { 29 _host = host; 30 _port = port; 31 } 32 33 @property string host() { 34 return(_host); 35 } 36 37 @property ushort port() { 38 return(_port); 39 } 40 41 /** 42 * This method fetches a Tube object representing a named tube on a server 43 * running Beanstalkd. This is the preferred method of obtaining a tube as 44 * it hides the various class interactions behind a simple interface call. 45 * 46 * Params: 47 * name = The name of the tube on the server. Defaults to 48 * Tube.DEFAULT_TUBE_NAME. 49 */ 50 Tube getTube(string name=Tube.DEFAULT_TUBE_NAME) { 51 Connection connection = new Connection(this); 52 Tube tube = new Tube(connection); 53 54 if(name !is Tube.DEFAULT_TUBE_NAME) { 55 tube.use(name); 56 } 57 return(tube); 58 } 59 60 private string _host; 61 private ushort _port; 62 } 63 64 //------------------------------------------------------------------------------ 65 // Unit Tests 66 //------------------------------------------------------------------------------ 67 unittest { 68 auto server = new Server("localhost"); 69 70 assert(server.host is "localhost"); 71 assert(server.port == Server.DEFAULT_BEANSTALKD_PORT); 72 73 server = new Server("blah.com", 13214); 74 75 assert(server.host is "blah.com"); 76 assert(server.port == 13214); 77 assert(server.getTube() !is null); 78 }