Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F63701658
client.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, May 21, 23:01
Size
3 KB
Mime Type
text/x-c
Expires
Thu, May 23, 23:01 (2 d)
Engine
blob
Format
Raw Data
Handle
17808329
Attached To
rSCMINICLUSTER SCITAS Mini Cluster
client.cc
View Options
#include "client.hh"
#include "commands.hh"
#include <QByteArray>
#include <QDataStream>
#include <QTcpSocket>
#include <iostream>
/* -------------------------------------------------------------------------- */
GridSender
::
GridSender
(
Grid
&
grid
)
:
_grid
(
grid
),
prank
(
0
),
psize
(
0
)
{}
void
GridSender
::
setParallelContext
(
int
prank
,
int
psize
)
{
this
->
prank
=
prank
;
this
->
psize
=
psize
;
}
void
GridSender
::
connect
(
const
std
::
string
&
server_name
,
int
port
)
{
socket
=
new
QTcpSocket
();
qDebug
()
<<
prank
<<
"Connecting to"
<<
server_name
.
c_str
();
socket
->
connectToHost
(
QString
(
server_name
.
c_str
()),
port
);
socket
->
waitForConnected
();
qDebug
()
<<
prank
<<
"Connected"
;
char
command
=
CONFIGURE
;
socket
->
putChar
(
command
);
QByteArray
data
;
QDataStream
buffer
(
&
data
,
QIODevice
::
WriteOnly
);
buffer
<<
prank
;
buffer
<<
psize
;
buffer
<<
int
(
_grid
.
size
(
0
));
buffer
<<
int
(
_grid
.
size
(
1
));
buffer
<<
int
(
_grid
.
globalSize
(
0
));
buffer
<<
int
(
_grid
.
globalSize
(
1
));
buffer
<<
int
(
_grid
.
offset
(
0
));
buffer
<<
int
(
_grid
.
offset
(
1
));
buffer
<<
int
(
_grid
.
ghost
());
socket
->
write
(
data
);
qDebug
()
<<
prank
<<
"Waiting for connection confirmation..."
;
socket
->
waitForReadyRead
();
socket
->
getChar
(
&
command
);
qDebug
()
<<
prank
<<
"command"
<<
int
(
command
)
<<
"ok!"
;
}
/* -------------------------------------------------------------------------- */
GridSender
::~
GridSender
()
{
socket
->
disconnectFromHost
();
socket
->
waitForDisconnected
();
}
/* -------------------------------------------------------------------------- */
void
GridSender
::
send
(
bool
wait
)
{
char
command
=
wait
?
READ_IMAGE_WAIT
:
READ_IMAGE
;
socket
->
putChar
(
command
);
qDebug
()
<<
prank
<<
"Sending"
<<
_grid
.
fullSize
()
<<
"chars"
;
socket
->
write
(
_grid
.
data
(),
_grid
.
fullSize
());
socket
->
waitForBytesWritten
();
if
(
wait
)
{
qDebug
()
<<
prank
<<
"Waiting..."
;
if
(
socket
->
waitForReadyRead
())
{
socket
->
getChar
(
&
command
);
qDebug
()
<<
prank
<<
"command"
<<
int
(
command
)
<<
"ok!"
;
}
else
{
qFatal
(
"Confirmation timeout!!!"
);
}
}
}
/* -------------------------------------------------------------------------- */
void
GridSender
::
recv_update
()
{
char
command
=
CHECK_UPDATE
;
socket
->
putChar
(
command
);
qDebug
()
<<
prank
<<
"Sending command"
<<
int
(
command
);
socket
->
waitForReadyRead
();
socket
->
getChar
(
&
command
);
while
(
command
==
SEND_GRID_UPDATE
)
{
QByteArray
data
;
int
expected_size
=
4
*
sizeof
(
int
)
+
sizeof
(
bool
);
while
(
data
.
size
()
<
expected_size
)
{
data
.
append
(
socket
->
read
(
expected_size
-
data
.
size
()));
}
QDataStream
stream
(
&
data
,
QIODevice
::
ReadOnly
);
int
size
[
2
];
int
origin
[
2
];
uint8_t
type
;
stream
>>
size
[
0
];
stream
>>
size
[
1
];
stream
>>
origin
[
0
];
stream
>>
origin
[
1
];
stream
>>
type
;
data
.
clear
();
QDataStream
buffer
(
&
data
,
QIODevice
::
ReadOnly
);
while
(
data
.
size
()
<
size
[
0
]
*
size
[
1
])
{
data
.
append
(
socket
->
read
(
size
[
0
]
*
size
[
1
]
-
data
.
size
()));
}
for
(
int
i
=
0
;
i
<
size
[
0
];
++
i
)
{
for
(
int
j
=
0
;
j
<
size
[
1
];
++
j
)
{
uint8_t
val
;
buffer
>>
val
;
if
(
type
==
REPLACE
||
(
type
==
ADD
&&
val
==
1
))
_grid
(
origin
[
0
]
+
i
,
origin
[
1
]
+
j
)
=
val
;
}
}
socket
->
waitForReadyRead
();
socket
->
getChar
(
&
command
);
}
qDebug
()
<<
prank
<<
"command"
<<
int
(
command
)
<<
"ok!"
;
}
void
GridSender
::
wait
()
{
char
command
=
WAIT
;
socket
->
putChar
(
command
);
qDebug
()
<<
prank
<<
"Sending wait"
<<
int
(
command
);
socket
->
waitForReadyRead
();
socket
->
getChar
(
&
command
);
qDebug
()
<<
prank
<<
"command"
<<
int
(
command
)
<<
"ok!"
;
}
Event Timeline
Log In to Comment