-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundleDownloader.cpp
130 lines (116 loc) · 3.08 KB
/
BundleDownloader.cpp
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
/*
* BundleDownloader.cpp
*
* Created on: Apr 4, 2012
* Author: iraklis
*/
#include <ma.h>
#include <mavsprintf.h>
#include <conprint.h>
#include "BundleDownloader.h"
BundleDownloader::BundleDownloader(BundleListener *bl):mSocket(this)
{
mBundleListener = bl;
maTextBox(L"Server IP:",L"localhost", mWServerAddress, 128, 0);
Environment::getEnvironment().addTextBoxListener(this);
}
void BundleDownloader::textBoxClosed(int result, int textLength)
{
if(result == MA_TB_RES_OK)
{
mDownloader = new Downloader();
mDownloader->addDownloadListener(this);
//User tries to connect, reset the socket and start a new connection
mSocket.close();
wcstombs(mServerAddress, mWServerAddress, 128);
//Add the port number to the IP
sprintf(mBuffer,"socket://%s:7000", mServerAddress);
lprintfln(mBuffer);
mSocket.connect(mBuffer);
}
}
//The socket->connect() operation has finished
void BundleDownloader::connectFinished(Connection *conn, int result)
{
printf("connection result: %d\n", result);
if(result > 0)
{
mSocket.recv(mBuffer,1024);
}
else
{
//showConErrorMessage(result);
}
}
//We received a TCP message from the server
void BundleDownloader::connRecvFinished(Connection *conn, int result)
{
lprintfln("recv result: %d\n", result);
if(result > 0)
{
//Null terminate the string message (it's a URL of the .bin bundle)
mBuffer[result] = '\0';
sprintf(mBundleAddress,"http://%ls:8282%s", mServerAddress, mBuffer);
lprintfln("FileURL:%s\n",mBundleAddress);
//Reset the app environment (destroy widgets, stop sensors)
//freeHardware();
downloadBundle();
//Set the socket to receive the next TCP message
mSocket.recv(mBuffer, 1024);
}
else
{
printf("connRecvFinished result %d", result);
//showConErrorMessage(result);
}
}
void BundleDownloader::downloadBundle()
{
//Prepare a reciever for the download
mResourceFile = maCreatePlaceholder();
//Start the bundle download
if(mDownloader->isDownloading())
{
mDownloader->cancelDownloading();
}
int res = mDownloader->beginDownloading(mBundleAddress, mResourceFile);
if(res > 0)
{
printf("Downloading Started with %d\n", res);
}
else
{
//showConErrorMessage(res);
}
}
/**
* Called when a download operation is canceled
* @param downloader The downloader that was canceled
*/
void BundleDownloader::downloadCancelled(Downloader* downloader)
{
printf("Cancelled");
}
/**
* Method displays error code in case of error in downloading.
* @param downloader The downloader that got the error
* @param code The error code that was returned
*/
void BundleDownloader::error(Downloader* downloader, int code)
{
printf("Error: %d", code);
//showConErrorMessage(code);
}
/**
* Called when the download is complete
* @param downloader The downloader who finished it's operation
* @param data A handle to the data that was downloaded
*/
void BundleDownloader::finishedDownloading(Downloader* downloader, MAHandle data)
{
lprintfln("Completed download");
//extract the file System
mBundleListener->bundleDownloaded(data);
maDestroyPlaceholder(mResourceFile);
//loadSavedApp();
}