package org.pileus.spades;
-import java.io.BufferedReader;
-import java.io.PrintWriter;
+import java.io.*;
+import java.net.*;
public class Client
{
/* Private data */
private String server = null;
+ private int port = 6667;
private String nickname = null;
private String channel = null;
private String username = null;
private String hostname = null;
+ private Socket socket = null;
private BufferedReader input = null;
private PrintWriter output = null;
- /* Public data */
- public boolean running = true;
-
/* Public Methods */
- public Client(String server, String nickname, String channel,
- String username, String hostname)
+ public Client(String username, String hostname)
{
- this.server = server;
- this.nickname = nickname;
- this.channel = channel;
this.username = username;
this.hostname = hostname;
Os.debug("Client: create");
}
- public Client(String server, String nickname, String channel)
+ public Client()
{
- this(server, nickname, channel, "user", "localhost");
+ this("user", "localhost");
}
- public void connect(BufferedReader input, PrintWriter output)
+ public boolean connect(String server, String nickname, String channel)
{
- this.input = input;
- this.output = output;
Os.debug("Client: connect");
- putline("USER "+username+" "+hostname+" "+server+" :"+nickname);
- putline("NICK "+nickname);
+
+ this.server = server;
+ this.nickname = nickname;
+ this.channel = channel;
+
+ try {
+ socket = new Socket(this.server, this.port);
+ input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ output = new PrintWriter(socket.getOutputStream());
+ } catch (Exception e) {
+ Os.debug("Client: failed to create connection: " + e);
+ return false;
+ }
+
+ Os.debug("Client: connected");
+ raw("USER "+username+" "+hostname+" "+server+" :"+nickname);
+ raw("NICK "+nickname);
+
+ return true;
+ }
+
+ public boolean abort()
+ {
+ Os.debug("Client: abort");
+ try {
+ this.socket.close();
+ return true;
+ } catch (Exception e) {
+ Os.debug("Client: error closing socket", e);
+ return false;
+ }
+ }
+
+ public void raw(String line)
+ {
+ try {
+ Os.debug("< " + line);
+ output.println(line);
+ output.flush();
+ } catch (Exception e) {
+ Os.debug("Client: error writing line", e);
+ }
}
public Message send(String txt)
{
- Message msg = new Message(channel, nickname, txt);
- putline(msg.line);
+ Message msg = new Message(channel, nickname, txt);
+ raw(msg.line);
return msg;
}
public Message recv()
{
try {
- String line = getline();
- if (line == null) {
- this.running = false;
+ String line = input.readLine();
+ if (line == null)
return null;
- } else {
- Message msg = new Message(line);
- process(msg);
- return msg;
- }
+ Os.debug("> " + line);
+ Message msg = new Message(line);
+ process(msg);
+ return msg;
+ } catch (SocketException e) {
+ return null;
} catch (Exception e) {
Os.debug("Client: error in recv", e);
- this.running = false;
return null;
}
}
private void process(Message msg)
{
if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
- putline("JOIN " + channel);
- putline("TOPIC " + channel);
+ raw("JOIN " + channel);
+ raw("TOPIC " + channel);
}
if (msg.cmd.equals("PING")) {
- putline("PING " + msg.msg);
- }
- }
-
- private String getline()
- {
- try {
- String line = input.readLine();
- if (line != null)
- Os.debug("> " + line);
- return line;
- } catch (Exception e) {
- Os.debug("Client: error reading line", e);
- this.running = false;
- return "";
- }
- }
-
- private void putline(String line)
- {
- try {
- Os.debug("< " + line);
- output.println(line);
- output.flush();
- } catch (Exception e) {
- Os.debug("Client: error writing line", e);
- this.running = false;
+ raw("PING " + msg.msg);
}
}
}
import android.os.Messenger;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
.newTabSpec("debug")
.setIndicator("Debug")
.setContent(R.id.debug));
-
- // Start IRC service
- this.startService();
-
} catch (Exception e) {
Os.debug("Error setting content view", e);
return;
Os.debug("Main: onDestroy");
}
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu)
+ {
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.main, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.connect:
+ this.startService();
+ return true;
+ case R.id.disconnect:
+ this.stopService();
+ return true;
+ case R.id.help:
+ Os.debug("Main: Help!");
+ return true;
+ default:
+ return false;
+ }
+ }
+
/* Handler class */
class MainHandler extends Handler
{
package org.pileus.spades;
-import java.io.*;
-import java.net.*;
-
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
private String server = "irc.freenode.net";
private String nickname = "andydroid";
private String channel = "#rhnoise";
- private int port = 6667;
/* Private data */
private Messenger messenger = null;
private Thread thread = null;
- private Socket socket = null;
private Client client = null;
/* Private methods */
- private void setup()
- {
- Os.debug("Task: setup");
- try {
- this.socket = new Socket(server, port);
- this.client = new Client(server, nickname, channel);
- Os.debug("Task: Socket and client created");
- } catch(Exception e) {
- Os.debug("Task: Failed to create socket: " + e);
- return;
- }
-
- try {
- BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintWriter output = new PrintWriter(socket.getOutputStream());
- this.client.connect(input, output);
- Os.debug("Task: Client connected");
- } catch (Exception e) {
- Os.debug("Task: Failed to create readers writers: " + e);
- return;
- }
- }
-
private void command(int cmd, Object value)
{
try {
/* Public methods */
public Message send(String txt)
{
- if (this.client == null && !this.client.running)
+ if (this.client == null)
return null;
return this.client.send(txt);
}
public void run()
{
Os.debug("Task: thread run");
- setup();
- while (client.running)
- this.command(MESSAGE, client.recv());
+
+ if (!client.connect(server, nickname, channel))
+ return;
+
+ while (true) {
+ Message msg = client.recv();
+ if (msg == null)
+ break;
+ this.command(MESSAGE, msg);
+ }
+
+ if (!client.abort())
+ return;
+
Os.debug("Task: thread exit");
}
startForeground(1, note);
/* Start client thread */
+ client = new Client();
thread = new Thread(this);
thread.start();
}
public void onDestroy()
{
Os.debug("Task: onDestroy");
- super.onDestroy();
+ try {
+ this.client.abort();
+ this.thread.join();
+ } catch (Exception e) {
+ Os.debug("Task: error stopping service", e);
+ }
}
@Override