diff options
Diffstat (limited to 'client/Piztor/src')
26 files changed, 976 insertions, 339 deletions
diff --git a/client/Piztor/src/com/macaroon/piztor/ActMgr.java b/client/Piztor/src/com/macaroon/piztor/ActMgr.java index f5ff994..903527e 100644 --- a/client/Piztor/src/com/macaroon/piztor/ActMgr.java +++ b/client/Piztor/src/com/macaroon/piztor/ActMgr.java @@ -25,6 +25,7 @@ public class ActMgr { void trigger(int event) { System.out.println(act.id + " : " + event); if (mp.get(nowStatus).containsKey(event)) { + System.out.println("status + " + event); nowStatus.leave(event); nowStatus = mp.get(nowStatus).get(event); nowStatus.enter(event); diff --git a/client/Piztor/src/com/macaroon/piztor/AppMgr.java b/client/Piztor/src/com/macaroon/piztor/AppMgr.java index aed8ca6..790fe4d 100644 --- a/client/Piztor/src/com/macaroon/piztor/AppMgr.java +++ b/client/Piztor/src/com/macaroon/piztor/AppMgr.java @@ -14,10 +14,10 @@ import com.baidu.mapapi.MKGeneralListener; @SuppressLint("UseSparseArrays") public class AppMgr { - + private static final String strKey = "8a0ae50048d103b2b8b12b7066f4ea7d"; - static BMapManager mBMapManager = null; - + static BMapManager mBMapManager = null; + // Status public enum ActivityStatus { create, start, resume, restart, stop, pause, destroy @@ -37,9 +37,9 @@ public class AppMgr { final static int hasToken = 104; final static int toSettings = 105; final static int logout = 106; - + static MapInfo mapInfo; - + static HashMap<Class<?>, HashMap<Integer, Class<?>>> mp; static HashSet<PiztorAct> acts; @@ -76,13 +76,14 @@ public class AppMgr { i.setClass(nowAct, mp.get(nowAct.getClass()).get(event)); if (event == errorToken) Infomation.token = null; - if (event == toSettings) { - if (nowAct.actMgr.nowStatus.getClass() == Main.FetchStatus.class) - i.putExtra("status", true); - else i.putExtra("status", false); + if (event == loginSuccess) { + mBMapManager.start(); + transam.startPush(Infomation.token, Infomation.username); + } + if (event == logout) { + mBMapManager.stop(); + transam.stopPush(); } - if (event == loginSuccess) mBMapManager.start(); - if (event == logout) mBMapManager.stop(); nowAct.startActivity(i); } @@ -115,16 +116,18 @@ public class AppMgr { static void init(Context context) { if (mBMapManager == null) { mBMapManager = new BMapManager(context); - mBMapManager.init(strKey, new MKGeneralListener(){ + mBMapManager.init(strKey, new MKGeneralListener() { + @Override + public void onGetNetworkState(int iError) { + Log.d("Network", "failure"); + System.out.println("network wocao ni ma de !!!!!!!!!"); + } + @Override - public void onGetNetworkState(int iError) { - Log.d("Network","failure"); - } - - @Override - public void onGetPermissionState(int iError) { - Log.d("Permission","wrong key"); - } + public void onGetPermissionState(int iError) { + Log.d("Permission", "wrong key"); + System.out.println("ju ran bu gei wo quan xian !!!!!!!!!!"); + } }); } mp = new HashMap<Class<?>, HashMap<Integer, Class<?>>>(); @@ -138,15 +141,15 @@ public class AppMgr { addStatus(Login.class); addStatus(Main.class); addStatus(Settings.class); - addTransition(Main.class, logout, Login.class); addTransition(InitAct.class, noToken, Login.class); - addTransition(Login.class, loginSuccess, Main.class); - addTransition(Main.class, errorToken, Login.class); - addTransition(Settings.class, errorToken, Login.class); addTransition(InitAct.class, hasToken, Main.class); addTransition(InitAct.class, errorToken, Login.class); + addTransition(Main.class, logout, Login.class); addTransition(Main.class, toSettings, Settings.class); + addTransition(Main.class, errorToken, Login.class); + addTransition(Login.class, loginSuccess, Main.class); addTransition(Settings.class, logout, Login.class); + addTransition(Settings.class, errorToken, Login.class); } } diff --git a/client/Piztor/src/com/macaroon/piztor/Infomation.java b/client/Piztor/src/com/macaroon/piztor/Infomation.java index cd0b49e..603819e 100644 --- a/client/Piztor/src/com/macaroon/piztor/Infomation.java +++ b/client/Piztor/src/com/macaroon/piztor/Infomation.java @@ -1,7 +1,8 @@ package com.macaroon.piztor; public class Infomation { - static String ip = "69.85.86.42"; + static String ip = "192.168.1.171"; +// static String ip = "69.85.86.42"; // static String ip = "192.168.1.101"; static int port = 2222; static String token = null; diff --git a/client/Piztor/src/com/macaroon/piztor/InitAct.java b/client/Piztor/src/com/macaroon/piztor/InitAct.java index 8a7ad7a..8145935 100644 --- a/client/Piztor/src/com/macaroon/piztor/InitAct.java +++ b/client/Piztor/src/com/macaroon/piztor/InitAct.java @@ -4,7 +4,7 @@ import android.os.Bundle; import android.view.Menu; public class InitAct extends PiztorAct { - + @Override protected void onCreate(Bundle savedInstanceState) { id = "initAct"; @@ -13,14 +13,18 @@ public class InitAct extends PiztorAct { AppMgr.transam.setTimeOutTime(5000); setContentView(R.layout.activity_init); } - + @Override protected void onStart() { super.onStart(); + } + + @Override + protected void onResume() { + super.onResume(); if (Infomation.token == null) AppMgr.trigger(AppMgr.noToken); else { - //TODO jump to main AppMgr.trigger(AppMgr.hasToken); } } @@ -28,8 +32,6 @@ public class InitAct extends PiztorAct { @Override protected void onDestroy() { super.onDestroy(); - //TODO 减少频率 - } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Login.java b/client/Piztor/src/com/macaroon/piztor/Login.java index b55cd2e..a1a6d0d 100644 --- a/client/Piztor/src/com/macaroon/piztor/Login.java +++ b/client/Piztor/src/com/macaroon/piztor/Login.java @@ -19,7 +19,7 @@ public class Login extends PiztorAct { int loginButtonClick = 1, retryButtonClick = 2, loginFailed = 3; @SuppressLint("HandlerLeak") - Handler hand = new Handler() { + Handler handler = new Handler() { @Override public void handleMessage(Message m) { System.out.println("receive what : " + m.what); @@ -30,17 +30,20 @@ public class Login extends PiztorAct { } if (m.what == 0) { ResLogin res = (ResLogin) m.obj; - if (res.s == 1) { + if (res.status == 1) { actMgr.trigger(loginFailed); return; } Infomation.token = res.t; Infomation.myInfo.uid = res.uid; Infomation.username = edtUser.getText().toString(); - System.out.println(res.s + " :!!! " + res.t); + System.out.println(res.status + " :!!! " + res.type); actMgr.trigger(AppMgr.loginSuccess); + System.out.println("............"); } else { - actMgr.trigger(loginFailed); + System.out.println("reveive other info ~~~~~~~~~~~~~~" + m.what); +// System.out.println("login de " + m.what); +// actMgr.trigger(loginFailed); } } }; @@ -69,9 +72,13 @@ public class Login extends PiztorAct { @Override void leave(int e) { - Toast toast = Toast.makeText(getApplicationContext(), - "login failed", Toast.LENGTH_LONG); - toast.show(); + if (e == loginFailed) { + Toast toast = Toast.makeText(getApplicationContext(), + "login failed", Toast.LENGTH_LONG); + toast.show(); + } else { + System.out.println("fuck!!!asdfasdfasdf!"); + } } } @@ -81,7 +88,6 @@ public class Login extends PiztorAct { id = "login"; super.onCreate(savedInstanceState); ActStatus[] r = new ActStatus[2]; - AppMgr.transam.setHandler(hand); r[0] = new StartStatus(); r[1] = new LoginStatus(); actMgr = new ActMgr(this, r[0], r); @@ -107,7 +113,7 @@ public class Login extends PiztorAct { @Override protected void onResume() { super.onResume(); - + AppMgr.transam.setHandler(handler); } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/Main.java b/client/Piztor/src/com/macaroon/piztor/Main.java index eeab207..be01c6a 100644 --- a/client/Piztor/src/com/macaroon/piztor/Main.java +++ b/client/Piztor/src/com/macaroon/piztor/Main.java @@ -3,27 +3,21 @@ package com.macaroon.piztor; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; +import java.util.Vector; import android.annotation.SuppressLint; -import android.app.AlertDialog; -import android.app.TimePickerDialog; import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.provider.Settings; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.inputmethod.InputMethodManager; -import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; -import android.widget.TimePicker; import android.widget.Toast; import com.baidu.location.BDLocation; @@ -35,27 +29,23 @@ import com.baidu.mapapi.map.MKMapTouchListener; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.OverlayItem; import com.baidu.mapapi.map.PopupOverlay; +import com.baidu.mapapi.utils.DistanceUtil; import com.baidu.platform.comapi.basestruct.GeoPoint; public class Main extends PiztorAct { - final static int SearchButtonPress = 1; final static int FocuseButtonPress = 3; final static int SuccessFetch = 4; final static int FailedFetch = 5; - final static int Fetch = 6; final static int mapViewtouched = 7; - + + MapMaker mapMaker = null; MapView mMapView; AlertMaker alertMaker; private Calendar calendar; GeoPoint markerPoint = null; - - // map touch listener - private MKMapTouchListener mapTouchListener; - - boolean isFirstLocation = true; + private MKMapTouchListener mapTouchListener; /** * Locating component @@ -65,21 +55,11 @@ public class Main extends PiztorAct { LocationClient mLocClient; LocationData locData = null; public MyLocationListener myListener = new MyLocationListener(); + boolean isFirstLocation = true; ImageButton btnSearch, btnFetch, btnFocus, btnSettings; - // Timer autodate; MapInfo mapInfo; - /* - * @SuppressLint("HandlerLeak") Handler fromGPS = new Handler() { - * - * @Override public void handleMessage(Message m) { if (m.what != 0) { - * Location l = (Location) m.obj; if (l == null) - * System.out.println("fuck!!!"); else { ReqUpdate r = new - * ReqUpdate(Infomation.token, Infomation.username, l.getLatitude(), - * l.getLongitude(), System.currentTimeMillis(), 1000); - * AppMgr.transam.send(r); } } } }; - */ - + @SuppressLint("HandlerLeak") Handler handler = new Handler() { @Override @@ -87,7 +67,7 @@ public class Main extends PiztorAct { switch (m.what) { case 1:// 上传自己信息成功or失败 ResUpdate update = (ResUpdate) m.obj; - if (update.s == 0) + if (update.status == 0) System.out.println("update success"); else { System.out.println("update failed"); @@ -96,32 +76,36 @@ public class Main extends PiztorAct { break; case 2:// 得到别人的信息 ResLocation location = (ResLocation) m.obj; - if (location.s == 0) { + if (location.status == 0) { mapInfo.clear(); - for (Rlocation i : location.l) { - System.out.println(i.i + " : " + i.lat + " " + i.lot); - UserInfo info = new UserInfo(i.i); - info.setLocation(i.lat, i.lot); + for (RLocation i : location.l) { + System.out.println(i.id + " : " + i.latitude + " " + + i.longitude); + UserInfo info = new UserInfo(i.id); + info.setLocation(i.latitude, i.longitude); mapInfo.addUserInfo(info); } - actMgr.trigger(SuccessFetch); +// actMgr.trigger(SuccessFetch); + flushMap(); } else { System.out.println("resquest for location failed!"); actMgr.trigger(AppMgr.errorToken); } break; case 3:// 得到用户信息 - ResUserinfo r = (ResUserinfo) m.obj; - if (r.s == 0) { + ResUserInfo r = (ResUserInfo) m.obj; + if (r.status == 0) { System.out.println("id : " + r.uid + " sex : " + r.sex - + " group : " + r.gid); + + " group : " + r.section); if (r.uid == Infomation.myInfo.uid) { - Infomation.myInfo.gid = r.gid; + System.out.println("flush myself info!!!"); + Infomation.myInfo.section = r.section; + Infomation.myInfo.company = r.company; Infomation.myInfo.sex = r.sex; } else { UserInfo user = mapInfo.getUserInfo(r.uid); if (user != null) - user.setInfo(r.gid, r.sex); + user.setInfo(r.company, r.section, r.sex); else System.out.println("fuck!!!!"); } @@ -133,7 +117,7 @@ public class Main extends PiztorAct { break; case 4:// 登出 ResLogout logout = (ResLogout) m.obj; - if (logout.s == 0) { + if (logout.status == 0) { actMgr.trigger(AppMgr.logout); } else { Toast toast = Toast.makeText(getApplicationContext(), @@ -141,18 +125,49 @@ public class Main extends PiztorAct { toast.show(); } break; + case Transam.StartPush: + ResStartPush startpush = (ResStartPush) m.obj; + if (startpush.status == 1) { + System.out.println("!!!!!!!!!!!!!jian gui le!!!!!!!!!!!!!"); + } + break; + case Transam.PushMessage: + ResPushMessage pushMessage = (ResPushMessage) m.obj; + receiveMessage(pushMessage.message); + break; + case Transam.SendMessage: + ResSendMessage resMessage = (ResSendMessage) m.obj; + System.out.println("res message " + resMessage.status); + break; + case Transam.PushLocation: + ResPushLocation pushLocation = (ResPushLocation) m.obj; + upDateInfo(pushLocation.l); + break; default: break; } } + + void upDateInfo(Vector<RLocation> l) { + // TODO + for (RLocation i : l) { + UserInfo info = AppMgr.mapInfo.getUserInfo(i.id); + if (info != null) { + info.setLocation(i.latitude, i.longitude); + } else { + info = new UserInfo(i.id); + info.setLocation(i.latitude, i.longitude); + AppMgr.mapInfo.addUserInfo(info); + } + } + flushMap(); + } }; String cause(int t) { switch (t) { case SearchButtonPress: return "Search Button Press"; - case Fetch: - return "Fetch "; case FocuseButtonPress: return "Focuse Button Press"; case SuccessFetch: @@ -172,29 +187,44 @@ public class Main extends PiztorAct { System.out .println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); } + + void receiveMessage(String msg) { + System.out.println("receiveed push message!!!!!"); + System.out.println(msg); + Toast toast = Toast.makeText(getApplicationContext(), + msg, Toast.LENGTH_LONG); + toast.show(); + } + public class MyLocationListener implements BDLocationListener { + int cnt = 0; + GeoPoint lastPoint = null; @Override public void onReceiveLocation(BDLocation location) { Log.d("GPS", "Gotten"); + cnt++; if (location == null) { return; } - locData.latitude = location.getLatitude(); locData.longitude = location.getLongitude(); locData.accuracy = location.getRadius(); locData.direction = location.getDerect(); - - System.out.println("LLLLLLLLLLLLLLLLLLLLLLL " + locData.latitude + " " + locData.longitude); - mapMaker.UpdateLocationOverlay(locData, isFirstLocation); - isFirstLocation = false; - if (Infomation.token != null) { - AppMgr.transam.send(new ReqUpdate(Infomation.token, - Infomation.username, locData.latitude, - locData.longitude, System.currentTimeMillis(), 2000)); - Infomation.myInfo.setLocation(locData.latitude, locData.longitude); + + GeoPoint point = new GeoPoint((int)(locData.latitude * 1e6), (int)(locData.longitude * 1e6)); + if (lastPoint == null || cnt > 5 || DistanceUtil.getDistance(point, lastPoint) > 10 ) { + if (Infomation.token != null) { + Infomation.myInfo.setLocation(locData.latitude, + locData.longitude); + AppMgr.transam.send(new ReqUpdate(Infomation.token, + Infomation.username, locData.latitude, + locData.longitude, System.currentTimeMillis(), 2000)); + lastPoint = point; + cnt = 0; + } } + mapMaker.UpdateLocationOverlay(locData, false); } @Override @@ -213,14 +243,9 @@ public class Main extends PiztorAct { if (e == ActMgr.Create) { System.out.println(Infomation.token + " " + Infomation.username + " " + Infomation.myInfo.uid); - AppMgr.transam.send(new ReqUserinfo(Infomation.token, + AppMgr.transam.send(new ReqUserInfo(Infomation.token, Infomation.username, Infomation.myInfo.uid, System .currentTimeMillis(), 5000)); - // TODO flush mapinfo.myinfo - } - - if (e == Fetch) { - requesLocation(Infomation.myInfo.gid); } if (e == SuccessFetch) flushMap(); @@ -233,67 +258,19 @@ public class Main extends PiztorAct { } - class FetchStatus extends ActStatus { - - @Override - void enter(int e) { - System.out.println("enter Fetch status!!!!"); - if (e == Fetch) { - requesLocation(Infomation.myInfo.gid); - } - if (e == SuccessFetch) { - flushMap(); - } - } - @Override - void leave(int e) { - System.out.println("leave fetch status!!!! because" + cause(e)); - } - - } - - class FocusStatus extends ActStatus { - - @Override - void enter(int e) { - // TODO - switch (e) { - case Fetch: - requesLocation(Infomation.myInfo.gid); - break; - case FocuseButtonPress: - mapMaker.UpdateLocationOverlay(locData, true); - break; - case SuccessFetch: - flushMap(); - break; - default: - break; - } - System.out.println("enter focus status!!!!"); - } - - @Override - void leave(int e) { - // TODO leave focus - System.out.println("leave focus status!!!! because" + cause(e)); - } - - } - - void requesLocation(int gid) { + void requesLocation(int company, int section) { ReqLocation r = new ReqLocation(Infomation.token, Infomation.username, - gid, System.currentTimeMillis(), 2000); + company, section, System.currentTimeMillis(), 2000); + AppMgr.transam + .send(new ReqSendMessage(Infomation.token, Infomation.username, + "hahaha", System.currentTimeMillis(), 5000)); System.out.println("get others infomation!!!"); AppMgr.transam.send(r); } - class AutoUpdate extends TimerTask { - @Override - public void run() { - actMgr.trigger(Main.Fetch); - } + void focusOn() { + mapMaker.mMapController.animateTo(Infomation.myInfo.location); } public void InitTouchListenr() { @@ -304,11 +281,6 @@ public class Main extends PiztorAct { public void onMapLongClick(GeoPoint arg0) { closeBoard(Main.this); alertMaker.showMarkerAlert(arg0); - /* - if (mapMaker != null) - mapMaker.DrawMarker(arg0); - Log.d("marker", "draw a new marker"); - */ } @Override @@ -335,24 +307,25 @@ public class Main extends PiztorAct { isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER); mapInfo = AppMgr.mapInfo; - ActStatus[] r = new ActStatus[3]; + ActStatus[] r = new ActStatus[1]; ActStatus startStatus = r[0] = new StartStatus(); - ActStatus fetchStatus = r[1] = new FetchStatus(); - ActStatus focusStatus = r[2] = new FocusStatus(); +// ActStatus fetchStatus = r[1] = new FetchStatus(); +// ActStatus focusStatus = r[2] = new FocusStatus(); AppMgr.transam.setHandler(handler); actMgr = new ActMgr(this, startStatus, r); - actMgr.add(startStatus, FocuseButtonPress, focusStatus); - actMgr.add(startStatus, Fetch, fetchStatus); - actMgr.add(startStatus, SuccessFetch, startStatus); - actMgr.add(startStatus, Fetch, startStatus); - actMgr.add(fetchStatus, Fetch, startStatus); - actMgr.add(fetchStatus, FailedFetch, startStatus); - actMgr.add(fetchStatus, SuccessFetch, startStatus); - actMgr.add(focusStatus, FocuseButtonPress, startStatus); - actMgr.add(focusStatus, mapViewtouched, startStatus); - actMgr.add(focusStatus, SuccessFetch, focusStatus); - actMgr.add(focusStatus, Fetch, focusStatus); +// actMgr.add(startStatus, FocuseButtonPress, focusStatus); +// actMgr.add(startStatus, Fetch, fetchStatus); +// actMgr.add(startStatus, SuccessFetch, startStatus); +// actMgr.add(startStatus, Fetch, startStatus); +// actMgr.add(fetchStatus, Fetch, startStatus); +// actMgr.add(fetchStatus, FailedFetch, startStatus); +// actMgr.add(fetchStatus, SuccessFetch, startStatus); +// actMgr.add(focusStatus, FocuseButtonPress, startStatus); +// actMgr.add(focusStatus, mapViewtouched, startStatus); +// actMgr.add(focusStatus, SuccessFetch, focusStatus); +// actMgr.add(focusStatus, Fetch, focusStatus); setContentView(R.layout.activity_main); + mMapView = (MapView) findViewById(R.id.bmapView); mapMaker = new MapMaker(mMapView, getApplicationContext()); alertMaker = new AlertMaker(Main.this, mapMaker); @@ -371,7 +344,7 @@ public class Main extends PiztorAct { mLocClient.start(); mapMaker.UpdateLocationOverlay(locData, false); } - + public static void closeBoard(Context cc) { InputMethodManager imm = (InputMethodManager) cc .getSystemService(Context.INPUT_METHOD_SERVICE); @@ -379,7 +352,11 @@ public class Main extends PiztorAct { imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); } - + + /* + * public boolean onTap(int index) { OverlayItem item = getItem(index); + * mCurItem = item; if () } + */ @Override protected void onStart() { super.onStart(); @@ -390,15 +367,17 @@ public class Main extends PiztorAct { btnFetch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { - actMgr.trigger(Fetch); + requesLocation(Infomation.myInfo.company, Infomation.myInfo.section); } }); + btnFocus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - actMgr.trigger(FocuseButtonPress); + focusOn(); } }); + btnSettings.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { @@ -409,10 +388,17 @@ public class Main extends PiztorAct { } @Override + protected void onRestart() { + super.onRestart(); + AppMgr.transam.setHandler(handler); + } + + @Override protected void onResume() { isFirstLocation = true; mapMaker.onResume(); flushMap(); + requesLocation(Infomation.myInfo.company, Infomation.myInfo.section); super.onResume(); } @@ -456,7 +442,6 @@ public class Main extends PiztorAct { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); - } @Override diff --git a/client/Piztor/src/com/macaroon/piztor/MapInfo.java b/client/Piztor/src/com/macaroon/piztor/MapInfo.java index f5840c7..52e168b 100644 --- a/client/Piztor/src/com/macaroon/piztor/MapInfo.java +++ b/client/Piztor/src/com/macaroon/piztor/MapInfo.java @@ -53,7 +53,9 @@ public class MapInfo { } class UserInfo { - int uid, gid, sex; + int uid, sex; + int company; //group id + int section; GeoPoint location; UserInfo(int uid) { @@ -64,8 +66,9 @@ class UserInfo { location = new GeoPoint((int)(lat * 1e6), (int)(lot * 1e6)); } - void setInfo(int gid, int sex) { - this.gid = gid; + void setInfo(int company, int section, int sex) { + this.company = company; + this.section = section; this.sex = sex; } diff --git a/client/Piztor/src/com/macaroon/piztor/MapMaker.java b/client/Piztor/src/com/macaroon/piztor/MapMaker.java index 3987cd6..05cf0b9 100644 --- a/client/Piztor/src/com/macaroon/piztor/MapMaker.java +++ b/client/Piztor/src/com/macaroon/piztor/MapMaker.java @@ -61,7 +61,7 @@ public class MapMaker extends Activity { // MapView controlling component private MapView mMapView = null; - private MapController mMapController = null; + MapController mMapController = null; // Default center private final static GeoPoint sjtuCenter = new GeoPoint((int)(31.032247 * 1E6), (int)(121.445937 * 1E6)); diff --git a/client/Piztor/src/com/macaroon/piztor/PushClient.java b/client/Piztor/src/com/macaroon/piztor/PushClient.java new file mode 100644 index 0000000..ea32ad6 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/PushClient.java @@ -0,0 +1,252 @@ +package com.macaroon.piztor;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+import java.util.Vector;
+
+import android.annotation.SuppressLint;
+import android.os.Handler;
+import android.os.Message;
+
+
+
+public class PushClient {
+ static Socket client;
+ static Handler recall;
+
+ static final int ByteLength = 1;
+ static final int IntLength = 4;
+ static final int DoubleLength = 8;
+ static final int TokenLength = 32;
+ static final int FingerPrintLength = 32;
+
+ public final static int StartPush =5;
+
+ public final static int Message = 0;
+ public final static int Location = 1;
+ public final static int PushMessage =100;
+ public final static int PushLocation =101;
+
+ public final static int Reconnect =-2;
+
+ public final static int Failed = 2;
+ public final static int TimeOut = 1;
+ public final static int Success = 0;
+
+ private String LastPrint = "";
+
+
+ public PushClient(String site, int port,int retime) throws UnknownHostException,
+ IOException {
+ try {
+ client = new Socket();
+ client.connect(new InetSocketAddress(site,port), 5000);
+ client.setSoTimeout(5000);
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ throw e;
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ public void setPushHandler(Handler handler) {
+ recall = handler;
+ }
+
+ public int start(ReqStartPush r) throws IOException,SocketTimeoutException {
+ try {
+ DataOutputStream out = new DataOutputStream(client.getOutputStream());
+ DataInputStream in = new DataInputStream(client.getInputStream());
+ int len;
+ len = IntLength+ByteLength+TokenLength+(r.uname).length()+ByteLength;
+ byte[] b = new byte[len];
+ int pos = 0;
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) 5;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(r.token),pos);
+ pos+=TokenLength;
+ write(b,r.uname.getBytes(),pos);
+ pos+=r.uname.length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ out.write(b);
+ out.flush();
+ Message msg = new Message();
+ in.readInt();
+ in.readUnsignedByte();
+ int status = in.readUnsignedByte();
+ ResStartPush rchk = new ResStartPush(status);
+ msg.obj = rchk;
+ msg.what = StartPush;
+ recall.sendMessage(msg);
+ if(status == 1) {
+ return Failed;
+ }
+ return Success;
+ } catch (SocketTimeoutException e){
+ e.printStackTrace();
+ return TimeOut;
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ public boolean isClosed() {
+ return client.isClosed();
+ }
+
+ public void listen(Handler recall,Handler h) throws IOException{
+ client.setSoTimeout(0);
+ DataInputStream in = new DataInputStream(client.getInputStream());
+ DataOutputStream out = new DataOutputStream(client.getOutputStream());
+
+ while(true){
+ try {
+ int len = in.readInt();
+ System.out.println(len);
+ int tmp = in.readUnsignedByte();
+ byte[] buffer = new byte[32];
+ in.read(buffer);
+ String p = byteToHexString(buffer);
+ int outlen;
+ int pos=0;
+ byte[] o = new byte[IntLength+ByteLength+FingerPrintLength];;
+ outlen = IntLength+ByteLength+FingerPrintLength;
+ switch(tmp) {
+ case Message:
+ byte[] b = new byte[len-IntLength-ByteLength-FingerPrintLength-ByteLength];
+ in.read(b);
+ String m = new String(b);
+ in.readUnsignedByte();
+ if(LastPrint != p) {
+ Message msg = new Message();
+ msg.what = PushMessage;
+ msg.obj = new ResPushMessage(m);
+ recall.sendMessage(msg);
+ LastPrint = p;
+ }
+ write(o,intToBytes(outlen),pos); //can be folded!
+ pos+=IntLength;
+ o[pos]=(byte) Message;
+ pos+=ByteLength;
+ write(o,hexStringToBytes(p),pos);
+ pos+=FingerPrintLength;
+ out.write(o);
+ out.flush();
+ break;
+ case Location:
+ len-=(IntLength+ByteLength+FingerPrintLength);
+ int n=0;
+ Vector<RLocation> tmpv = new Vector<RLocation>();
+ while(len > 0) {
+ int tid = in.readInt();
+ double lat = in.readDouble();
+ double lot = in.readDouble();
+ tmpv.add(new RLocation(tid,lat,lot));
+ len -= (IntLength+DoubleLength+DoubleLength);
+ n++;
+ }
+ if(LastPrint != p) {
+ Message msg = new Message();
+ msg.obj = new ResPushLocation(n,tmpv);
+ msg.what = PushLocation;
+ recall.sendMessage(msg);
+ LastPrint = p;
+ }
+ write(o,intToBytes(outlen),pos);
+ pos+=IntLength;
+ o[pos]=(byte) Location;
+ pos+=ByteLength;
+ write(o,hexStringToBytes(p),pos);
+ pos+=FingerPrintLength;
+ out.write(o);
+ out.flush();
+ break;
+ }
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
+ }
+
+ public void closeSocket() throws IOException{
+ try {
+ client.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ @SuppressLint("DefaultLocale")
+ private static byte[] hexStringToBytes(String hexString) {
+ if (hexString == null || hexString.equals("")) {
+ return null;
+ }
+ hexString = hexString.toUpperCase();
+ int length = hexString.length() / 2;
+ char[] hexChars = hexString.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ return d;
+ }
+
+ private static byte charToByte(char c) {
+ return (byte) "0123456789ABCDEF".indexOf(c);
+ }
+
+ private static byte[] intToBytes(int i) {
+ byte[] d = new byte[4];
+ d[0] = (byte)((i >> 24) & 0xFF);
+ d[1] = (byte)((i >> 16) & 0xFF);
+ d[2] = (byte)((i >> 8) & 0xFF);
+ d[3] = (byte)(i & 0xFF);
+ return d;
+ }
+
+ @SuppressLint("UseValueOf")
+ public static byte[] doubleToBytes(double d){
+ byte[] b=new byte[8];
+ long l=Double.doubleToLongBits(d);
+ for(int i=0;i < 8;i++){
+ b[i]=new Long(l).byteValue();
+ l=l>>8;
+ }
+ return b;
+ }
+
+ private static void write(byte[] s,byte[] w,int l) {
+
+ for(int i=0;i<w.length;i++){
+ s[i+l] = w[i];
+ }
+ }
+
+ private static String byteToHexString(byte[] buffer){
+ String p ="";
+ for (int i = 0; i < buffer.length; i++) {
+ String hex = Integer.toHexString(buffer[i] & 0xFF);
+ if (hex.length() == 1) {
+ hex = '0' + hex;
+ }
+ p += hex;
+ }
+ return p;
+ }
+
+}
diff --git a/client/Piztor/src/com/macaroon/piztor/RLocation.java b/client/Piztor/src/com/macaroon/piztor/RLocation.java new file mode 100644 index 0000000..3ba86e5 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/RLocation.java @@ -0,0 +1,13 @@ +package com.macaroon.piztor;
+
+class RLocation{
+ int id; //id
+ double latitude; //latitude
+ double longitude; //longitude
+
+ RLocation(int i,double lat,double lot){
+ id = i;
+ latitude = lat;
+ longitude = lot;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqLocation.java b/client/Piztor/src/com/macaroon/piztor/ReqLocation.java index ee4824c..4ed1ffc 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqLocation.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqLocation.java @@ -5,10 +5,12 @@ package com.macaroon.piztor; //--------------------------------------//
public class ReqLocation extends Req{
- int gid; //group id;
+ int company; //group id;
+ int section;
- ReqLocation(String token,String name,int groupid,long time,long alive){
+ ReqLocation(String token,String name,int com,int sec,long time,long alive){
super(2,token,name,time,alive); //for type 2
- gid = groupid;
+ company = com;
+ section = sec;
}
}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqSendMessage.java b/client/Piztor/src/com/macaroon/piztor/ReqSendMessage.java new file mode 100644 index 0000000..d5bcef4 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ReqSendMessage.java @@ -0,0 +1,14 @@ +package com.macaroon.piztor;
+
+//--------------------------------------//
+// Send Message //
+//--------------------------------------//
+
+public class ReqSendMessage extends Req{
+ String msg;
+
+ ReqSendMessage(String token,String name,String message,long time,long alive){
+ super(6,token,name,time,alive); //for type 6
+ msg = message;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqStartPush.java b/client/Piztor/src/com/macaroon/piztor/ReqStartPush.java new file mode 100644 index 0000000..91d8c0f --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ReqStartPush.java @@ -0,0 +1,12 @@ +package com.macaroon.piztor;
+
+//--------------------------------------//
+// Start push //
+//--------------------------------------//
+
+public class ReqStartPush extends Req{
+
+ ReqStartPush(String token,String name){
+ super(5,token,name,(long) 0,(long) 0); //for type 5
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java b/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java index b7b8fba..234114a 100644 --- a/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java +++ b/client/Piztor/src/com/macaroon/piztor/ReqUpdate.java @@ -5,12 +5,12 @@ package com.macaroon.piztor; //--------------------------------------//
public class ReqUpdate extends Req{
- double lat; //latitude
- double lot; //longitude
+ double latitude; //latitude
+ double longitude; //longitude
- ReqUpdate(String token,String name,double latitude,double longitude,long time,long alive){
+ ReqUpdate(String token,String name,double lat,double lot,long time,long alive){
super(1,token,name,time,alive); //for type 1
- lat = latitude;
- lot = longitude;
+ latitude = lat;
+ longitude = lot;
}
}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ReqUserInfo.java b/client/Piztor/src/com/macaroon/piztor/ReqUserInfo.java new file mode 100644 index 0000000..89c20ee --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ReqUserInfo.java @@ -0,0 +1,15 @@ +package com.macaroon.piztor;
+
+//--------------------------------------//
+// Ask user info //
+//--------------------------------------//
+
+public class ReqUserInfo extends Req{
+ int uid; //user id
+
+
+ ReqUserInfo(String token,String name,int id,long time,long alive){
+ super(3,token,name,time,alive); //for type 3
+ uid = id;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/Res.java b/client/Piztor/src/com/macaroon/piztor/Res.java index 4cc98a9..a225080 100644 --- a/client/Piztor/src/com/macaroon/piztor/Res.java +++ b/client/Piztor/src/com/macaroon/piztor/Res.java @@ -1,11 +1,11 @@ package com.macaroon.piztor;
public class Res{
- int t;
- int s;
- Res(int type,int status){
- t = type;
- s = status;
+ int type;
+ int status;
+ Res(int t,int s){
+ type = t;
+ status = s;
}
}
diff --git a/client/Piztor/src/com/macaroon/piztor/ResLocation.java b/client/Piztor/src/com/macaroon/piztor/ResLocation.java index f6622d5..a4f2622 100644 --- a/client/Piztor/src/com/macaroon/piztor/ResLocation.java +++ b/client/Piztor/src/com/macaroon/piztor/ResLocation.java @@ -7,10 +7,10 @@ import java.util.Vector; //--------------------------------------//
public class ResLocation extends Res{
- Vector<Rlocation> l; //vector for location info
+ Vector<RLocation> l; //vector for location info
int n; //number of location info
- ResLocation(int num,int status,Vector<Rlocation> locationvec){
+ ResLocation(int num,int status,Vector<RLocation> locationvec){
super(2,status); //for type 2
l = locationvec;
n = num;
diff --git a/client/Piztor/src/com/macaroon/piztor/ResPushLocation.java b/client/Piztor/src/com/macaroon/piztor/ResPushLocation.java new file mode 100644 index 0000000..c3e2b64 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResPushLocation.java @@ -0,0 +1,18 @@ +package com.macaroon.piztor;
+
+import java.util.Vector;
+
+//--------------------------------------//
+// Push Location //
+//--------------------------------------//
+
+public class ResPushLocation extends Res{
+ Vector<RLocation> l; //vector for location info
+ int n; //number of location info
+
+ ResPushLocation(int num,Vector<RLocation> locationvec){
+ super(101,0); //for type 101
+ l = locationvec;
+ n = num;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResPushMessage.java b/client/Piztor/src/com/macaroon/piztor/ResPushMessage.java new file mode 100644 index 0000000..ff61ee0 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResPushMessage.java @@ -0,0 +1,15 @@ +package com.macaroon.piztor;
+
+
+//--------------------------------------//
+// Push Message //
+//--------------------------------------//
+
+public class ResPushMessage extends Res{
+ String message;
+
+ ResPushMessage(String s){
+ super(100,0); //for type 100
+ message = s;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResSendMessage.java b/client/Piztor/src/com/macaroon/piztor/ResSendMessage.java new file mode 100644 index 0000000..09f3ffa --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResSendMessage.java @@ -0,0 +1,13 @@ +package com.macaroon.piztor;
+
+
+//--------------------------------------//
+// Respond to send Message //
+//--------------------------------------//
+
+public class ResSendMessage extends Res{
+
+ ResSendMessage(int status){
+ super(6,status); //for type 6
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResStartPush.java b/client/Piztor/src/com/macaroon/piztor/ResStartPush.java new file mode 100644 index 0000000..33343d7 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResStartPush.java @@ -0,0 +1,13 @@ +package com.macaroon.piztor;
+
+
+//--------------------------------------//
+// Respond to start push //
+//--------------------------------------//
+
+public class ResStartPush extends Res{
+
+ ResStartPush(int status){
+ super(5,status); //for type 5
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/ResUserInfo.java b/client/Piztor/src/com/macaroon/piztor/ResUserInfo.java new file mode 100644 index 0000000..2556c68 --- /dev/null +++ b/client/Piztor/src/com/macaroon/piztor/ResUserInfo.java @@ -0,0 +1,22 @@ +package com.macaroon.piztor;
+
+
+//--------------------------------------//
+// Respond to User Info //
+//--------------------------------------//
+
+public class ResUserInfo extends Res{
+ int uid; //userid
+ int company; //group id
+ int section;
+ int sex; //type 0 for female,type 1 for male
+
+
+ ResUserInfo(int status,int u,int com,int sec,int s){
+ super(3,status); //for type 3
+ uid = u;
+ company = com;
+ section = sec;
+ sex =s;
+ }
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/Rlocation.java b/client/Piztor/src/com/macaroon/piztor/Rlocation.java index 8f7d42b..c2d429c 100644 --- a/client/Piztor/src/com/macaroon/piztor/Rlocation.java +++ b/client/Piztor/src/com/macaroon/piztor/Rlocation.java @@ -1,13 +1,13 @@ package com.macaroon.piztor;
class Rlocation{
- int i; //id
- double lat; //latitude
- double lot; //longitude
+ int id; //id
+ double latitude; //latitude
+ double longitude; //longitude
- Rlocation(int id,double latitude,double longitude){
- i = id;
- lat = latitude;
- lot = longitude;
+ Rlocation(int i,double lat,double lot){
+ id = i;
+ latitude = lat;
+ longitude = lot;
}
-}
+}
\ No newline at end of file diff --git a/client/Piztor/src/com/macaroon/piztor/Settings.java b/client/Piztor/src/com/macaroon/piztor/Settings.java index 8ad44b5..e930bf2 100644 --- a/client/Piztor/src/com/macaroon/piztor/Settings.java +++ b/client/Piztor/src/com/macaroon/piztor/Settings.java @@ -14,8 +14,10 @@ public class Settings extends PiztorAct { MapInfo mapInfo; // Event - final static int logoutButtonPressed = 1; - final static int logoutFailed = 2; + final static int logoutButtonPressed = 10; + final static int logoutFailed = 11; + + @SuppressLint("HandlerLeak") Handler handler = new Handler() { @Override @@ -24,7 +26,7 @@ public class Settings extends PiztorAct { switch (m.what) { case 1:// 上传自己信息成功or失败 ResUpdate update = (ResUpdate) m.obj; - if (update.s == 0) + if (update.status == 0) System.out.println("update success"); else { System.out.println("update failed"); @@ -33,12 +35,12 @@ public class Settings extends PiztorAct { break; case 2:// 得到别人的信息 ResLocation location = (ResLocation) m.obj; - if (location.s == 0) { + if (location.status == 0) { mapInfo.clear(); - for (Rlocation i : location.l) { - System.out.println(i.i + " : " + i.lat + " " + i.lot); - UserInfo info = new UserInfo(i.i); - info.setLocation(i.lat, i.lot); + for (RLocation i : location.l) { + System.out.println(i.id + " : " + i.latitude + " " + i.longitude); + UserInfo info = new UserInfo(i.id); + info.setLocation(i.latitude, i.longitude); mapInfo.addUserInfo(info); } } else { @@ -47,12 +49,12 @@ public class Settings extends PiztorAct { } break; case 3:// 得到用户信息 - ResUserinfo r = (ResUserinfo) m.obj; - if (r.s == 0) { + ResUserInfo r = (ResUserInfo) m.obj; + if (r.status == 0) { System.out.println("id : " + r.uid + " sex : " + r.sex - + " group : " + r.gid); + + " group : " + r.section); UserInfo user = mapInfo.getUserInfo(r.uid); - user.setInfo(r.gid, r.sex); + user.setInfo(r.company, r.section, r.sex); } else { System.out.println("reqest for userInfo must be wrong!!!"); actMgr.trigger(AppMgr.errorToken); @@ -60,10 +62,11 @@ public class Settings extends PiztorAct { break; case 4:// 登出 ResLogout logout = (ResLogout) m.obj; - System.out.println("logout status" + logout.s); - if (logout.s == 0) { + System.out.println("logout status" + logout.status); + if (logout.status == 0) { Infomation.token = null; - Infomation.myInfo.gid = -1; + Infomation.myInfo.company = -1; + Infomation.myInfo.section = -1; Infomation.myInfo.uid = -1; Infomation.username = null; actMgr.trigger(AppMgr.logout); @@ -96,6 +99,7 @@ public class Settings extends PiztorAct { @Override void enter(int e) { + System.out.println("!!!!!!!logout info send!!!!!!!!"); AppMgr.transam.send(new ReqLogout(Infomation.token, Infomation.username, System.currentTimeMillis(), 2000)); } diff --git a/client/Piztor/src/com/macaroon/piztor/SocketClient.java b/client/Piztor/src/com/macaroon/piztor/SocketClient.java index ed75e52..622c581 100644 --- a/client/Piztor/src/com/macaroon/piztor/SocketClient.java +++ b/client/Piztor/src/com/macaroon/piztor/SocketClient.java @@ -13,6 +13,8 @@ import android.annotation.SuppressLint; import android.os.Handler;
import android.os.Message;
+
+
public class SocketClient {
static Socket client;
@@ -21,6 +23,16 @@ public class SocketClient { static final int DoubleLength = 8;
static final int TokenLength = 32;
+ public final static int Login =0;
+ public final static int Update =1;
+ public final static int Location =2;
+ public final static int UserInfo =3;
+ public final static int Logout =4;
+ public final static int SendMessage =6;
+
+ public final static int GroupID =0;
+ public final static int Gender =1;
+
public SocketClient(String site, int port, int retime) throws UnknownHostException,
IOException {
try {
@@ -42,74 +54,120 @@ public class SocketClient { client.getOutputStream());
int tmp = req.type;
int len;
+ byte[] b;
+ int pos = 0;
switch (tmp) {
- case 0:
+ case Login:
ReqLogin rau = (ReqLogin) req;
- String id = rau.user;
- String pa = rau.pass;
- len = IntLength+ByteLength+id.length()+ByteLength+pa.length()+ByteLength;
- out.writeInt(len);
- out.writeByte(tmp);
- out.writeBytes(id);
- out.writeByte(0);
- out.writeBytes(pa);
- out.writeByte(0);
+ len = IntLength+ByteLength+rau.user.length()+ByteLength+rau.pass.length()+ByteLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,rau.user.getBytes(),pos);
+ pos+=rau.user.length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ write(b, rau.pass.getBytes(),pos);
+ pos+= rau.pass.length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ out.write(b);
break;
- case 1:
+ case Update:
ReqUpdate rup = (ReqUpdate) req;
- String tk1 = rup.token;
- String name1 = rup.uname;
- len = IntLength+ByteLength+TokenLength+name1.length()+ByteLength+DoubleLength+DoubleLength;
- out.writeInt(len);
- out.writeByte(tmp);
- double slat = rup.lat;
- double slot = rup.lot;
- byte[] b = hexStringToBytes(tk1);
+ len = IntLength+ByteLength+TokenLength+(rup.uname).length()+ByteLength+DoubleLength+DoubleLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(rup.token),pos);
+ pos+=TokenLength;
+ write(b,(rup.uname).getBytes(),pos);
+ pos+=(rup.uname).length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ write(b,doubleToBytes(rup.latitude),pos);
+ pos+=DoubleLength;
+ write(b,doubleToBytes(rup.longitude),pos);
+ pos+=DoubleLength;
out.write(b);
- out.writeBytes(name1);
- out.writeByte(0);
- out.writeDouble(slat);
- out.writeDouble(slot);
break;
- case 2:
+ case Location:
ReqLocation ras = (ReqLocation) req;
- String tk2 = ras.token;
- String name2 = ras.uname;
- len = IntLength+ByteLength+TokenLength+name2.length()+ByteLength+IntLength;
- out.writeInt(len);
- out.writeByte(tmp);
- int gid = ras.gid;
- byte[] b2 = hexStringToBytes(tk2);
- out.write(b2);
- out.writeBytes(name2);
- out.writeByte(0);
- out.writeInt(gid);
+ len = IntLength+ByteLength+TokenLength+ras.uname.length()+ByteLength+ByteLength+ByteLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(ras.token),pos);
+ pos+=TokenLength;
+ write(b,(ras.uname).getBytes(),pos);
+ pos+=(ras.uname).length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ b[pos] = (byte) ras.company;
+ pos+=ByteLength;
+ b[pos] = (byte) ras.section;
+ pos+=ByteLength;
+ out.write(b);
break;
- case 3:
- ReqUserinfo rus = (ReqUserinfo) req;
- String tk3 = rus.token;
- String name3 = rus.uname;
- len = IntLength+ByteLength+TokenLength+name3.length()+ByteLength+IntLength;
- out.writeInt(len);
- out.writeByte(tmp);
- int usid = rus.uid;
- byte[] b3 = hexStringToBytes(tk3);
- out.write(b3);
- out.writeBytes(name3);
- out.writeByte(0);
- out.writeInt(usid);
+ case UserInfo:
+ ReqUserInfo rus = (ReqUserInfo) req;
+ len = IntLength+ByteLength+TokenLength+rus.uname.length()+ByteLength+IntLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(rus.token),pos);
+ pos+=TokenLength;
+ write(b,(rus.uname).getBytes(),pos);
+ pos+=(rus.uname).length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ write(b,intToBytes(rus.uid),pos);
+ pos+=IntLength;
+ out.write(b);
break;
- case 4:
+ case Logout:
ReqLogout rlo = (ReqLogout) req;
- String tk4 = rlo.token;
- String name4 = rlo.uname;
- len = IntLength+ByteLength+TokenLength+name4.length()+ByteLength;
- out.writeInt(len);
- out.writeByte(tmp);
- byte[] b4 = hexStringToBytes(tk4);
- out.write(b4);
- out.writeBytes(name4);
- out.writeByte(0);
+ len = IntLength+ByteLength+TokenLength+rlo.uname.length()+ByteLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(rlo.token),pos);
+ pos+=TokenLength;
+ write(b,(rlo.uname).getBytes(),pos);
+ pos+=(rlo.uname).length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ out.write(b);
+ break;
+ case SendMessage:
+ ReqSendMessage rem = (ReqSendMessage) req;
+ len = IntLength+ByteLength+TokenLength+rem.uname.length()+ByteLength+rem.msg.length()+ByteLength;
+ b = new byte[len];
+ write(b,intToBytes(len),pos);
+ pos+=IntLength;
+ b[pos] = (byte) tmp;
+ pos+=ByteLength;
+ write(b,hexStringToBytes(rem.token),pos);
+ pos+=TokenLength;
+ write(b,(rem.uname).getBytes(),pos);
+ pos+=(rem.uname).length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ write(b,rem.msg.getBytes(),pos);
+ pos+=rem.msg.length();
+ b[pos] = 0;
+ pos+=ByteLength;
+ out.write(b);
break;
}
out.flush();
@@ -117,9 +175,9 @@ public class SocketClient { Message msg = new Message();
int outlen = in.readInt();
int type = in.readUnsignedByte();
+ int status = in.readUnsignedByte();
switch (type) {
- case 0:
- int status = in.readUnsignedByte();
+ case Login:
int id = in.readInt();
byte[] buffer = new byte[32];
in.read(buffer);
@@ -133,64 +191,62 @@ public class SocketClient { }
ResLogin rchklogin = new ResLogin(id,tk,status);
msg.obj = rchklogin;
- msg.what = 0;
+ msg.what = Login;
recall.sendMessage(msg);
break;
- case 1:
- int status1 = in.readUnsignedByte();
- ResUpdate rchkupd = new ResUpdate(status1);
- msg.obj = rchkupd;
- msg.what = 1;
+ case Update:
+ msg.obj = new ResUpdate(status);
+ msg.what = Update;
recall.sendMessage(msg);
break;
- case 2:
- int status2 = in.readUnsignedByte();
+ case Location:
int n = 0;
outlen-=(IntLength+ByteLength+ByteLength);
- Vector<Rlocation> tmpv = new Vector<Rlocation>();
+ Vector<RLocation> tmpv = new Vector<RLocation>();
while(outlen > 0) {
int tid = in.readInt();
double lat = in.readDouble();
double lot = in.readDouble();
- tmpv.add(new Rlocation(tid,lat,lot));
+ tmpv.add(new RLocation(tid,lat,lot));
outlen -= (IntLength+DoubleLength+DoubleLength);
n++;
}
- ResLocation rlocin = new ResLocation(n,status2,tmpv);
- msg.obj = rlocin;
- msg.what = 2;
+ msg.obj = new ResLocation(n,status,tmpv);
+ msg.what = Location;
recall.sendMessage(msg);
break;
- case 3:
- int status3 = in.readUnsignedByte();
+ case UserInfo:
outlen-=(IntLength+ByteLength+ByteLength);
- ReqUserinfo rus = (ReqUserinfo) req;
+ ReqUserInfo rus = (ReqUserInfo) req;
int u = rus.uid;
- int g = 0,s = 0;
+ int com = 0,sec = 0,s = 0;
while(outlen > 0) {
int typ = in.readUnsignedByte();
outlen-=ByteLength;
switch(typ){
- case 0:
- g = in.readInt();
- outlen-=IntLength;
+ case GroupID:
+ com = in.readUnsignedByte();
+ sec = in.readUnsignedByte();
+ outlen-=(ByteLength+ByteLength);
break;
- case 1:
- s = in.readByte();
+ case Gender:
+ s = in.readUnsignedByte();
outlen-=ByteLength;
break;
}
}
- ResUserinfo resus = new ResUserinfo(status3,u,g,s);
- msg.obj = resus;
- msg.what = 3;
+ msg.obj = new ResUserInfo(status,u,com,sec,s);
+ msg.what = UserInfo;
recall.sendMessage(msg);
break;
- case 4:
- int status4 = in.readUnsignedByte();
- ResLogout rlogout = new ResLogout(status4);
- msg.obj = rlogout;
- msg.what = 4;
+ case Logout:
+ msg.obj = new ResLogout(status);
+ msg.what = Logout;
+ recall.sendMessage(msg);
+ break;
+ case SendMessage:
+ msg.obj = new ResSendMessage(status);
+ msg.what = SendMessage;
recall.sendMessage(msg);
break;
}
@@ -231,5 +287,30 @@ public class SocketClient { private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
+
+ private static byte[] intToBytes(int i) {
+ byte[] d = new byte[4];
+ d[0] = (byte)((i >> 24) & 0xFF);
+ d[1] = (byte)((i >> 16) & 0xFF);
+ d[2] = (byte)((i >> 8) & 0xFF);
+ d[3] = (byte)(i & 0xFF);
+ return d;
+ }
+
+ public static byte[] doubleToBytes(double d){
+ byte[] b=new byte[8];
+ long l=Double.doubleToLongBits(d);
+ for(int i=0;i<8;i++){
+ b[i] = (byte)(l >>> 8*(7-i));
+ }
+ return b;
+ }
+
+ private static void write(byte[] s,byte[] w,int l) {
+
+ for(int i=0;i<w.length;i++){
+ s[i+l] = w[i];
+ }
+ }
}
diff --git a/client/Piztor/src/com/macaroon/piztor/Transam.java b/client/Piztor/src/com/macaroon/piztor/Transam.java index f77df39..7902fde 100644 --- a/client/Piztor/src/com/macaroon/piztor/Transam.java +++ b/client/Piztor/src/com/macaroon/piztor/Transam.java @@ -20,19 +20,25 @@ import android.os.Message; // 2 for locationRequest //
// 3 for userinfo //
// 4 for logout //
+// 5 for requestpush //
+// 6 for sendmessage //
+// //
+// 100 for pushmessage //
// //
// ----------I'm the division line-------- //
// //
// -1 for Exceptions //
+// Exception (req type , exception type) //
// //
// ----------I'm the division line-------- //
// //
// *Request form* //
// login -- username & password //
//update -- token & username & latitude & longitude//
-// getlocation -- token & username & groupid //
+//getlocation -- token & username & company & section//
// getuserinfo -- token & userinfo & userid //
// logout -- token & username //
+// send message -- token & username & message //
// //
// ----------I'm the division line-------- //
// //
@@ -42,21 +48,50 @@ import android.os.Message; // getlocation -- status & entrynumber & data //
// entry -- userid & latitude & longitude //
// //
-// getuserinfo -- status & uid & gid & gender //
-// logout -- status //
+//getuserinfo -- status & uid & company & section & gender//
+// logout -- status //
+// request push -- status //
+// send message -- status //
// //
// status -- 0 for success //
// 1 for failed/invalid //
// //
+// push message -- message //
+// //
//------------------------------------------------//
+
+
public class Transam implements Runnable {
+
+ public final static int Login =0;
+ public final static int Update =1;
+ public final static int Location =2;
+ public final static int UserInfo =3;
+ public final static int Logout =4;
+ public final static int StartPush =5;
+ public final static int SendMessage =6;
+
+ public final static int PushMessage =100;
+ public final static int PushLocation =101;
+
+ public final static int GroupID =0;
+ public final static int Gender =1;
+
+ public final static int EConnectedFailedException =101;
+ public final static int ETimeOutException =102;
+ public final static int EJavaHostException =103;
+ public final static int EPushFailedException =104;
+ public final static int EIOException =105;
+ public final static int EUnknownHostException =106;
+
public Timer timer;
public boolean running = false;
public boolean flag = true;
public int cnt = 4; //retry times
public int tcnt; //current remain retry times
- public int retime = 10000; //timeout time
+ public int rcnt; //current remain retry times (push)
+ public int retime = 2000; //timeout time
Res res;
Req req;
public int p; //port
@@ -65,6 +100,17 @@ public class Transam implements Runnable { Handler core;
Handler recall; //recall
Queue<Req> reqtask ; //request task
+
+ public String itoken;
+ public String iname;
+
+ Thread Pushthread;
+ PushClient push;
+
+
+ public final static int Reconnect =-2;
+ public final static int Exception =-1;
+ public final static int TimeOut =0;
Transam(String ip, int port,Handler Recall) {
p = port;
@@ -78,6 +124,29 @@ public class Transam implements Runnable { }
+ public void startPush(String token,String name) {
+ itoken = token;
+ iname = name;
+ rcnt = cnt;
+ connectpush();
+ }
+
+ public void stopPush() {
+ try{
+ if(push.isClosed() == false) {
+ push.closeSocket();
+ itoken = null;
+ iname = null;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Exception;
+ msg.obj = new EIOException(5,0);
+ recall.sendMessage(msg);
+ }
+ }
+
public void setTimeOutTime(int msec){
retime = msec;
}
@@ -89,8 +158,13 @@ public class Transam implements Runnable { public void setHandler(Handler Recall){
recall = Recall;
+ if(push != null) {
+ push.setPushHandler(Recall);
+ }
reqtask.clear();
}
+
+
public void run() { //start the main thread
while(true){
@@ -100,9 +174,9 @@ public class Transam implements Runnable { req = reqtask.poll();
if(req.time + req.alive < System.currentTimeMillis()){ //time out!
Message ret = new Message();
- TimeOutException t = new TimeOutException();
+ ETimeOutException t = new ETimeOutException(req.type,req.time);
ret.obj = t;
- ret.what = -1;
+ ret.what = Exception;
recall.sendMessage(ret);
}
else{ //run the request
@@ -120,6 +194,49 @@ public class Transam implements Runnable { thread = new Thread(t);
thread.start();
}
+
+ private void connectpush() {
+ reqpush r = new reqpush();
+ Pushthread = new Thread(r);
+ Pushthread.start();
+ }
+
+ class reqpush implements Runnable {
+ public void run() {
+ try {
+ if(itoken == null || iname == null) return;
+ push = new PushClient(i,p,retime);
+ push.setPushHandler(recall);
+ int out = push.start(new ReqStartPush(itoken,iname));
+ if(out == 1) {
+ push.closeSocket();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new ETimeOutException(5,0);
+ handler.sendMessage(msg);
+ }
+ else if (out == 2){
+ stopPush();
+ }
+ else {
+ push.listen(recall,handler);
+ }
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new EUnknownHostException(5,0);
+ handler.sendMessage(msg);
+ } catch (IOException e) {
+ e.printStackTrace();
+ Message msg = new Message();
+ msg.what = Reconnect;
+ msg.obj = new EIOException(5,0);
+ handler.sendMessage(msg);
+ }
+
+ }
+ }
class thd implements Runnable {
public void run() {
@@ -132,21 +249,23 @@ public class Transam implements Runnable { }
else {
client.closeSocket();
- Message msg = new Message();
- msg.what = 0;
- handler.sendMessage(msg);
+ Message m = new Message();
+ EConnectFailedException c = new EConnectFailedException(req.type,req.time);
+ m.obj = c;
+ m.what = Exception;
+ handler.sendMessage(m);
}
} catch (UnknownHostException e) {
e.printStackTrace();
Message msg = new Message();
- msg.what = -1;
- msg.obj = e;
+ msg.what = Exception;
+ msg.obj = new EUnknownHostException(req.type,req.time);
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
Message msg = new Message();
- msg.what = -1;
- msg.obj = e;
+ msg.what = Exception;
+ msg.obj = new EIOException(req.type,req.time);
handler.sendMessage(msg);
}
@@ -157,7 +276,7 @@ public class Transam implements Runnable { Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
- case -1:
+ case Exception:
if (tcnt > 0) {
tcnt--;
System.out.println(tcnt);
@@ -165,50 +284,93 @@ public class Transam implements Runnable { } else if (tcnt == 0) {
Message m = new Message();
m.obj = msg.obj;
- m.what = -1;
+ m.what = Exception;
recall.sendMessage(m);
running = false;
}
break;
- case 0:
+ case TimeOut:
if (tcnt > 0) {
tcnt--;
connect();
} else if (tcnt == 0) {
Message m = new Message();
- ConnectFailedException c = new ConnectFailedException();
+ EConnectFailedException c = new EConnectFailedException(req.type,req.time);
m.obj = c;
- m.what = -1;
+ m.what = Exception;
recall.sendMessage(m);
running = false;
}
break;
+ case Reconnect:
+ if (rcnt > 0) {
+ rcnt--;
+ connectpush();
+ } else if (rcnt == 0) {
+ Message m = new Message();
+ //EPushFailedException c = new EPushFailedException(req.type);
+ m.obj = msg.obj;
+ m.what = Exception;
+ recall.sendMessage(m);
+ }
+ break;
}
super.handleMessage(msg);
}
};
- class ConnectFailedException extends Exception{
+ class EException extends Exception {
+ private static final long serialVersionUID = 100L;
+ int Rtype;
+ int Etype;
+ long time;
+ public EException(int e,int r,long timep) {
+ super();
+ Rtype = r;
+ Etype = e;
+ time = timep;
+ }
+ }
+
+ class EConnectFailedException extends EException{
private static final long serialVersionUID = 101L;
- public ConnectFailedException() {
- super();
+ public EConnectFailedException(int t,long timep) {
+ super(101,t,timep);
}
}
- class TimeOutException extends Exception{
+ class ETimeOutException extends EException{
private static final long serialVersionUID = 102L;
- public TimeOutException() {
- super();
- }
-
+ public ETimeOutException(int t,long timep) {
+ super(102,t,timep);
+ }
}
- class JavaHostException extends Exception{
+ class EJavaHostException extends EException{
private static final long serialVersionUID = 103L;
- public JavaHostException() {
- super();
- }
-
+ public EJavaHostException(int t,long timep) {
+ super(103,t,timep);
+ }
}
+ class EPushFailedException extends EException{
+ private static final long serialVersionUID = 104L;
+ public EPushFailedException(int t,long timep) {
+ super(104,t,timep);
+ }
+ }
+
+ class EIOException extends EException{
+ private static final long serialVersionUID = 105L;
+ public EIOException(int t,long timep) {
+ super(105,t,timep);
+ }
+ }
+
+ class EUnknownHostException extends EException{
+ private static final long serialVersionUID = 106L;
+ public EUnknownHostException(int t,long timep) {
+ super(106,t,timep);
+ }
+ }
}
\ No newline at end of file |