diff options
author | Dwight Merriman <dwight@10gen.com> | 2010-04-18 13:05:57 -0400 |
---|---|---|
committer | Dwight Merriman <dwight@10gen.com> | 2010-04-18 13:05:57 -0400 |
commit | aaf23bff896a62a57ada59e19e4d34e380981dd7 (patch) | |
tree | d03d64fe45f98ea24017f3bd241cae1167a0314e /util/hostandport.h | |
parent | a9cb4e6918bce0364ec0ebdea78f9ef9bf28d01d (diff) | |
download | mongo-aaf23bff896a62a57ada59e19e4d34e380981dd7.tar.gz |
hostandport.h
Diffstat (limited to 'util/hostandport.h')
-rw-r--r-- | util/hostandport.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/util/hostandport.h b/util/hostandport.h new file mode 100644 index 00000000000..f3f6cc3a26f --- /dev/null +++ b/util/hostandport.h @@ -0,0 +1,75 @@ +// hostandport.h + +/* Copyright 2009 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "sock.h" +#include "../db/cmdline.h" + +namespace mongo { + + /** helper for manipulating host:port connection endpoints. + */ + struct HostAndPort { + HostAndPort() : _port(-1) { } + + HostAndPort(string h, int p = -1) : _host(h), _port(p) { } + + bool operator<(const HostAndPort& r) const { return _host < r._host || (_host==r._host&&_port<r._port); } + + /* returns true if the host/port combo identifies this process instance. */ + bool isSelf() const; + + bool isLocalHost() const; + + string toString(); + private: + // invariant (except full obj assignment): + string _host; + int _port; + }; + + /** returns true if strings share a common starting prefix */ + inline bool sameStart(const char *p, const char *q) { + while( 1 ) { + if( *p == 0 || *q == 0 ) + return true; + if( *p != *q ) + break; + p++; q++; + } + return false; + } + + inline bool HostAndPort::isSelf() const { + if( _port != cmdLine.port ) + return false; + assert( _host != "localhost" && _host != "127.0.0.1" ); + return sameStart(getHostName().c_str(), _host.c_str()); + } + + inline string HostAndPort::toString() { + stringstream ss; + ss << _host << ':' << _port; + return ss.str(); + } + + inline bool HostAndPort::isLocalHost() const { + return _host == "localhost" || _host == "127.0.0.1"; + } + +} |