1 /*------------------------------------------------------------------------------
  2 Name:      Address.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Holding address string and protocol string
  6 Version:   $Id: Address.cpp 13597 2005-07-28 10:11:52Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 /**
 10  * Helper class holding address string, protocol string and client side connection properties.
 11  * <p />
 12  * <pre>
 13  * &lt;address type='XMLRPC' sessionId='4e56890ghdFzj0'
 14  *           pingInterval='60000' retries='5' delay='10000'
 15  *           oneway='false'>
 16  *    http://server:8080/cb
 17  *    &lt;compress type='gzip' minSize='1000'/>
 18  *    &lt;burstMode collectTime='400'/> <!-- for publishOneway() calls -->
 19  * &lt;/address>
 20  * </pre>
 21  */
 22 
 23 #include <util/qos/address/Address.h>
 24 #include <util/lexical_cast.h>
 25 #include <util/Global.h>
 26 #include <util/StringTrim.h>
 27 
 28 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace address {
 29 
 30 using namespace std;
 31 
 32 inline void Address::initialize()
 33 {
 34    setPort(global_.getProperty().getIntProperty("bootstrapPort", getPort()));
 35 
 36    setType(global_.getProperty().getStringProperty("protocol", getType()));
 37    setType(global_.getProperty().getStringProperty("dispatch/connection/protocol", getType()));
 38    setCollectTime(global_.getProperty().getLongProperty("dispatch/connection/burstMode/collectTime", DEFAULT_collectTime));
 39    setBurstModeMaxEntries(global_.getProperty().getIntProperty("dispatch/connection/burstMode/maxEntries", DEFAULT_burstModeMaxEntries));
 40    setBurstModeMaxBytes(global_.getProperty().getLongProperty("dispatch/connection/burstMode/maxBytes", DEFAULT_burstModeMaxBytes));
 41    setPingInterval(global_.getProperty().getLongProperty("dispatch/connection/pingInterval", defaultPingInterval_));
 42    setRetries(global_.getProperty().getIntProperty("dispatch/connection/retries", defaultRetries_));
 43    setDelay(global_.getProperty().getLongProperty("dispatch/connection/delay", defaultDelay_));
 44    setOneway(global_.getProperty().getBoolProperty("dispatch/connection/oneway", DEFAULT_oneway));
 45    setCompressType(global_.getProperty().getStringProperty("dispatch/connection/compress.type", DEFAULT_compressType));
 46    setMinSize(global_.getProperty().getLongProperty("dispatch/connection/compress.minSize", DEFAULT_minSize));
 47    setPtpAllowed(global_.getProperty().getBoolProperty("dispatch/connection/ptpAllowed", DEFAULT_ptpAllowed));
 48    setSecretSessionId(global_.getProperty().getStringProperty("dispatch/connection/sessionId", DEFAULT_sessionId));
 49    setDispatchPlugin(global_.getProperty().getStringProperty("dispatch/connection/DispatchPlugin/defaultPlugin", DEFAULT_dispatchPlugin));
 50    if (nodeId_ != "") {
 51       setPort(global_.getProperty().getIntProperty("dispatch/connection/port["+nodeId_+"]", getPort()));
 52 
 53       setType(global_.getProperty().getStringProperty("dispatch/connection/protocol["+nodeId_+"]", getType()));
 54       setCollectTime(global_.getProperty().getLongProperty("dispatch/connection/burstMode/collectTime["+nodeId_+"]", getCollectTime()));
 55       setBurstModeMaxEntries(global_.getProperty().getIntProperty("dispatch/connection/burstMode/maxEntries["+nodeId_+"]", getBurstModeMaxEntries()));
 56       setBurstModeMaxBytes(global_.getProperty().getLongProperty("dispatch/connection/burstMode/maxBytes["+nodeId_+"]", getBurstModeMaxBytes()));
 57       setPingInterval(global_.getProperty().getLongProperty("dispatch/connection/pingInterval["+nodeId_+"]", getPingInterval()));
 58       setRetries(global_.getProperty().getIntProperty("dispatch/connection/retries["+nodeId_+"]", getRetries()));
 59       setDelay(global_.getProperty().getLongProperty("dispatch/connection/delay["+nodeId_+"]", getDelay()));
 60       setOneway(global_.getProperty().getBoolProperty("dispatch/connection/oneway["+nodeId_+"]", oneway()));
 61       setCompressType(global_.getProperty().getStringProperty("dispatch/connection/compress.type["+nodeId_+"]", getCompressType()));
 62       setMinSize(global_.getProperty().getLongProperty("dispatch/connection/compress.minSize["+nodeId_+"]", getMinSize()));
 63       setPtpAllowed(global_.getProperty().getBoolProperty("dispatch/connection/ptpAllowed["+nodeId_+"]", isPtpAllowed()));
 64       setSecretSessionId(global_.getProperty().getStringProperty("dispatch/connection/sessionId["+nodeId_+"]", getSecretSessionId()));
 65       setDispatchPlugin(global_.getProperty().getStringProperty("dispatch/connection/DispatchPlugin/defaultPlugin["+nodeId_+"]", dispatchPlugin_));
 66    }
 67 
 68    // TODO: This is handled in ClientQueueProperty.java already ->
 69    //      long maxEntries = global_.getProperty().getLongProperty("queue/connection/maxEntries", CbQueueProperty.DEFAULT_maxEntriesDefault);
 70    long maxEntries = global_.getProperty().getLongProperty("queue/connection/maxEntries", 10000l);
 71    setMaxEntries(maxEntries);
 72    if (nodeId_ != "") {
 73       setMaxEntries(global_.getProperty().getLongProperty("queue/connection/maxEntries["+nodeId_+"]", getMaxEntries()));
 74    }
 75 
 76    // Resets cached rawAddress_ :
 77    string type = getType();
 78    StringTrim::toLowerCase(type);
 79    // These properties are evaluated directly by our C SOCKET library:
 80    // -dispatch/connection/plugin/socket/port
 81    // -dispatch/connection/plugin/socket/hostname
 82    // -dispatch/connection/plugin/socket/localPort
 83    // -dispatch/connection/plugin/socket/localHostname
 84    hostname_ = global_.getProperty().getStringProperty("dispatch/connection/plugin/"+type+"/hostname", getHostname());
 85    setPort(global_.getProperty().getIntProperty("dispatch/connection/plugin/"+type+"/port", getPort()));
 86 }
 87 
 88 Address::Address(Global& global, const string& type, const string& nodeId)
 89  : AddressBase(global, "address")
 90 {
 91    defaultRetries_      = -1;    // How often to retry if connection fails: defaults to -1 (retry forever), 0 switches failsafe mode off
 92    defaultDelay_        = 5000;  // Delay between connection retries in milliseconds: defaults to 5000 (5 sec)
 93    defaultPingInterval_ = 10000; // Ping interval: pinging every given milliseconds, defaults to 10 seconds
 94    pingInterval_ = defaultPingInterval_;
 95    retries_      = defaultRetries_;
 96    delay_        = defaultDelay_;
 97    ME = "Address";
 98    if (nodeId != "") nodeId_ = nodeId;
 99    initialize();
100    if (type != "")   type_ = type;
101 }
102 
103 Address::Address(const AddressBase& addr) : AddressBase(addr)
104 {
105 }
106 
107 Address& Address::operator =(const AddressBase& addr)
108 {
109    AddressBase::copy(addr);
110    return *this;
111 }
112 
113 
114 void Address::setMaxEntries(long maxEntries)
115 {
116    maxEntries_ = maxEntries;
117 }
118 
119 long Address::getMaxEntries() const
120 {
121    return maxEntries_;
122 }
123 
124 /** For logging only */
125 string Address::getSettings()
126 {
127    string ret;
128    ret = AddressBase::getSettings();
129    if (getDelay() > 0)
130       ret += string(" delay=") + lexical_cast<std::string>(getDelay()) +
131              string(" retries=") + lexical_cast<std::string>(getRetries()) +
132              string(" maxEntries=") + lexical_cast<std::string>(getMaxEntries()) +
133              string(" pingInterval=") + lexical_cast<std::string>(getPingInterval());
134    return ret;
135 }
136 
137 string Address::toString()
138 {
139    return getRawAddress();
140 }
141 
142 /**
143  * Get a usage string for the connection parameters
144  */
145 string Address::usage()
146 {
147    string text = "";
148    text += string("Control failsafe connection to xmlBlaster server:\n");
149    // is in ClientQueueProperty.java: text += "   -queue/connection/maxEntries       The max. capacity of the client queue in number of messages [" + CbQueueProperty.DEFAULT_maxEntriesDefault + "].\n";
150    //text += "   -queue/callback/onOverflow   Error handling when queue is full, 'block | deadMessage' [" + CbQueueProperty.DEFAULT_onOverflow + "].\n";
151    //text += "   -queue/callback/onFailure    Error handling when connection failed (after all retries etc.) [" + CbQueueProperty.DEFAULT_onFailure + "].\n";
152    text += string("   -dispatch/connection/burstMode/collectTime [" + lexical_cast<std::string>(DEFAULT_collectTime) + "]\n");
153    text += string("                       Number of milliseconds we shall collect publish messages.\n");
154    text += string("                       This allows performance tuning, try set it to 200.\n");
155    text += string("   -dispatch/connection/burstMode/maxEntries [" + lexical_cast<std::string>(DEFAULT_burstModeMaxEntries) + "]\n");
156    text += string("                       The maximum number of queue entries to send in a bulk.\n");
157    text += string("                       -1L takes all entries of highest priority available in the ram queue in a bulk.\n");
158    text += string("   -dispatch/connection/burstMode/maxBytes [" + lexical_cast<std::string>(DEFAULT_burstModeMaxBytes) + "]\n");
159    text += string("                       The maximum bulk size of invocations.\n");
160    text += string("                       -1L takes all entries of highest priority available in the ram queue in a bulk.\n");
161  //text += "   -oneway             Shall the publish() messages be send oneway (no application level ACK) [" + Address.DEFAULT_oneway + "]\n";
162    text += string("   -dispatch/connection/pingInterval [" + lexical_cast<std::string>(defaultPingInterval_) + "]\n");
163    text += string("                       Pinging every given milliseconds.\n");
164    text += string("   -dispatch/connection/retries [" + lexical_cast<std::string>(defaultRetries_) + "]\n");
165    text += string("                       How often to retry if connection fails (-1 is forever).\n");
166    text += string("   -dispatch/connection/delay [" + lexical_cast<std::string>(defaultDelay_) + "]\n");
167    text += string("                       Delay between connection retries in milliseconds.\n");
168    text += string("                       A delay value > 0 switches fails save mode on, 0 switches it off.\n");
169  //text += "   -DispatchPlugin/defaultPlugin  Specify your specific dispatcher plugin [" + CallbackAddress.DEFAULT_dispatchPlugin + "]\n";
170  //text += "   -compress.type      With which format message be compressed on callback [" + Address.DEFAULT_compressType + "]\n";
171  //text += "   -compress.minSize   Messages bigger this size in bytes are compressed [" + Address.DEFAULT_minSize + "]\n";
172    return text;
173 }
174 
175 }}}}} // namespace
176 
177 
178 #ifdef _XMLBLASTER_CLASSTEST
179 
180 using namespace std;
181 using namespace org::xmlBlaster::util::qos::address;
182 
183 /** For testing */
184 int main(int args, char* argv[])
185 {
186    try {
187       {
188          Global& glob = Global::getInstance();
189          glob.initialize(args, argv);
190          Log& log = glob.getLog("org.xmlBlaster.util.qos");
191          log.info("main", "This is a simple info");
192          Address a(glob);
193          a.setType("SOCKET");
194          a.setAddress("127.0.0.1:7600");
195          a.setCollectTime(12345l);
196          a.setPingInterval(54321l);
197          a.setRetries(17);
198          a.setDelay(7890l);
199          a.setOneway(true);
200          a.setSecretSessionId("0x4546hwi89");
201          cout << a.toXml() << endl;
202       }
203       {
204          string nodeId = "heron";
205          int                nmax = 8;
206          const char** argc = new const char*[nmax];
207          argc[0] = "-sessionId";
208          argc[1] = "ERROR";
209          string help = string("-sessionId[") + nodeId + string("]");
210          argc[2] = string(help).c_str();
211          argc[3] = "OK";
212          argc[4] = "-pingInterval";
213          argc[5] = "8888";
214          help = string("-delay[") + nodeId + string("]");
215          argc[6] = help.c_str();
216          argc[7] = "8888";
217 
218          Global& glob = Global::getInstance();
219          glob.initialize(nmax, argc);
220          Address a(glob, "RMI", nodeId);
221          cout << a.toXml() << endl;
222       }
223    }
224    catch(...) {
225       cout << "unknown uncatched exception" << endl;
226    }
227 }
228 
229 #endif


syntax highlighted by Code2HTML, v. 0.9.1