Commit f4e7c6a0 authored by Haejoong Lee's avatar Haejoong Lee

bug fixes

parent caa9c6a5
......@@ -45,7 +45,7 @@
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"
android:text="8080" >
android:text="@string/http_ui_default_port" >
<requestFocus />
</EditText>
......@@ -61,8 +61,8 @@
android:id="@+id/button_start_http"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/http_ui_start"
android:onClick="startServer" />
android:onClick="startServer"
android:text="@string/http_ui_start" />
<Button
android:id="@+id/button_stop_http"
......@@ -74,11 +74,15 @@
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_server_log"
android:layout_width="match_parent"
android:layout_height="168dp"
android:ems="10" >
</TextView>
android:layout_height="324dp"
android:ems="10" />
</LinearLayout>
\ No newline at end of file
......@@ -36,7 +36,6 @@
<string name="connect">Connect</string>
<string name="http_dialog_success">Server started at port %1$d</string>
<string name="http_dialog_failure">Failed to start server</string>
<string name="http_dialog_title">HTTP</string>
<string name="http_server_activity_title">HTTP Server</string>
<string name="http_server_activity_server_status">Server status</string>
<string name="http_server_activity_host">Host</string>
......@@ -44,4 +43,5 @@
<string name="http_server_activity_ip">IP address</string>
<string name="http_ui_start">START</string>
<string name="http_ui_stop">STOP</string>
<string name="http_ui_default_port">8080</string>
</resources>
......@@ -26,9 +26,11 @@ import java.net.InetAddress;
public class Server extends NanoHTTPD {
private static String host_; // hostname
private static int port_ = -1; // port number
private static Server server_; // singleton server object
private static Server server_ = null; // singleton server object
private static AssetManager am_;
private Proc proc_;
private String current_host_; // hostname/ip of running instance
private int current_port_; // port of running instance
/**
* Protected constructor used by a factory function.
......@@ -91,13 +93,12 @@ public class Server extends NanoHTTPD {
* @return Server or null of server can't be created.
*/
public static Server getServer() {
if (port_ < 0) {
if (host_ == null || port_ <= 0 || port_ > 65535)
return null;
}
if (server_ == null) {
if (server_ == null)
server_ = new Server(host_, port_);
}
return server_;
}
......@@ -167,8 +168,18 @@ public class Server extends NanoHTTPD {
return ips;
}
public String getHost() {
return current_host_;
}
public int getPort() {
return current_port_;
}
@Override
public void start() throws IOException {
current_host_ = host_;
current_port_ = port_;
super.start();
}
......@@ -178,8 +189,13 @@ public class Server extends NanoHTTPD {
}
private Response serveIndex(String path) {
if (path.equals("/")) {
Response r = serveAsset("/transcriber.html");
return r == null ? mkNotFoundResponse(path) : r;
}
String a[] = path.split("/");
if (a[1].equals("index.json")) {
if (a.length == 2 && a[1].equals("index.json")) {
JSONObject originals = new JSONObject();
JSONObject commentaries = new JSONObject();
JSONObject speakers = new JSONObject();
......@@ -205,7 +221,7 @@ public class Server extends NanoHTTPD {
private Response serveRecording(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 3)
if (a.length != 3 || !a[1].equals("recording"))
return null;
try {
......@@ -218,17 +234,16 @@ public class Server extends NanoHTTPD {
}
catch (IllegalArgumentException e) {
return mkNotFoundResponse(path);
}
}
}
private Response serveMapFile(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 4 || !a[3].equals("mapfile"))
if (a.length != 4 || !a[1].equals("recording") || !a[3].equals("mapfile"))
return null;
try {
File mapfile = new File(Recording.getRecordingsPath(), a[2] + ".map");
android.util.Log.e("abc", Recording.getRecordingsPath().toString());
InputStream is = new FileInputStream(mapfile);
return new Response(Status.OK, "text/plain", is);
}
......@@ -239,7 +254,7 @@ public class Server extends NanoHTTPD {
private Response serveShapeFile(String path) {
String[] a = path.split("/");
if (!a[1].equals("recording") || a.length != 4 || !a[3].equals("shapefile"))
if (a.length != 4 || !a[1].equals("recording") || !a[3].equals("shapefile"))
return null;
try {
......@@ -254,7 +269,7 @@ public class Server extends NanoHTTPD {
private Response serveSpeakerImage(String path) {
String[] a = path.split("/");
if (!a[1].equals("speaker") || a.length != 4 || !a[3].equals("image"))
if (a.length != 4 || !a[1].equals("speaker") || !a[3].equals("image"))
return null;
try {
......@@ -267,12 +282,12 @@ public class Server extends NanoHTTPD {
}
catch (IllegalArgumentException e) {
return mkNotFoundResponse(path);
}
}
}
private Response serveSpeakerSmallImage(String path) {
String[] a = path.split("/");
if (!a[1].equals("speaker") || a.length != 4 || !a[3].equals("smallimage"))
if (a.length != 4 || !a[1].equals("speaker") || !a[3].equals("smallimage"))
return null;
try {
......@@ -285,7 +300,7 @@ public class Server extends NanoHTTPD {
}
catch (IllegalArgumentException e) {
return mkNotFoundResponse(path);
}
}
}
private Response serveAsset(String path) {
......@@ -296,7 +311,7 @@ public class Server extends NanoHTTPD {
}
catch (IOException e) {
return mkNotFoundResponse(path);
}
}
}
private String guessMimeType(String path) {
......
......@@ -23,25 +23,57 @@ public class HttpServerActivity extends AikumaActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_server);
// read default port number set in ui
port = Integer.parseInt(((TextView) findViewById(R.id.text_port)).getText().toString());
log = (TextView) findViewById(R.id.text_server_log);
Server.setAssetManager(getAssets());
log = (TextView) findViewById(R.id.text_server_log);
final TextView t = (TextView) findViewById(R.id.text_port);
Boolean running = Server.getServer() != null && Server.getServer().isAlive();
findViewById(R.id.ip_address).setEnabled(!running);
findViewById(R.id.text_port).setEnabled(!running);
findViewById(R.id.button_start_http).setEnabled(!running);
findViewById(R.id.button_stop_http).setEnabled(running);
if (running == true) {
hostname = Server.getServer().getHost();
port = Server.getServer().getPort();
log.append("Server already running at " + hostname + ":" + Integer.toString(port) + "\n");
t.setText(Integer.toString(port));
}
else {
// read default port number set in ui
String p = getString(R.string.http_ui_default_port);
t.setText(p);
port = Integer.parseInt(p);
}
// initially set the ip address field
Map<String,String> addrs = Server.getIpAddresses();
if (addrs.size() > 0) {
selected_device = (String) addrs.keySet().toArray()[0];
for (String dev : addrs.keySet()) {
if (addrs.get(dev).equals(hostname)) {
selected_device = dev;
break;
}
}
if (selected_device == null)
selected_device = (String) addrs.keySet().toArray()[0];
hostname = addrs.get(selected_device).toString();
((TextView) findViewById(R.id.ip_address)).setText(addrs.get(selected_device));
}
TextView t = (TextView) findViewById(R.id.text_port);
t.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
port = Integer.parseInt(s.toString());
try {
port = Integer.parseInt(s.toString());
findViewById(R.id.button_start_http).setEnabled(port > 0 && port < 65536);
}
catch (NumberFormatException e) {
findViewById(R.id.button_start_http).setEnabled(false);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
......@@ -53,6 +85,8 @@ public class HttpServerActivity extends AikumaActivity {
}
public void showNetworkInterfaceDialog(View view) {
if (!findViewById(R.id.ip_address).isEnabled())
return;
Map<String,String> addrs = Server.getIpAddresses();
CharSequence[] menuitems = new CharSequence[addrs.size()];
final String[] devices = new String[addrs.size()];
......@@ -74,6 +108,7 @@ public class HttpServerActivity extends AikumaActivity {
@Override
public void onClick(DialogInterface dialog, int which) {
selected_device = devices[which];
hostname = ipaddrs[which].toString();
((TextView) findViewById(R.id.ip_address)).setText(ipaddrs[which]);
dialog.dismiss();
}
......@@ -99,17 +134,19 @@ public class HttpServerActivity extends AikumaActivity {
log.append(e.getMessage());
log.append("\n");
}
log.setText("Server started.");
findViewById(R.id.ip_address).setEnabled(!success);
findViewById(R.id.text_port).setEnabled(!success);
view.setEnabled(!success);
findViewById(R.id.button_stop_http).setEnabled(success);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.http_dialog_title).setMessage(msg);
AlertDialog dialog = builder.create();
dialog.show();
}
public void stopServer(View view) {
log.setText("Server stopped.");
Server.getServer().stop();
findViewById(R.id.button_start_http).setEnabled(true);
view.setEnabled(false);
findViewById(R.id.ip_address).setEnabled(true);
findViewById(R.id.text_port).setEnabled(true);
}
}
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