netty参考 android手机客户端_android

 

/*
* Copyright 2009 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* ​​​http://www.apache.org/licenses/LICENSE-2.0​​​ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.lenovo.lsf.push.net.handler;import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.UpstreamMessageEvent;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.util.CharsetUtil;import android.content.Context;
import com.lenovo.lsf.push.log.PushLog;
import com.lenovo.lsf.push.log.PushLog.LEVEL;/**
* @author <a href="​​​http://www.jboss.org/netty/">The​​​ Netty Project</a>
* @author Andy Taylor (​​​andy.taylor@jboss.org​​​)
* @author <a href="​​​http://gleamynode.net/">Trustin​​​ Lee</a>
*
* @version $Rev: 2189 $, $Date: 2010-02-19 18:02:57 +0900 (Fri, 19 Feb 2010) $
*/
public class HttpResponseHandler extends SimpleChannelUpstreamHandler { private boolean readingChunks;
private Context context;
private HttpResponse response; public HttpResponseHandler(Context context) {
this.context = context;
}

   
   
  

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (!readingChunks) {
response = (HttpResponse) e.getMessage();
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","STATUS: " + response.getStatus());
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","VERSION: " + response.getProtocolVersion());
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived",""); if (!response.getHeaderNames().isEmpty()) {
for (String name: response.getHeaderNames()) {
for (String value: response.getHeaders(name)) {
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","HEADER: " + name + " = " + value);
}
}
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","");
} if (response.isChunked()) {
readingChunks = true;
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","CHUNKED CONTENT {");
} else {
ChannelBuffer content = response.getContent();
if (content.readable()) {
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","CONTENT {");
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived",content.toString(CharsetUtil.UTF_8));

/*String result = content.toString(CharsetUtil.UTF_8);
ArrayList<Ticket> tickets = parseTickets(result);
notifyPushApp(context, tickets);
resetUpdateAlarm(context);*/

PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","} END OF CONTENT");
UpstreamMessageEvent event = new UpstreamMessageEvent(ctx.getChannel(), response, ctx.getChannel().getRemoteAddress());
ctx.sendUpstream(event);
}
}
} else {
HttpChunk chunk = (HttpChunk) e.getMessage();
if (chunk.isLast()) {
readingChunks = false;
PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived","} END OF CHUNKED CONTENT");

UpstreamMessageEvent event = new UpstreamMessageEvent(ctx.getChannel(), response, ctx.getChannel().getRemoteAddress());
ctx.sendUpstream(event);

} else { /*String result = chunk.getContent().toString(CharsetUtil.UTF_8);
ArrayList<Ticket> tickets = parseTickets(result);
System.out.println("context is:" + context);
notifyPushApp(context, tickets);
resetUpdateAlarm(context);*/

PushLog.log(context, LEVEL.INFO, "HttpResponseHandler.messageReceived",chunk.getContent().toString(CharsetUtil.UTF_8));
response.removeHeader("Transfer-Encoding");
response.setChunked(false);
response.setContent(ChannelBuffers.copiedBuffer(response.getContent(),chunk.getContent())); }
}
}

}

 

 

package com.lenovo.lsf.push.net.handler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.UpstreamMessageEvent;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Base64;import com.lenovo.lsf.device.R;
import com.lenovo.lsf.push.dao.PushDAOImpl;
import com.lenovo.lsf.push.log.PushLog;
import com.lenovo.lsf.push.log.PushLog.LEVEL;
import com.lenovo.lsf.push.service.PushService;@SuppressLint({ "ParserError", "ParserError", "ParserError", "ParserError" })
public class MessageNotificationHandler extends SimpleChannelUpstreamHandler {
private Context context;
public MessageNotificationHandler(Context context) {
this.context = context;
}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if(e instanceof PollNotificationEvent) {
PollNotificationEvent event = (PollNotificationEvent)e;
ArrayList<PushMessage> pushMsgs = event.getPushMsgs();
dispatchMessages(pushMsgs);

}
}

private void dispatchMessages(ArrayList<PushMessage> messages) {
Iterator<PushMessage> iterator = messages.iterator();
while(iterator.hasNext()) {
PushMessage msg = iterator.next();

if(msg.sid.equals("rsys001")) {
showSystemNotification(msg);
}
else {
notifyPushApp(msg);
}
}

// clear all out-dated messages
messages.clear();
messages = null;
}

private void showSystemNotification(PushMessage msg) {
//PushLog.log(context, "notifyPushApp", "received system notification");
String body = null;
try {
body = new String(Base64.decode(msg.body, Base64.DEFAULT),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(body != null && !body.equals(""))
parseNotificationBodyNode(body,msg);
String messageFBID = msg.messageFBID;
String notificationTitle = msg.notifTitle;
String notificationText = msg.notifContent;
String flag = msg.notifFlag;
boolean visible = msg.notifVisib;
String eventType = msg.eveType;
String action = msg.intentAct;
String intentExtras = msg.intentExt;
String url = msg.url;
String className = msg.className;
String packageName = msg.packName;
String lastVer = msg.lastVer;
int wifiDelay = msg.wifiDelay;

Intent notificationIntent = new Intent();
PendingIntent contentIntent = null;
Notification notification = new Notification(R.drawable.sys_notify,
notificationText, System.currentTimeMillis());
NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int notifID = 0;
if (eventType == null){
return;
} else if (eventType.equals("Notification")) {
notifID = 1;
if (action != null) {
notificationIntent.setAction(action);
}
if(packageName != null && className != null) {
notificationIntent.setClassName(packageName,className);
}
if (intentExtras != null) {
/*
String[] extras = intentExtras.split("&");
if (extras.length > 0) {
for (int index = 0; index < extras.length; index++) {
String name = extras[index].substring(0,
extras[index].indexOf("="));
String value = extras[index].substring(
extras[index].indexOf("=") + 1,
extras[index].length());
if (name != null && value != null)
notificationIntent.putExtra(name, value);
}
}
*/
Uri uri = Uri.parse(intentExtras);
notificationIntent.setData(uri);
}
contentIntent = PendingIntent.getActivity(context,
R.string.app_name, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else if (eventType.equals("HypLink")) {
notifID = 2;
Uri uri = Uri.parse(url);
notificationIntent.setAction("android.intent.action.VIEW");
notificationIntent.setData(uri);
contentIntent = PendingIntent.getActivity(context,
R.string.app_name, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else if (eventType.equals("AppInstall")) {
notifID = 3;
notificationIntent.setAction(PushService.ACTION_INTERNAL_APP_INSTALL);
contentIntent = PendingIntent.getService(context,
R.string.app_name, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else if (eventType.equals("EngUpgrade")) {
notifID = 4;
notificationIntent.setAction(PushService.ACTION_INTERNAL_ENGINE_UPGRADE);
contentIntent = PendingIntent.getService(context,
R.string.app_name, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
if (flag.equals("FLAG_AUTO_CANCEL")) {
notification.flags |= Notification.FLAG_AUTO_CANCEL;
} else if (flag.equals("FLAG_INSISTENT")) {
notification.flags |= Notification.FLAG_NO_CLEAR;
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(context, notificationTitle,
notificationText, contentIntent); if(visible){
manager.notify(notifID , notification);
}else{
if(PushService.ACTION_INTERNAL_APP_INSTALL.equals(notificationIntent.getAction()) || PushService.ACTION_INTERNAL_ENGINE_UPGRADE.equals(notificationIntent.getAction())){
context.startService(notificationIntent);

}else{
context.startActivity(notificationIntent);
} }


}
private void parseNotificationBodyNode(String contentString,PushMessage msg){
InputStream is = null;
try {
is = new ByteArrayInputStream(contentString.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<PushMessage> messages = new ArrayList<PushMessage>(); XmlPullParser parser = null;
int eventType = 0;
try {
XmlPullParserFactory parserFactory = XmlPullParserFactory
.newInstance();
parserFactory.setNamespaceAware(true);
parser = parserFactory.newPullParser();
parser.setInput(is, "UTF-8");
eventType = parser.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (tagName.equals("MessageFBID")) {
try {
msg.messageFBID = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (tagName.equals("NotifTitle")) {
try {
msg.notifTitle = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("NotifContent")) {
try {
msg.notifContent = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("NotifFlag")) {
try {
msg.notifFlag = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("NotifVisib")) {
try {
msg.notifVisib = Boolean.parseBoolean(parser.nextText()
.trim());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("EveType")) {
try {
msg.eveType = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("IntentAct")) {
try {
msg.intentAct = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("IntentExt")) {
try {
msg.intentExt = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("URL")) {
try {
msg.url = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("PackName")) {
try {
msg.packName = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("ClassName")) {
try {
msg.className = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("LastVer")) {
try {
msg.lastVer = parser.nextText().trim();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else if (tagName.equals("WifiDelay")) {
try {
msg.wifiDelay = Integer.parseInt(parser.nextText().trim());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
try {
eventType = parser.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

private void notifyPushApp(PushMessage msg) {
PushDAOImpl dao = new PushDAOImpl(context);
if(dao.isExist(msg.sid)) {
String packageName = dao.getPackageBySID(msg.sid);
String receiver = dao.getReceiverBySID(msg.sid); PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "receiver:"+msg.sid);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "category:"+packageName);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "com.lenovo.lsf.device.permission.MESSAGE"); Intent i = new Intent(msg.sid);
i.putExtra("body", msg.body);
i.addCategory(packageName);
//context.sendBroadcast(i);
context.sendOrderedBroadcast(i, "com.lenovo.lsf.device.permission.MESSAGE");
PushLog.log(context, LEVEL.INFO, "notifyPushApp", "sid:" + msg.sid + ", body:" + msg.body); }
}
}@SuppressLint("ParserError")
class PollNotificationEvent extends UpstreamMessageEvent {

public PollNotificationEvent(Channel channel, Object message,
SocketAddress remoteAddress) {
super(channel, message, remoteAddress);
// TODO Auto-generated constructor stub
} private ArrayList<PushMessage> pushMsgs;

public void setPushMsgs(ArrayList<PushMessage> pushMsgs) {
this.pushMsgs = pushMsgs;
}

@SuppressLint("ParserError")
ArrayList<PushMessage> getPushMsgs() {
return this.pushMsgs;
}
}

 

 

package com.lenovo.lsf.push.net.handler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.UpstreamMessageEvent;
import org.jboss.netty.channel.WriteCompletionEvent;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.AlarmManagerTimerFactory;
import org.jboss.netty.util.CharsetUtil;
import org.jboss.netty.util.Timer;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;import com.lenovo.lsf.push.dao.PushDAOImpl;
import com.lenovo.lsf.push.log.PushLog;
import com.lenovo.lsf.push.log.PushLog.LEVEL;
import com.lenovo.lsf.push.service.PushIntentAware;
import com.lenovo.lsf.push.service.PushMessagePollDelayRetryProxy;
import com.lenovo.lsf.push.service.PushMessagePollImpl;
import com.lenovo.lsf.push.service.PushPollIntervalTunningManager;
import com.lenovo.lsf.push.service.PushService;
import com.lenovo.lsf.push.service.PushTicketImpl;
import com.lenovo.lsf.push.util.PushWakeLock;public class PollHandler extends IdleStateAwareChannelHandler {

private Context context;
private final String SP_NAME = "lsf_sp";
private final String KEY_ACK = "ack"; public PollHandler(Context context) {
// TODO Auto-generated constructor stub
this.context = (PushService) context;
}

@Override
public void channelConnected(
ChannelHandlerContext ctx, ChannelStateEvent e){ // Prepare the HTTP request.
HttpRequest request = getHttpRequest();
// Send the HTTP request.
ctx.getChannel().write(request);

PushLog.log(context, LEVEL.INFO, "PollHandler.channelConnected", "send poll request complete !!!");

}


@Override
public void writeComplete(
ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {

PushLog.log(context, LEVEL.INFO, "PollHandler.writeComplete", "write poll request complete begin to release wake lock!!!");
PushWakeLock.release(context, PushMessagePollImpl.POLL_WAKE_LOCK);

}


@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if(e.getMessage() instanceof HttpResponse){
HttpResponse response = (HttpResponse) e.getMessage();

if(response.getStatus().getCode() != 200) {

if(response.getStatus().getCode() == 401) {
PushTicketImpl.needToUpdateST = true;
}

rePoll(ctx , true); }else{

String content = response.getContent().toString(CharsetUtil.UTF_8);
ArrayList<PushMessage> messages = parseMessage(content);

if(!messages.isEmpty()) {
PollNotificationEvent event = new PollNotificationEvent(ctx.getChannel(), response, ctx.getChannel().getRemoteAddress()); event.setPushMsgs(messages);

ctx.sendUpstream(event);
}else{

PushPollIntervalTunningManager.increasePollKeepAliveInterval(context, ctx);
}



PushMessagePollImpl.pollFailCount = PushMessagePollImpl.INIT_POLL_FAIL_COUNT;
PushMessagePollImpl.setDayPollCount(PushMessagePollImpl.getDayPollCount() + 1);

PushDAOImpl dao = new PushDAOImpl(context);
boolean registrationIsEmpty = dao.isEmpty();


if(PushMessagePollImpl.oneTime == true || registrationIsEmpty){
if(PushMessagePollImpl.oneTime){
PushLog.log(context, LEVEL.INFO, "PollHandler.messageReceived", "onetime mode, begin to close channel !!!");
}

if(registrationIsEmpty){
PushLog.log(context, LEVEL.INFO, "PollHandler.messageReceived", "registration is null,day one poll mode, begin to close channel !!!");
}


AlarmManagerTimerFactory.destroyAlarmManagerTimer(context);

Channel channel = ctx.getChannel();
channel.close(); // Wait for the server to close the connection.
channel.getCloseFuture().awaitUninterruptibly();
//channel.getFactory().releaseExternalResources();
PushMessagePollImpl.isPollRunning = false;

Intent i = new Intent(PushService.ACTION_INTERNAL_STOP_ALL);
//context.sendBroadcast(i);
i = PushIntentAware.awareIntent(context, i);
context.startService(i);
}else{
PushLog.log(context, LEVEL.INFO, "PollHandler.messageReceived", "continue mode, begin to repoll !!!");


ArrayList<String> list = dao.getOutdatedApps(PushTicketImpl.MAX_EXPIRED);
if(list != null && !list.isEmpty()) {
Intent i = new Intent(PushService.ACTION_INTERNAL_UPDATE_PT);
//context.sendBroadcast(i);
i = PushIntentAware.awareIntent(context, i);
context.startService(i);
}

PushWakeLock.acquire(context, PushMessagePollImpl.POLL_WAKE_LOCK);

// Prepare the HTTP request.
HttpRequest request = getHttpRequest();
// Send the HTTP request.
ctx.getChannel().write(request);
}
}

}
}
@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
throws Exception {
PushLog.log(context, LEVEL.INFO, "PollHandler.channelIdle", "IdleStateEvent:" + e.getState().name());

//PushWakeLock.release(context, PushMessagePollImpl.POLL_WAKE_LOCK);

PushPollIntervalTunningManager.decreasePollKeepAliveInterval(context, ctx);

rePoll(ctx, false);
} @Override
public void exceptionCaught(
ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
PushLog.log(context, LEVEL.INFO, "PollHandler.exceptionCaught", "Exception:" + e.getCause().getMessage());

e.getCause().printStackTrace();

rePoll(ctx, true);

}

 

   
 

public HttpRequest getHttpRequest() {

// get response ack
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
String ack = sp.getString(KEY_ACK, "0");
if(ack==null || "".equals(ack)){
ack = "R1:0";
}
String url = PushMessagePollImpl.pollURI+"&min=1800&max="+PushPollIntervalTunningManager.getPollKeepAlive()+"&ack="+ack;

PushLog.log(context, LEVEL.INFO, "PushMessagePollImpl.getHttpRequest", "url is:" + url);


HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET, url);
request.setHeader(HttpHeaders.Names.HOST, PushMessagePollImpl.pollHost);
request.setHeader(HttpHeaders.Names.CONNECTION,
HttpHeaders.Values.KEEP_ALIVE); return request;
} public void rePoll(ChannelHandlerContext ctx, boolean needAddFailCount) {
if(needAddFailCount){
PushMessagePollImpl.pollFailCount = PushMessagePollImpl.pollFailCount + 1;
PushLog.log(context, LEVEL.INFO, "PollHandler.rePoll", "now fail count is "+PushMessagePollImpl.pollFailCount+", max fail count is "+PushMessagePollImpl.MAX_POLL_FAIL_COUNT+" !!!");

}

AlarmManagerTimerFactory.destroyAlarmManagerTimer(context);

Channel channel = ctx.getChannel();
channel.close(); // Wait for the server to close the connection.
channel.getCloseFuture().awaitUninterruptibly();
//channel.getFactory().releaseExternalResources();
PushMessagePollImpl.isPollRunning = false;
if (PushMessagePollImpl.pollFailCount <= PushMessagePollImpl.MAX_POLL_FAIL_COUNT) {
PushLog.log(context, LEVEL.INFO, "PollHandler.rePoll", "poll fail and begin to retry !!!");
/*
Intent i = new Intent(PushInternalReceiver.ACTION_INTERNAL_START_ALL);
context.sendBroadcast(i);
*/
PushMessagePollDelayRetryProxy.setPollRetryAlarm(context); }else{
Intent i = new Intent(PushService.ACTION_INTERNAL_STOP_ALL);
//context.sendBroadcast(i);
i = PushIntentAware.awareIntent(context, i);
context.startService(i);
}
}


public ArrayList<PushMessage> parseMessage(String contentString) { InputStream is;
try {
is = new ByteArrayInputStream(contentString.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
ArrayList<PushMessage> messages = new ArrayList<PushMessage>();

XmlPullParser parser = null;
int eventType = 0;
try {
XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
parser = parserFactory.newPullParser();
parser.setInput(is, "UTF-8");
eventType = parser.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


PushMessage msg = null;
String sid = null;
String body = null;

while(eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if(tagName.equals("RepID")) {
String ack = null;
try {
ack = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} PushLog.log(context, LEVEL.INFO, "PollHandler.parseMessage", "new_ack:"+ack);
SharedPreferences preference = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
preference.edit().putString(KEY_ACK, ack).commit();
}
else if(tagName.equals("Message")) {
msg = new PushMessage();
messages.add(msg);
}
else if(tagName.equals("SID")) {
try {
sid = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msg.sid = sid;
}
else if(tagName.equals("Body")) {
try {
body = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} msg.body = body;
}
}
try {
eventType = parser.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return messages;
}

}class PushMessage {
String sid;
String body;
String messageFBID;
String notifTitle;
String notifContent;
String notifFlag;
boolean notifVisib;
String eveType;
String intentAct;
String intentExt;
String url;
String packName;
String className;
String lastVer;
int wifiDelay;
}

 

 

package com.lenovo.lsf.push.net.handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.WriteCompletionEvent;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.jboss.netty.util.CharsetUtil;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;import android.content.Context;
import android.content.Intent;
import android.util.Log;import com.lenovo.lsf.push.dao.PushDAOImpl;
import com.lenovo.lsf.push.log.PushLog;
import com.lenovo.lsf.push.log.PushLog.LEVEL;
import com.lenovo.lsf.push.service.PushIntentAware;
import com.lenovo.lsf.push.service.PushMessagePollImpl;
import com.lenovo.lsf.push.service.PushService;
import com.lenovo.lsf.push.service.PushTicketImpl;
import com.lenovo.lsf.push.util.PushWakeLock;public class PTHandler extends IdleStateAwareChannelHandler {
private Context context;
private int instance_number; public PTHandler(Context context, int instance_number) {
// TODO Auto-generated constructor stub
this.context = context;
this.instance_number = instance_number;
}


 
 
 

@Override
public void channelConnected(
ChannelHandlerContext ctx, ChannelStateEvent e){ // Prepare the HTTP request.
HttpRequest request = getHttpRequest();
// Send the HTTP request.
ctx.getChannel().write(request);
}


 
 
 

@Override
public void writeComplete(
ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {

PushLog.log(context, LEVEL.INFO, "PTHandler.writeComplete", "write pt request complete begin to release wake lock!!!");
PushWakeLock.release(context, PushTicketImpl.PT_WAKE_LOCK+"-"+instance_number);

}


 
 

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { cancelRequest(ctx);

if(e.getMessage() instanceof HttpResponse){
HttpResponse response = (HttpResponse) e.getMessage();

if(response.getStatus().getCode() == 200) {
String content = response.getContent().toString(CharsetUtil.UTF_8);

if(content != null){
ArrayList<Ticket> tickets = parseTickets(content);
PTNotificationMessageEvent event = new PTNotificationMessageEvent(ctx.getChannel(), e.getMessage(), ctx.getChannel().getRemoteAddress());
event.setTickets(tickets);
ctx.sendUpstream(event); }
}else{ PushLog.log(context, LEVEL.INFO, "PTHandler.messageReceived", "response error code:" + response.getStatus().getCode() +" try to update st .");
PushTicketImpl.needToUpdateST = true; }
}
}


 
 
 

@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
PushLog.log(context, LEVEL.INFO, "PTHandler.channelIdle", "IdleStateEvent:" + e.getState().name()); PushWakeLock.release(context, PushTicketImpl.PT_WAKE_LOCK+"-"+instance_number);

cancelRequest(ctx);

PushWakeLock.release(context, PushTicketImpl.PT_WAKE_LOCK+"-"+instance_number);


}


 
 
 

@Override
public void exceptionCaught(
ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {

PushLog.log(context, LEVEL.INFO, "PTHandler.exceptionCaught", "Exception:" + e.getCause().getMessage());
PushWakeLock.release(context, PushTicketImpl.PT_WAKE_LOCK+"-"+instance_number);

e.getCause().printStackTrace();

cancelRequest(ctx);

    
 
 
 

public void cancelRequest(ChannelHandlerContext ctx){
Channel channel = ctx.getChannel();
channel.close(); // Wait for the server to close the connection.
channel.getCloseFuture().awaitUninterruptibly(); }


 
 
 
 
 

public HttpRequest getHttpRequest() {
// Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, PushTicketImpl.ptURI);
request.setHeader(HttpHeaders.Names.HOST, PushTicketImpl.ptHost);
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
//request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Set some example cookies.
//CookieEncoder httpCookieEncoder = new CookieEncoder(false);
//httpCookieEncoder.addCookie("my-cookie", "foo");
//httpCookieEncoder.addCookie("another-cookie", "bar");
//request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); // Send the HTTP request.
return request; }


 

public ArrayList<Ticket> parseTickets(String contentString) {
InputStream is = new StringBufferInputStream(contentString);
ArrayList<Ticket> tickets = new ArrayList<Ticket>();

Ticket ticket = null;
String sid = null;
String value = null;
String expired = null;
String error = null;

XmlPullParser parser = null;
int eventType = 0;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is,"UTF-8");
eventType = parser.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();

   
       
  
       

while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {


String tagName = parser.getName();
if(tagName.equalsIgnoreCase("Ticket")) {
ticket = new Ticket();
tickets.add(ticket);
}
if(tagName.equalsIgnoreCase("SID")) {
try {
sid = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ticket.sid = sid;
}
if(tagName.equalsIgnoreCase("Expired")) {
try {
expired = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ticket.expired = expired;
}
if(tagName.equalsIgnoreCase("Value")) {
try {
value = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ticket.value = value;
}
if(tagName.equalsIgnoreCase("Error")) {
try {
error = parser.nextText();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ticket.error = error;
}
}
try {
eventType = parser.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return tickets;
} }

 

class Ticket {
String sid;
String expired;
String value;
String error;
}
package com.lenovo.lsf.push.net.handler;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.UpstreamMessageEvent;import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;import com.lenovo.lsf.push.dao.PushDAOImpl;
import com.lenovo.lsf.push.log.PushLog;
import com.lenovo.lsf.push.log.PushLog.LEVEL;
import com.lenovo.lsf.push.service.PushIntentAware;
import com.lenovo.lsf.push.service.PushService;public class PTNotificationHandler extends SimpleChannelUpstreamHandler {

private Context context;
private String sids;
private boolean isRegistration;

public PTNotificationHandler(Context context, String sids, boolean isRegistration) {
this.context = context;
this.sids = sids;
this.isRegistration = isRegistration;
}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if(e instanceof PTNotificationMessageEvent) {
PTNotificationMessageEvent event = (PTNotificationMessageEvent)e;
ArrayList<Ticket> tickets = event.getTickets();
notifyPushApp(event, context, tickets);
//resetUpdateAlarm();
}
}

private void notifyPushApp(PTNotificationMessageEvent event, Context ctx, ArrayList<Ticket> tickets) {
PushDAOImpl dao = new PushDAOImpl(context);
String packageName = null;
String receiver = null;

if(!tickets.isEmpty()) {
Iterator<Ticket> it = tickets.iterator();
while(it.hasNext()) {
Ticket t = (Ticket) it.next(); /*Iterator<RegisterInfo> iter = list.iterator();
while(iter.hasNext()) {
RegisterInfo info = iter.next();
if(info.sid.equals(t.sid)) {
packageName = info.packageName;
receiver = info.receiver;
break;
}
}*/ packageName = dao.getPackageBySID(t.sid);
receiver = dao.getReceiverBySID(t.sid);

if(t.error == null || t.error.equals("")) {
if(t.sid != null && t.expired != null && t.value != null) {
savePT(ctx, t, packageName, receiver, isRegistration); sendSuccessNotificationWithReceiver(ctx, t, packageName, receiver);
}
}
else { sendFailNotificationWithReceiver(ctx, t, packageName, receiver);
}
}

tickets.clear();
}
else { if (sids != null) {
String[] sidArray = sids.split(",");
if (sidArray != null) {
for (int i = 0; i < sidArray.length; i++) { Ticket ticket = new Ticket();
ticket.sid = sidArray[i];
ticket.error = "UNKNOWN";
packageName = dao.getPackageBySID(ticket.sid);
receiver = dao.getReceiverBySID(ticket.sid);

sendFailNotificationWithReceiver(ctx, ticket, packageName, receiver);
}
}
}

} }


 
 

/*
void resetUpdateAlarm() {
PushDAOImpl dao = new PushDAOImpl(context);
long minExpired = dao.getMinExpiration();
if(minExpired != 0) {
Intent intent = new Intent(PushService.ACTION_INTERNAL_UPDATE_PT);
intent = PushIntentAware.awareIntent(context, intent);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, minExpired, pendingIntent);
PushLog.log(context, LEVEL.INFO, "PushTicketImpl.resetUpdateAlarm", "update pt alarm has been set, minExpired: "+minExpired+" ("+(new Date(minExpired)).toLocaleString()+")!!!");
}}
*/


 
 
 

private void savePT(Context ctx, Ticket t, String packageName, String receiver, boolean isRegistration) {
ContentValues cv = new ContentValues();
cv.put(PushDAOImpl.SID, t.sid);
cv.put(PushDAOImpl.PACKAGE, packageName);
cv.put(PushDAOImpl.RECEIVER, receiver);
String expiredStr = t.expired;
cv.put(PushDAOImpl.EXPIRED, expiredStr);
cv.put(PushDAOImpl.PT, t.value);
PushDAOImpl dao = new PushDAOImpl(ctx);
if(!dao.isExist(t.sid)) {
dao.insert(PushDAOImpl.REGISTRY_TABLE, cv);
}
else {
dao.update(PushDAOImpl.REGISTRY_TABLE, cv);
}


  

if(isRegistration){
if(dao.isEmpty()) {
Intent i = new Intent(PushService.ACTION_INTERNAL_STOP_ALL);
//context.sendBroadcast(i);
i = PushIntentAware.awareIntent(context, i);
context.startService(i);
}else{
Intent i = new Intent(PushService.ACTION_INTERNAL_START_ALL);
//context.sendBroadcast(i);
i = PushIntentAware.awareIntent(context, i);
context.startService(i);
}
}
}
private void sendSuccessNotificationWithReceiver(Context ctx, Ticket t, String packageName, String receiver) {
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "receiver:"+receiver);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "category:"+packageName);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "com.lenovo.lsf.device.permission.MESSAGE"); Intent intent = new Intent(receiver);
intent.addCategory(packageName);
intent.putExtra("result", "success");
intent.putExtra("expired", t.expired);
intent.putExtra("push_ticket", t.value);
//ctx.sendBroadcast(intent);
context.sendOrderedBroadcast(intent, "com.lenovo.lsf.device.permission.MESSAGE");


}


 

 

private void sendFailNotificationWithReceiver(Context ctx, Ticket t, String packageName, String receiver) {
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "receiver:"+receiver);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "category:"+packageName);
PushLog.log(context, LEVEL.INFO, "PTNotificationHandler.sendSuccessNotificationWithReceiver", "com.lenovo.lsf.device.permission.MESSAGE"); Intent intent = new Intent(receiver);
intent.addCategory(packageName);
intent.putExtra("result", "fail");
intent.putExtra("error_code", t.error);
//ctx.sendBroadcast(intent);
context.sendOrderedBroadcast(intent, packageName+".permission.MESSAGE");
}


 

 

public static void clearUpdateAlarm(Context ctx) {
//PushAlarmManager.cancelAlarm(ctx, PushReceiver.ACTION_UPDATE_PT);
}}
class PTNotificationMessageEvent extends UpstreamMessageEvent {

public PTNotificationMessageEvent(Channel channel, Object message,
SocketAddress remoteAddress) {
super(channel, message, remoteAddress);
// TODO Auto-generated constructor stub
} private ArrayList<Ticket> tickets;
private String error_content;
public void setTickets(ArrayList<Ticket> tickets) {
this.tickets = tickets;
}

ArrayList<Ticket> getTickets() {
return tickets;
}

public void setErrorConent(String content) {
this.error_content = content;
}

public String getErrorContent() {
return this.error_content;
}

}

 

/*
* Copyright 2009 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* ​​​http://www.apache.org/licenses/LICENSE-2.0​​​ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.lenovo.lsf.push.net.pipeline;import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.http.HttpClientCodec;import com.lenovo.lsf.push.net.handler.HttpResponseHandler;
/**
* @author <a href="​​​http://www.jboss.org/netty/">The​​​ Netty Project</a>
* @author Andy Taylor (​​​andy.taylor@jboss.org​​​)
* @author <a href="​​​http://gleamynode.net/">Trustin​​​ Lee</a>
*
* @version $Rev: 2226 $, $Date: 2010-03-31 11:26:51 +0900 (Wed, 31 Mar 2010) $
*/
public class HttpClientPipelineFactory implements ChannelPipelineFactory { @Override
public ChannelPipeline getPipeline() throws Exception {
// TODO Auto-generated method stub
return null;
}

}

 

 

 

package com.lenovo.lsf.push.net.pipeline;
import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.http.HttpClientCodec;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.AlarmManagerTimer;
import org.jboss.netty.util.AlarmManagerTimerFactory;
import org.jboss.netty.util.Timer;import android.content.Context;
import com.lenovo.lsf.push.net.handler.HttpResponseHandler;
import com.lenovo.lsf.push.net.handler.MessageNotificationHandler;
import com.lenovo.lsf.push.net.handler.PollHandler;
import com.lenovo.lsf.push.service.PushMessagePollImpl;
import com.lenovo.lsf.push.service.PushPollIntervalTunningManager; public class PollPipelineFactory extends HttpClientPipelineFactory {
private Context context;
private Timer timer;
public Timer getTimer() {
return timer;
}
public void setTimer(Timer timer) {
this.timer = timer;
}
public PollPipelineFactory(Context context, Timer timer) {
// TODO Auto-generated constructor stub
this.context = context;
this.timer = timer;
}

 
 

@Override
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline();

pipeline.addLast("poll_timeout", new IdleStateHandler(timer, 0, 0, PushPollIntervalTunningManager.getPollKeepAlive()+PushPollIntervalTunningManager.POLL_KEEP_ALIVE_INTERVAL));

pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("handler", new HttpResponseHandler(context));
pipeline.addLast("poll", new PollHandler(context));

pipeline.addLast("poll_notification", new MessageNotificationHandler(context));

return pipeline;
}}

 

 

package com.lenovo.lsf.push.net.pipeline;
import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.http.HttpClientCodec;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.AlarmManagerTimer;
import org.jboss.netty.util.AlarmManagerTimerFactory;
import org.jboss.netty.util.Timer;import android.content.Context;
import com.lenovo.lsf.push.net.handler.HttpResponseHandler;
import com.lenovo.lsf.push.net.handler.PTHandler;
import com.lenovo.lsf.push.net.handler.PTNotificationHandler;public class PTPipelineFactory extends HttpClientPipelineFactory {
private Context context;
private String sid;
private int instance_number;
private boolean isRegistration; public PTPipelineFactory(Context context,int instance_number, String sid, boolean isRegistration) {
// TODO Auto-generated constructor stub
this.context = context;
this.sid = sid;
this.instance_number = instance_number;
this.isRegistration = isRegistration;
}

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();

pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("handler", new HttpResponseHandler(context));

pipeline.addLast("pt", new PTHandler(context, instance_number));
pipeline.addLast("pt_notification", new PTNotificationHandler(context, sid, isRegistration));
return pipeline;
}}