GBMessage.h 7.67 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
#ifndef GBMESSAGE_HPP_
#define GBMESSAGE_HPP_

#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include "../tinyxml2/tinyxml2.h"


#if defined(CATCH_RESIP_EXCEPTION)
#undef __DUM_TRY
#undef __DUM_CATCH
#define __DUM_TRY try {
#define __DUM_CATCH \
} catch(resip::DumException* be) { \
	resip::Data ex; \
	ex = "RESIP ³öÏÖÒì³££ºname = "; \
	ex += be->name(); \
	ex += "£¬what = "; \
	ex += be->what(); \
	throw new std::exception(ex.c_str()); \
}
#else
#undef  __DUM_TRY
#undef  __DUM_CATCH
#define __DUM_TRY
#define __DUM_CATCH
#endif


class CGBMessage
{
public:
	enum EGBResultType
	{
		RESULT_OK = 0,
		RESULT_ERROR,
	};

	struct SDate
	{
		int year;
		int month;
		int day;
		int hour;
		int minute;
		int second;
		int millisecond;
	};

	struct SSubject
	{
		std::string id;
		std::string seq;
	};

	struct SUserIdentity
	{
		std::string user;
		std::string deviceid;
		std::string organization;
		std::string policetype;
		std::string duty;
	};

public:
	CGBMessage() : m_adjusttime( false ), m_port( 0 ), m_cseq( 0 ), m_forwards( 0 ), m_expires( 0 ), m_statusCode( 0 )
	{
	}

	virtual ~CGBMessage()
	{
	}

private:
	CGBMessage( const CGBMessage &rhs );
	CGBMessage& operator=( const CGBMessage &rhs );

public:
	inline bool Ok() const
	{
		return( m_statusCode == 200 );
	}

	inline bool AdjustTime() const
	{
		return m_adjusttime;
	}

	inline const std::string& GetSubjectSendID() const
	{
		return m_sender.id;
	}

	inline const std::string& GetSubjectSendSeq() const
	{
		return m_sender.seq;
	}

	inline const std::string& GetSubjectRecvID() const
	{
		return m_recver.id;
	}

	inline const std::string& GetSubjectRecvSeq() const
	{
		return m_recver.seq;
	}

	inline const std::string& GetUser() const
	{
		return m_request;
	}

	inline const std::string& GetSubject() const
	{
		return m_subject;
	}

	inline const std::string& GetFrom() const
	{
		return m_from;
	}

	inline const std::string& GetFromTag() const
	{
		return m_tagfrom;
	}

	inline const std::string& GetTo() const
	{
		return m_to;
	}

	inline const std::string& GetToTag() const
	{
		return m_tagto;
	}

	inline unsigned long GetCSeq() const
	{
		return m_cseq;
	}

	inline int GetStatusCode() const
	{
		return m_statusCode;
	}

	inline int GetMaxFrowards() const
	{
		return m_forwards;
	}

	inline int GetExpires() const
	{
		return m_expires;
	}

	inline const std::string& GetIP() const
	{
		return m_ip;
	}

	inline int GetPort() const
	{
		return m_port;
	}

	inline const std::string& GetCallID() const
	{
		return m_callid;
	}

	inline const SDate& GetDate() const
	{
		return m_date;
	}

	inline const SUserIdentity& GetUserIdentity() const
	{
		return m_useridentity;
	}

	inline const std::vector< std::string >& GetContacts() const
	{
		return m_contacts;
	}
	
	inline const std::vector< std::string >& GetVias() const
	{
		return m_vias;
	}

	inline void SetSubjectSendID( const std::string &id )
	{
		m_sender.id = id;
	}

	inline void SetSubjectSendSeq( const std::string &seq )
	{
		m_sender.seq = seq;
	}

	inline void SetSubjectRecvID( const std::string &id )
	{
		m_recver.id = id;
	}

	inline void SetSubjectRecvSeq( const std::string &seq )
	{
		m_recver.seq = seq;
	}

	inline void SetUser( const char *user )
	{
		if( user != NULL )
		{
			m_request = user;
		}
	}

	inline void SetUser( const std::string &user )
	{
		m_request = user;
	}

	inline void SetSubject( const char *uri )
	{
		if( uri != NULL )
		{
			m_subject = uri;
			DecodeSubject();
		}
	}

	inline void SetSubject( const std::string &uri )
	{
		m_subject = uri;
		DecodeSubject();
	}

	inline void SetFrom( const char *code )
	{
		if( code != NULL )
		{
			m_from = code;
		}
	}

	inline void SetFrom( const std::string &code )
	{
		m_from = code;
	}

	inline void SetFromTag( const char *tag )
	{
		if( tag != NULL )
		{
			m_tagfrom = tag;
		}
	}

	inline void SetFromTag( const std::string &code )
	{
		m_tagfrom = code;
	}

	inline void SetTo( const char *code )
	{
		if( code != NULL )
		{
			m_to = code;
		}
	}

	inline void SetTo( const std::string &code )
	{
		m_to = code;
	}

	inline void SetToTag( const char *tag )
	{
		if( tag != NULL )
		{
			m_tagto = tag;
		}
	}

	inline void SetToTag( const std::string &tag )
	{
		m_tagto = tag;
	}

	inline void SetCSeq( unsigned long cseq )
	{
		m_cseq = cseq;
	}

	inline void SetExpires( int expires )
	{
		m_expires = expires;
	}

	inline void SetStatusCode( int code )
	{
		m_statusCode = code;
	}

	inline void SetMaxFrowards( int forwards )
	{
		m_forwards = forwards;
	}

	inline void SetCallID( const char *callid )
	{
		if( callid != NULL )
		{
			m_callid = callid;
		}
	}

	inline void SetCallID( const std::string &callid )
	{
		m_callid = callid;
	}

	inline void SetIP( const std::string &ip )
	{
		m_ip = ip;
	}

	inline void SetIP( const char *ip )
	{
		if( ip != NULL )
		{
			m_ip = ip;
		}
	}

	inline void SetPort( int port )
	{
		m_port = port;
	}

	inline void SetDate( int year, int month, int day, int hour, int minute, int second, int millisecond )
	{
		m_adjusttime = true;
		m_date.year   = year;
		m_date.month  = month;
		m_date.day    = day;
		m_date.hour   = hour;
		m_date.minute = minute;
		m_date.second = second;
		m_date.millisecond = millisecond;
	}

	inline void AddContacts( const char *contacts )
	{
		if( contacts != NULL )
		{
			m_contacts.push_back( contacts );
		}
	}

	inline void AddContacts( const std::string &contacts )
	{
		m_contacts.push_back( contacts );
	}

	inline void AddVias( const char *host, long port )
	{
		if( host != NULL )
		{
			char buf[16] = { 0 };
			sprintf( buf, "%d", port );
			std::string via = host;
			via += ":";
			via += buf;
			m_vias.push_back( via );
		}
	}

	inline void AddVias( const std::string &host, long port )
	{
		char buf[16] = { 0 };
		sprintf( buf, "%d", port );
		std::string via = host;
		via += ":";
		via += buf;
		m_vias.push_back( via );
	}

	inline void SetUserIdentity( const std::string &user, const std::string& deviceid, const std::string& organization, const std::string& policetype, const std::string& duty )
	{
		m_useridentity.user = user;
		m_useridentity.deviceid = deviceid;
		m_useridentity.organization = organization;
		m_useridentity.policetype = policetype;
		m_useridentity.duty  = duty;
	}

public:

	virtual bool Response( int status )
	{
		return false;
	}

public:
	static bool CompareNoCase( const char *type, const char *label );
	static bool CompareNoCase( const std::string &type, const char *label );
	static unsigned int GetNextSN();
	bool DecodeSubject();

protected:
	SSubject                    m_sender;	 // subject
	SSubject                    m_recver;	// subject
	std::string					m_request;
	std::string                 m_subject;
	std::string					m_callid;
	std::string					m_from;
	std::string					m_tagfrom;
	std::string					m_to;
	std::string					m_tagto;
	SDate                       m_date;
	std::vector< std::string >	m_contacts;
	std::vector< std::string >	m_vias;
	std::string                 m_ip;
	bool                        m_adjusttime;    // message has date header
	int                         m_port;
	unsigned long				m_cseq;
	int                         m_forwards;
	int                         m_expires;
	int                         m_statusCode;
	SUserIdentity               m_useridentity;
};

//////////////////////////////////////////////////////////////////////////
#endif // GBMESSAGE_HPP_