mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
wip: data gets to relay and response handled
A device registers and a game can start. But we don't get to being able to make a move yet.
This commit is contained in:
parent
2dc80ac93f
commit
b86ffeb2b9
2 changed files with 69 additions and 29 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import mod_python, json
|
||||
import mod_python, json, socket, base64
|
||||
|
||||
try:
|
||||
from mod_python import apache
|
||||
|
@ -10,12 +10,31 @@ except ImportError:
|
|||
print('failed')
|
||||
|
||||
def post(req, params):
|
||||
err = 'none'
|
||||
dataLen = 0
|
||||
jobj = json.loads(params)
|
||||
jobj = {'data' : jobj['data']}
|
||||
data = base64.b64decode(jobj['data'])
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.settimeout(3) # seconds
|
||||
addr = ("127.0.0.1", 10997)
|
||||
sock.sendto(data, addr)
|
||||
|
||||
response = None
|
||||
try:
|
||||
data, server = sock.recvfrom(1024)
|
||||
response = base64.b64encode(data)
|
||||
except socket.timeout:
|
||||
#If data is not received back from server, print it has timed out
|
||||
err = 'timeout'
|
||||
|
||||
jobj = {'err' : err, 'data' : response}
|
||||
return json.dumps(jobj)
|
||||
|
||||
def main():
|
||||
None
|
||||
params = { 'data' : 'V2VkIE9jdCAxOCAwNjowNDo0OCBQRFQgMjAxNwo=' }
|
||||
params = json.dumps(params)
|
||||
print(post(None, params))
|
||||
|
||||
##############################################################################
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -242,30 +242,9 @@ sendAckIf( RelayConStorage* storage, const MsgHeader* header )
|
|||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
relaycon_receive( GIOChannel* source, GIOCondition XP_UNUSED_DBG(condition), gpointer data )
|
||||
static gboolean
|
||||
process( RelayConStorage* storage, XP_U8* buf, ssize_t nRead )
|
||||
{
|
||||
XP_ASSERT( 0 != (G_IO_IN & condition) ); /* FIX ME */
|
||||
RelayConStorage* storage = (RelayConStorage*)data;
|
||||
XP_U8 buf[512];
|
||||
struct sockaddr_in from;
|
||||
socklen_t fromlen = sizeof(from);
|
||||
|
||||
int socket = g_io_channel_unix_get_fd( source );
|
||||
XP_LOGF( "%s: calling recvfrom on socket %d", __func__, socket );
|
||||
|
||||
ssize_t nRead = recvfrom( socket, buf, sizeof(buf), 0, /* flags */
|
||||
(struct sockaddr*)&from, &fromlen );
|
||||
|
||||
gchar* b64 = g_base64_encode( (const guchar*)buf,
|
||||
((0 <= nRead)? nRead : 0) );
|
||||
XP_LOGF( "%s: read %zd bytes ('%s')", __func__, nRead, b64 );
|
||||
g_free( b64 );
|
||||
#ifdef COMMS_CHECKSUM
|
||||
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, buf, nRead );
|
||||
XP_LOGF( "%s: read %zd bytes ('%s')(sum=%s)", __func__, nRead, b64, sum );
|
||||
g_free( sum );
|
||||
#endif
|
||||
if ( 0 <= nRead ) {
|
||||
const XP_U8* ptr = buf;
|
||||
const XP_U8* end = buf + nRead;
|
||||
|
@ -369,6 +348,33 @@ relaycon_receive( GIOChannel* source, GIOCondition XP_UNUSED_DBG(condition), gpo
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
relaycon_receive( GIOChannel* source, GIOCondition XP_UNUSED_DBG(condition), gpointer data )
|
||||
{
|
||||
XP_ASSERT( 0 != (G_IO_IN & condition) ); /* FIX ME */
|
||||
RelayConStorage* storage = (RelayConStorage*)data;
|
||||
XP_U8 buf[512];
|
||||
struct sockaddr_in from;
|
||||
socklen_t fromlen = sizeof(from);
|
||||
|
||||
int socket = g_io_channel_unix_get_fd( source );
|
||||
XP_LOGF( "%s: calling recvfrom on socket %d", __func__, socket );
|
||||
|
||||
ssize_t nRead = recvfrom( socket, buf, sizeof(buf), 0, /* flags */
|
||||
(struct sockaddr*)&from, &fromlen );
|
||||
|
||||
gchar* b64 = g_base64_encode( (const guchar*)buf,
|
||||
((0 <= nRead)? nRead : 0) );
|
||||
XP_LOGF( "%s: read %zd bytes ('%s')", __func__, nRead, b64 );
|
||||
g_free( b64 );
|
||||
#ifdef COMMS_CHECKSUM
|
||||
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, buf, nRead );
|
||||
XP_LOGF( "%s: read %zd bytes ('%s')(sum=%s)", __func__, nRead, b64, sum );
|
||||
g_free( sum );
|
||||
#endif
|
||||
return process( storage, buf, nRead );
|
||||
}
|
||||
|
||||
void
|
||||
relaycon_cleanup( LaunchParams* params )
|
||||
{
|
||||
|
@ -427,7 +433,7 @@ write_callback(void *contents, size_t size, size_t nmemb, void* data)
|
|||
}
|
||||
|
||||
static ssize_t
|
||||
post( const XP_U8* msgbuf, XP_U16 len )
|
||||
post( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
|
||||
{
|
||||
const char* data = g_base64_encode( msgbuf, len );
|
||||
struct json_object* jobj = json_object_new_object();
|
||||
|
@ -471,11 +477,26 @@ post( const XP_U8* msgbuf, XP_U16 len )
|
|||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
(void)json_object_put( jobj );
|
||||
|
||||
XP_LOGF( "%s(): got \"%s\"", __func__, rs.ptr );
|
||||
|
||||
/* Now pull any data from the reply */
|
||||
// got "{"status": "ok", "dataLen": 14, "data": "AYQDiDAyMUEzQ0MyADw=", "err": "none"}"
|
||||
json_object* reply = json_tokener_parse( rs.ptr );
|
||||
json_object* replyData;
|
||||
if ( json_object_object_get_ex( reply, "data", &replyData ) && !!replyData ) {
|
||||
const char* str = json_object_get_string( replyData );
|
||||
gsize out_len;
|
||||
guchar* buf = g_base64_decode( (const gchar*)str, &out_len );
|
||||
process( storage, buf, len );
|
||||
g_free( buf );
|
||||
(void)json_object_put( replyData );
|
||||
}
|
||||
(void)json_object_put( reply );
|
||||
|
||||
g_free( rs.ptr );
|
||||
|
||||
(void)json_object_put( jobj );
|
||||
return len;
|
||||
}
|
||||
|
||||
|
@ -484,7 +505,7 @@ sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
|
|||
{
|
||||
ssize_t nSent;
|
||||
if (1) {
|
||||
nSent = post( msgbuf, len );
|
||||
nSent = post( storage, msgbuf, len );
|
||||
} else {
|
||||
nSent = sendto( storage->socket, msgbuf, len, 0, /* flags */
|
||||
(struct sockaddr*)&storage->saddr,
|
||||
|
|
Loading…
Add table
Reference in a new issue