[go: up one dir, main page]

Menu

[r32]: / server / spe.cpp  Maximize  Restore  History

Download this file

200 lines (148 with data), 6.0 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
/*****************************************************************************
Copyright © 2006, 2007, The Board of Trustees of the University of Illinois.
All Rights Reserved.
National Center for Data Mining (NCDM)
University of Illinois at Chicago
http://www.ncdm.uic.edu/
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu [gu@lac.uic.edu], last updated 03/24/2007
*****************************************************************************/
#include <server.h>
#include <util.h>
#include <dlfcn.h>
using namespace cb;
void* Server::SPEHandler(void* p)
{
Server* self = ((Param4*)p)->s;
UDTSOCKET u = ((Param4*)p)->u;
string ip = ((Param4*)p)->ip;
int port = ((Param4*)p)->port;
int uport = ((Param4*)p)->p;
SPE spe = ((Param4*)p)->spe;
delete (Param4*)p;
CCBMsg msg;
sockaddr_in cli_addr;
cli_addr.sin_family = AF_INET;
cli_addr.sin_port = uport;
inet_pton(AF_INET, ip.c_str(), &cli_addr.sin_addr);
memset(&(cli_addr.sin_zero), '\0', 8);
cout << "rendezvous connect " << ip << " " << port << endl;
if (UDT::ERROR == UDT::connect(u, (sockaddr*)&cli_addr, sizeof(sockaddr_in)))
return NULL;
timeval t1, t2;
gettimeofday(&t1, 0);
int size = spe.m_llSize;
char* block = new char[size];
//check if file already exists!
if (self->m_LocalFile.lookup(spe.m_strDataFile.c_str(), NULL) > 0)
{
ifstream ifs;
ifs.open((self->m_strHomeDir + spe.m_strDataFile).c_str());
ifs.read(block, size);
ifs.close();
cout << "read data into block...\n";
}
else
{
int fid = DHash::hash(spe.m_strDataFile.c_str(), m_iKeySpace);
Node n;
if (- 1 == self->m_Router.lookup(fid, &n))
return NULL;
msg.setType(1); // locate file
msg.setData(0, spe.m_strDataFile.c_str(), spe.m_strDataFile.length() + 1);
msg.m_iDataLength = 4 + spe.m_strDataFile.length() + 1;
if (self->m_GMP.rpc(n.m_pcIP, n.m_iAppPort, &msg, &msg) < 0)
return NULL;
string srcip = msg.getData();
int srcport = *(int32_t*)(msg.getData() + 64);
int mode = 1; // READ ONLY
UDTSOCKET fu = UDT::socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = 0;
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);
UDT::bind(fu, (sockaddr*)&my_addr, sizeof(my_addr));
int size = sizeof(sockaddr_in);
UDT::getsockname(u, (sockaddr*)&my_addr, &size);
msg.setType(2); // open the file
msg.setData(0, spe.m_strDataFile.c_str(), spe.m_strDataFile.length() + 1);
msg.setData(64, (char*)&mode, 4);
msg.setData(68, (char*)&my_addr.sin_port, 4);
msg.m_iDataLength = 4 + 64 + 4 + 4;
if (self->m_GMP.rpc(srcip.c_str(), srcport, &msg, &msg) < 0)
return NULL;
msg.setType(-8);
msg.m_iDataLength = 4;
int rendezvous = 1;
UDT::setsockopt(fu, 0, UDT_RENDEZVOUS, &rendezvous, 4);
sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = *(int*)(msg.getData()); // port
inet_pton(AF_INET, srcip.c_str(), &serv_addr.sin_addr);
memset(&(serv_addr.sin_zero), '\0', 8);
if (UDT::ERROR == UDT::connect(fu, (sockaddr*)&serv_addr, sizeof(serv_addr)))
return NULL;
int h;
if (UDT::ERROR == UDT::recv(fu, block, size, 0, &h))
return NULL;
int32_t cmd = 4;
if (UDT::ERROR == UDT::send(fu, (char*)&cmd, 4, 0))
return NULL;
UDT::close(fu);
}
cout << "locating so " << (self->m_strHomeDir + spe.m_strOperator + ".so") << endl;
void* handle = dlopen((self->m_strHomeDir + spe.m_strOperator + ".so").c_str(), RTLD_LAZY);
if (NULL == handle)
return NULL;
cout << "so found " << "locating process " << spe.m_strOperator << endl;
int (*process)(const char*, const int&, char*, int&, const char*, const int&);
process = (int (*) (const char*, const int&, char*, int&, const char*, const int&) )dlsym(handle, spe.m_strOperator.c_str());
if (NULL == process)
{
cout << dlerror() << endl;
return NULL;
}
cout << "process found~\n";
char* res = new char[size];
int rsize = 0;
int rs = size;
for (int progress = 0; progress < size; progress += spe.m_iUnitSize)
{
process(block + progress, spe.m_iUnitSize, res + rsize, rs, spe.m_pcParam, spe.m_iParamSize);
rsize += rs;
rs = size - rsize;
}
dlclose(handle);
msg.setType(1); // success, return result
msg.setData(0, (char*)&(spe.m_uiID), 4);
msg.setData(4, (char*)&rsize, 4);
msg.m_iDataLength = 4 + 8;
if (self->m_GMP.rpc(ip.c_str(), port, &msg, &msg) < 0)
return NULL;
cout << "sending data back... " << rsize << endl;
int h;
UDT::send(u, res, size, 0, &h);
gettimeofday(&t2, 0);
int duration = t2.tv_sec - t1.tv_sec;
UDT::close(u);
cout << "comp server closed " << ip << " " << port << " " << duration << endl;
delete [] block;
delete [] res;
if (NULL != spe.m_pcParam)
delete [] spe.m_pcParam;
return NULL;
}