Commit d9d9ed5a authored by Dave Griffiths's avatar Dave Griffiths
Browse files

network and http requests wired into scheme code

parent 45fd16ec
......@@ -374,6 +374,9 @@
(define (make-directory name) (list "make-directory" 0 "make-directory" name))
;; treat this like a dialog so the callback fires
(define (list-files name path fn) (list "list-files" 0 "list-files" name fn path))
(define (network-connect name ssid fn) (list "network-connect" 0 "network-connect" name fn ssid))
(define (http-request name url fn) (list "http-request" 0 "http-request" name fn url))
(define (send-mail to subject body attachments) (list "send-mail" 0 "send-mail" to subject body attachments))
(define (time-picker-dialog name fn)
......@@ -521,7 +524,9 @@
;; todo - something a bit more fancy
(equal? (list-ref event 0) "date-picker-dialog")
(equal? (list-ref event 0) "alert-dialog")
(equal? (list-ref event 0) "list-files"))
(equal? (list-ref event 0) "list-files")
(equal? (list-ref event 0) "http-request")
(equal? (list-ref event 0) "network-connect"))
(add-new-dialog! event)))
events)))
......
......@@ -563,8 +563,22 @@
(text-view (make-id "sync-title") "Sync database" 40 fillwrap)
(mtext "sync-dirty" "...")
(horiz
(mtext "sync-range" "Out of range")
(mbutton "sync-sync" "Sync" (lambda () (list))))
(mbutton "sync-connect" "Connect"
(lambda ()
(list
(network-connect
"network"
"mongoose-web"
(lambda (state)
(msg state)
(list
(update-widget 'text-view (get-id "sync-connect") 'text state)))))))
(mbutton "sync-sync" "Sync"
(lambda ()
(list
(http-request "myreq" "http://192.168.2.1:8888/mongoose?function_name=ping"
(lambda (v)
(msg "got" v)))))))
(text-view (make-id "sync-console") "..." 15 (layout 300 'wrap-content 1 'left))
(mbutton "main-send" "Done" (lambda () (list (finish-activity 2)))))
......
......@@ -13,6 +13,8 @@ import android.widget.Toast;
import android.net.wifi.WifiManager;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.net.wifi.WifiConfiguration;
......@@ -29,15 +31,26 @@ import android.os.SystemClock;
public class NetworkManager {
public enum State {
SCANNING, CONNECTED // what else?
IDLE, SCANNING, CONNECTED // what else?
}
WifiManager wifi;
BroadcastReceiver receiver;
State state;
public State state;
String SSID;
NetworkManager(String ssid, Context c) {
String m_CallbackName;
StarwispActivity m_Context;
StarwispBuilder m_Builder;
NetworkManager() {
state = State.IDLE;
}
void Start(String ssid, StarwispActivity c, String name, StarwispBuilder b) {
m_CallbackName=name;
m_Context=c;
m_Builder=b;
wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
state = State.SCANNING;
SSID = ssid;
......@@ -45,6 +58,8 @@ public class NetworkManager {
receiver = new WiFiScanReceiver(SSID, this);
c.registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"Scanning\"");
}
void Connect() {
......@@ -63,6 +78,7 @@ public class NetworkManager {
wifi.enableNetwork(i.networkId, true);
wifi.reconnect();
Log.i("starwisp", "Connected");
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"Connected\"");
break;
}
}
......@@ -80,29 +96,31 @@ public class NetworkManager {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifi.addNetwork(conf);
}
else
{
StartRequestThread();
}
}
public void StartRequestThread() {
public void StartRequestThread(final String url, final String callbackname) {
Runnable runnable = new Runnable() {
public void run() {
Request();
Request(url, callbackname);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
private void Request() {
try {
SystemClock.sleep(7000);
private class ReqMsg {
ReqMsg(InputStream is, String c) {
m_Stream=is;
m_CallbackName=c;
}
public InputStream m_Stream;
public String m_CallbackName;
}
private void Request(String u, String CallbackName) {
try {
Log.i("starwisp", "Pinging URL");
String u="http://192.168.2.1:8888/mongoose?function_name=ping";
Log.i("starwisp",u);
URL url = new URL(u);
HttpURLConnection con = (HttpURLConnection) url
......@@ -116,22 +134,38 @@ public class NetworkManager {
con.connect();
Log.i("starwisp", "Connection open");
readStream(con.getInputStream());
//readStream(con.getInputStream());
Log.i("starwisp", "read stream, ending");
m_RequestHandler.sendMessage(
Message.obtain(m_RequestHandler, 0,
new ReqMsg(con.getInputStream(),CallbackName)));
} catch (Exception e) {
Log.i("starwisp",e.toString());
e.printStackTrace();
}
}
private void readStream(InputStream in) {
private Handler m_RequestHandler = new Handler(){
@Override
public void handleMessage(Message msg){
ReadStream((ReqMsg)msg.obj);
}
};
private void ReadStream(ReqMsg m) {
InputStream in = m.m_Stream;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
String all = "";
while ((line = reader.readLine()) != null) {
all+=line+"\n";
Log.i("starwisp",line);
}
m_Builder.DialogCallback(m_Context,m.m_CallbackName,all);
} catch (IOException e) {
e.printStackTrace();
} finally {
......@@ -168,6 +202,7 @@ public class NetworkManager {
for (ScanResult result : results) {
if (result.SSID.equals(SSID)) {
m_Builder.DialogCallback(m_Context,m_CallbackName,"\"In range\"");
nm.Connect();
return;
}
......
......@@ -83,9 +83,11 @@ import org.json.JSONArray;
public class StarwispBuilder
{
Scheme m_Scheme;
NetworkManager m_NetworkManager;
public StarwispBuilder(Scheme scm) {
m_Scheme = scm;
m_NetworkManager = new NetworkManager();
}
public int BuildOrientation(String p) {
......@@ -129,6 +131,17 @@ public class StarwispBuilder
}
}
public void DialogCallback(StarwispActivity ctx, String name, String args)
{
try {
String ret=m_Scheme.eval("(dialog-callback \""+name+"\" '("+args+"))");
UpdateList(ctx, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing data " + e.toString());
}
}
private void Callback(StarwispActivity ctx, int wid)
{
try {
......@@ -461,17 +474,32 @@ public class StarwispBuilder
}
code+=")";
try {
String ret=m_Scheme.eval("(dialog-callback \""+ name+"\" '("+code+"))");
UpdateList(ctx, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing data " + e.toString());
}
DialogCallback((StarwispActivity)ctx, name, code);
}
}
return;
}
if (token.equals("network-connect")) {
if (m_NetworkManager.state==NetworkManager.State.IDLE) {
final String name = arr.getString(3);
final String ssid = arr.getString(5);
m_NetworkManager.Start(ssid,(StarwispActivity)ctx,name,this);
}
return;
}
if (token.equals("http-request")) {
if (m_NetworkManager.state==NetworkManager.State.CONNECTED) {
Log.i("starwisp","attempting http request");
final String name = arr.getString(3);
final String url = arr.getString(5);
m_NetworkManager.StartRequestThread(url,name);
}
return;
}
if (token.equals("send-mail")) {
final String to[] = new String[1];
to[0]=arr.getString(3);
......@@ -518,13 +546,7 @@ public class StarwispBuilder
ctx,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
try {
String ret=m_Scheme.eval("(dialog-callback \""+
name+"\" '("+day+" "+month+" "+year+"))");
UpdateList(ctx, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing data " + e.toString());
}
DialogCallback((StarwispActivity)ctx, name, day+" "+month+" "+year);
}
}, year, month, day);
d.show();
......@@ -541,13 +563,7 @@ public class StarwispBuilder
public void onClick(DialogInterface dialog, int which) {
int result = 0;
if (which==DialogInterface.BUTTON_POSITIVE) result=1;
String ret=m_Scheme.eval("(dialog-callback \""+
name+"\" '("+result+"))");
try {
UpdateList(ctx, new JSONArray(ret));
} catch (JSONException e) {
Log.e("starwisp", "Error parsing data " + e.toString());
}
DialogCallback((StarwispActivity)ctx, name, ""+result);
}
};
......
......@@ -77,8 +77,6 @@ public class starwisp extends StarwispActivity
ActivityManager.Register("sync",SyncActivity.class);
};
NetworkManager nm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
......@@ -90,8 +88,6 @@ public class starwisp extends StarwispActivity
File appdir = new File(m_AppDir);
appdir.mkdirs();
nm = new NetworkManager("mongoose-web",this);
// build static things
m_Scheme = new Scheme(this);
m_Builder = new StarwispBuilder(m_Scheme);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment