Created
July 9, 2013 17:26
-
-
Save muromec/5959343 to your computer and use it in GitHub Desktop.
JavaHell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.texr.mobile; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import org.msgpack.MessagePack; | |
import org.msgpack.packer.Packer; | |
import org.msgpack.type.Value; | |
import org.msgpack.type.ArrayValue; | |
import org.msgpack.unpacker.Unpacker; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.LocalSocket; | |
import android.net.LocalSocketAddress; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.util.Log; | |
public class ReIpc { | |
private static ReIpc instance; // one and only | |
private static String sockpath; | |
private static Context ctx; | |
private static final String TAG = "IPC"; | |
private boolean running = false; | |
private Packer packer = null; | |
private | |
InputStream ips; | |
OutputStream ops; | |
public static void setCtx(Context p_ctx) { | |
ctx = p_ctx; | |
setCache(ctx.getCacheDir().getPath()); | |
} | |
public static void setCache(String cachePath) { | |
sockpath = cachePath + "/driver.sock"; | |
} | |
private void waitPacker () { | |
while(packer == null) { | |
try { | |
Thread.sleep(100); | |
} catch(InterruptedException ie) { | |
Log.e(TAG, "interr"); | |
} | |
} | |
} | |
public void setMe(String login) { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(2); | |
packer.write("sip.me"); | |
packer.write(login); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
} | |
public void doLogin(String login, String password) { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(3); | |
packer.write("cert.get"); | |
packer.write(login); | |
packer.write(password); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
} | |
public void setOnline() { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(2); | |
packer.write("sip.online"); | |
packer.write(1); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
} | |
public void startCall(String dest) { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(3); | |
packer.write("sip.call.place"); | |
packer.write(dest); | |
packer.write(dest); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
} | |
public void controlCall(String cid, Integer op) { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(3); | |
packer.write("sip.call.control"); | |
packer.write(cid); | |
packer.write(op); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
} | |
public void fetchHistory(Boolean reset) { | |
waitPacker(); | |
try { | |
packer.writeArrayBegin(2); | |
packer.write("hist.fetch"); | |
packer.write(1); | |
packer.writeArrayEnd(); | |
} catch (IOException e) { | |
Log.e(TAG, "failed cmd "); | |
} | |
Log.v(TAG, "fetch history"); | |
} | |
public void start() { | |
if(running==true) { | |
return; | |
} | |
ReUtils.spawn(sockpath); | |
new Thread(new Runnable() { | |
public void run() { | |
ReIpc.this.loop(); | |
} | |
}).start(); | |
running = true; | |
} | |
public static ReIpc getInstance() { | |
if(instance==null) { | |
instance = new ReIpc(); | |
} | |
return instance; | |
} | |
protected boolean connect() { | |
LocalSocketAddress addr = new LocalSocketAddress(sockpath, LocalSocketAddress.Namespace.FILESYSTEM); | |
while(true) { | |
try { | |
LocalSocket sock = new LocalSocket(); | |
sock.connect(addr); | |
ips = sock.getInputStream(); | |
ops = sock.getOutputStream(); | |
break; | |
} catch (IOException e) { | |
Log.e(TAG, "socket fail "+e); | |
try { | |
Thread.sleep(3000); | |
} catch(InterruptedException ie) { | |
Log.e(TAG, "interr"); | |
} | |
} | |
} | |
return true; | |
} | |
protected void sendIntent(ArrayValue msg) { | |
Value vcmd = msg.get(0); | |
String cmd = vcmd.asRawValue().getString(); | |
Intent intent = new Intent(); | |
intent.setAction("net.texr.mobile."+cmd); | |
if(cmd.equals("cert.ok")) { | |
Integer ok = msg.get(1).asIntegerValue().getInt(); | |
intent.putExtra("ok", ok); | |
if(ok==0) { | |
intent.putExtra("name", msg.get(2).asRawValue().getString()); | |
} | |
Log.v(TAG, "switch to calls " + ok.toString()); | |
ctx.sendBroadcast(intent); | |
return; | |
} | |
if(cmd.equals("sip.reg")) { | |
Integer state = msg.get(1).asIntegerValue().getInt(); | |
intent.putExtra("state", state); | |
ctx.sendBroadcast(intent); | |
return; | |
} | |
if(cmd.equals("sip.call.add")) { | |
String cid = msg.get(1).asRawValue().getString(); | |
Integer cdir = msg.get(2).asIntegerValue().getInt(); | |
Integer cstate = msg.get(3).asIntegerValue().getInt(); | |
Integer ctime = msg.get(4).asIntegerValue().getInt(); | |
String name = msg.get(5).asRawValue().getString(); | |
String login = msg.get(6).asRawValue().getString(); | |
intent.putExtra("cid", cid); | |
intent.putExtra("dir", cdir); | |
intent.putExtra("state", cstate); | |
intent.putExtra("time", ctime); | |
intent.putExtra("name", name); | |
intent.putExtra("login", login); | |
ctx.sendBroadcast(intent); | |
return; | |
} | |
if(cmd.equals("sip.call.del")) { | |
Log.v(TAG, "call end"); | |
String cid = msg.get(1).asRawValue().getString(); | |
Integer result = msg.get(2).asIntegerValue().getInt(); | |
intent.putExtra("cid", cid); | |
intent.putExtra("result", result); | |
ctx.sendBroadcast(intent); | |
return; | |
} | |
if(cmd.equals("hist.res")) { | |
Log.v(TAG, "have hist"); | |
Integer result = msg.get(1).asIntegerValue().getInt(); | |
String idx = msg.get(2).asRawValue().getString(); | |
ArrayValue data = msg.get(3).asArrayValue(); | |
Integer data_cnt = data.size(); | |
ArrayList<HistoryElement> hist_arr = new ArrayList<HistoryElement>(); | |
for(int i = 0; i < data_cnt ; i++) { | |
ArrayValue hist = (ArrayValue)data.get(i); | |
String login = hist.get(3).asRawValue().getString(); | |
Integer event = hist.get(0).asIntegerValue().getInt(); | |
Integer ts = hist.get(1).asIntegerValue().getInt(); | |
HistoryElement hp = new HistoryElement(event, ts, login); | |
hist_arr.add(hp); | |
} | |
intent.putExtra("result", result); | |
intent.putExtra("idx", idx); | |
intent.putParcelableArrayListExtra("hdata", hist_arr); | |
ctx.sendBroadcast(intent); | |
return; | |
} | |
Log.v(TAG, "unhandled " + msg.toString()); | |
} | |
protected void loop () { | |
Log.v(TAG, "start reading socket"); | |
connect(); | |
Log.v(TAG, "connect ok"); | |
MessagePack msgpack = new MessagePack(); | |
Unpacker unpacker = msgpack.createUnpacker(ips); | |
packer = msgpack.createPacker(ops); | |
while (true) { | |
try { | |
Value dst1 = unpacker.read(Value.class); | |
sendIntent((ArrayValue)dst1); | |
} catch (IOException e) { | |
try { | |
Thread.sleep(3000); | |
} catch(InterruptedException ie) { | |
Log.e(TAG, "interr"); | |
} | |
}} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment