forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_connector.cpp
More file actions
73 lines (64 loc) · 1.58 KB
/
tcp_connector.cpp
File metadata and controls
73 lines (64 loc) · 1.58 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include "tcp_messenger.h"
#include "tcp_connector.h"
#include <cppcms/cppcms_error.h>
namespace cppcms {
namespace impl {
tcp_connector::tcp_connector(std::vector<std::string> const& ip,std::vector<int> const &port)
{
if(ip.size()<1 || port.size()!=ip.size()) {
throw cppcms_error("Incorrect parameters for tcp cache");
}
conns=ip.size();
tcp=new messenger[conns];
try {
for(int i=0;i<conns;i++) {
tcp[i].connect(ip[i],port[i]);
}
}
catch(...) {
delete [] tcp;
tcp=NULL;
throw;
}
}
tcp_connector::~tcp_connector()
{
delete [] tcp;
}
void tcp_connector::broadcast(tcp_operation_header &h,std::string &data)
{
int i;
for(i=0;i<conns;i++) {
tcp_operation_header ht=h;
std::string dt=data;
tcp[i].transmit(ht,dt);
}
}
unsigned tcp_connector::hash(std::string const &key)
{
if(conns==1) return 0;
unsigned h=0;
// crc
for(size_t i=0;i<key.size();i++) {
unsigned char c=key[i];
unsigned highorder = h & 0xf8000000u;
h<<=5;
h^=highorder >> 27;
h^=c;
}
return h % conns;
}
messenger &tcp_connector::get(std::string const &key)
{
return tcp[hash(key)];
}
} // impl
} // cppcms