android push server & mqtt

41
Android Push Server & MQTT [email protected] 이광운 13128월요일

Upload: -

Post on 18-Jan-2015

27.830 views

Category:

Technology


0 download

DESCRIPTION

파이썬 1월 세미나 Android Push Server & MQTT

TRANSCRIPT

Page 2: Android Push Server & MQTT

● Google - GCM (Google Cloud Message), C2DM

● Apple - APNS (Apple Push Notification Service)

● MS - MPNS (Microsoft Push Notification Service)

● SKT - Smart Push(AlwaysOn Management framework)

● 기타...

Mobile Push Notification Service

13년 1월 28일 월요일

Page 3: Android Push Server & MQTT

● GCM (Google Cloud Message)

- Getting Started :

http://developer.android.com/google/gcm/gs.html

● Private Push Service

- 각 서비스/업체별로 구현한 Push Service

Android Push Notification Service

13년 1월 28일 월요일

Page 4: Android Push Server & MQTT

Naver Npush

SKT AOM

Kakatok

Facebook Messenger

Viber

NateOn

기타

이미지 출처 : http://helloworld.naver.com/helloworld/1846

13년 1월 28일 월요일

Page 5: Android Push Server & MQTT

● C2DM시절 Push의 낮은 신뢰도 (속도, 도달률)

● Google의 전송량 제한

● Android 2.2 이상 ( Froyo )

● 구글 계정으로 로그인해야 사용 가능

● 자체 Customizable Business Rules 구현 필요

Why Private Push Service ?

13년 1월 28일 월요일

Page 6: Android Push Server & MQTT

● Push - 지속적인 연결 유지

Server -> Client

● Pull - 지속적인 Polling으로 데이타 갱신 ( 카카오톡)

Client -> Server

● 차이점

- 정보의 주도권

- 배터리, 패킷 소모량

PUSH 구현 방식 - Push & Pull

13년 1월 28일 월요일

Page 7: Android Push Server & MQTT

● MQTT

● XMPP

● HTTP

● Custom Protocol

Push Service 구현 = Server <- (protocol) -> Client

MQTT = MQ Telemetry TransportXMPP = Extensible Messaging and Presence ProtocolHTTP = HyperText Transfer Protocol

13년 1월 28일 월요일

Page 8: Android Push Server & MQTT

* XML 기반

* 원래 이름은 Jabber

* Jabber 오픈소스 커뮤니티 1999년

* 국제 인터넷 표준화 기구 - 개방형 표준

* IM, 채팅

* Google Talk, Google App Engine 제공

* BOSH : JavaScript, Firewall

XMPP (5222)

BOSH : Bidirectional-streams Over Synchronous HTTP

13년 1월 28일 월요일

Page 9: Android Push Server & MQTT

XMPP 메시지 형태

<message from="sendinguser@somedomain" to="recipient@somedomain" xml:lang='en'>

<body> Body of message </body></message>

<stream:stream from="[server]" id="[unique ID over conversation]" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">

Stream

Message

http://www.ibm.com/developerworks/kr/xml/tutorials/x-realtimeXMPPtut/section3.html

13년 1월 28일 월요일

Page 10: Android Push Server & MQTT

* IBM과 Eurotech(Arcom)에 의해 1999년 최초 개발

* 센서, 원격 검침 영역 , 모바일기기, 아두이노

Low bandwidth, High latency, Unreliable, High cost

* Clinet lib: C 버전 30Kb, Java 버전 100Kb 내외

* Publish/Subscribe, QoS 제공 (0, 1, 2)

* Openly published with a royalty-free license

* minimal 2byte, 가변길이

MQTT (1883)

13년 1월 28일 월요일

Page 11: Android Push Server & MQTT

MQTT 메시지 형태

MQTT Spec v3.1 : http://www.ibm.com/developerworks/webservices/library/ws-mqtt/index.htmlhttp://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/MQTT_V3.1_Protocol_Specific.pdf

Reserved - ReservedCONNECT - Client request to connect to ServerCONNACK - Connect AcknowledgmentPUBLISH - Publish messagePUBACK - Publish AcknowledgmentPUBREC - Publish Received (assured delivery part 1)PUBREL - Publish Release (assured delivery part 2)PUBCOMP - Publish Complete (assured delivery part 3)SUBSCRIBE - Client Subscribe requestSUBACK - Subscribe AcknowledgmentUNSUBSCRIBE - Client Unsubscribe requestUNSUBACK - Unsubscribe AcknowledgmentPINGREQ - PING RequestPINGRESP - PING ResponseDISCONNECT - Client is DisconnectingReserved - Reserved

13년 1월 28일 월요일

Page 12: Android Push Server & MQTT

http://stephendnicholas.com/archives/1217

13년 1월 28일 월요일

Page 14: Android Push Server & MQTT

MQTT

13년 1월 28일 월요일

Page 15: Android Push Server & MQTT

* 서버 : RSMB, Mosquitto, etc...

* 클라이언트 : Console, Web, Android Emul & Phone

Private Push Service POC

13년 1월 28일 월요일

Page 16: Android Push Server & MQTT

* IBM RSMB (Really Small Message Broker)* Mosquitto (test.mosquitto.org)* IBM WebSphere MQ Telemetry* MQTT.js (Node.js)* eMQTT (Erlang)* RabbitMQ* Apache ActiveMQ* Apache Apollo* Moquette (Apache MINA)

MQTT Server - OpenSource

http://mqtt.org/software

13년 1월 28일 월요일

Page 17: Android Push Server & MQTT

* Device-specific* ActionScript* C / C++* C#* Dephi* Erlang* Java* JavaScript / Node.js* .NET* Object-c* Perl* PHP* Python* REXX* Ruby

MQTT Client - OpenSource

http://mqtt.org/software

13년 1월 28일 월요일

Page 18: Android Push Server & MQTT

출처 : http://mqtt.org/wiki/doku.php/server_support

Private Push Server 선택

13년 1월 28일 월요일

Page 19: Android Push Server & MQTT

Mosquitto Sample

13년 1월 28일 월요일

Page 20: Android Push Server & MQTT

솔루션을 만들어팔 수 있을까?

13년 1월 28일 월요일

Page 21: Android Push Server & MQTT

* Console & Mosquitto Command

* Java Client Library * Mosquitto Java Client * Akka + IBM Paho Client

* Python Client Libarry * python-mosquitto * MQTT-for-Twisted-Python * nyamuk

Mosquitto - 1차 테스트

Akka ( http://akka.io )

- Build powerful concurrent & distributed applications more easily. on the JVM

OK !

13년 1월 28일 월요일

Page 22: Android Push Server & MQTT

* 서버 1대 * CentOS 5.5 * Mosquitto 1 프로세스

* 클라이언트 * 맥북 프로 2대 + iMac 1대 * 각 6500개 Python Client * ipTime 공유기 n700

Mosquitto - 2차 성능 테스트

mosquitto broker

공유기

6500

6500

6500

console

13년 1월 28일 월요일

Page 23: Android Push Server & MQTT

- 약 2만개 Python MQTT Client 안정적 접속 (2.5일간 유지)- CPU : 단일 코어 사용- 메모리는 Client 수와 상관없이 일정하게 유지- 연결 유지를 위한 Ping Req/Res의 트래픽 : 별도 전용선- Publish Time 5초 ~ 6초 ( 이유는 ipTime 공유기의 한계 - 속도 지연)

Mosquitto - 2차 결과

OK !13년 1월 28일 월요일

Page 24: Android Push Server & MQTT

알게 된 것 1 : 별도의 전용 Line을 통한 서비스알게 된 것 2 : Default FD <= 1024

13년 1월 28일 월요일

Page 25: Android Push Server & MQTT

https://bitbucket.org/oojah/mosquitto/src/tip/lib/python/sub.py

import mosquitto

def on_connect(mosq, obj, rc): print("rc: "+str(rc))

def on_message(mosq, obj, msg): print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

def on_publish(mosq, obj, mid): print("mid: "+str(mid))

def on_subscribe(mosq, obj, mid, granted_qos): print("Subscribed: "+str(mid)+" "+str(granted_qos))

def on_log(mosq, obj, level, string): print(string)

# If you want to use a specific client id, use# mqttc = mosquitto.Mosquitto("client-id")# but note that the client id must be unique on the broker. Leaving the client# id parameter empty will generate a random id for you.mqttc = mosquitto.Mosquitto()mqttc.on_message = on_messagemqttc.on_connect = on_connectmqttc.on_publish = on_publishmqttc.on_subscribe = on_subscribe# Uncomment to enable debug messages#mqttc.on_log = on_logmqttc.connect("test.mosquitto.org", 1883, 60)mqttc.subscribe("$SYS/#", 0)

rc = 0while rc == 0: rc = mqttc.loop()

print("rc: "+str(rc))

13년 1월 28일 월요일

Page 26: Android Push Server & MQTT

filedescriptor out of range in select()

Error

Default 1024

13년 1월 28일 월요일

Page 27: Android Push Server & MQTT

Mosquitto Python MQTT Client

select? mysql select?

13년 1월 28일 월요일

Page 28: Android Push Server & MQTT

* select - The first three arguments are sequences of ‘waitable objects’: either integers representing file descriptors* kqueue - kernel event notification mechanism* kevent - Returns a kernel event object* poll - better scalability for network servers that service many, many clients at the same time* epoll - Returns an edge polling object, which can be used as Edge or Level Triggered interface for I/O events

Python IO

13년 1월 28일 월요일

Page 29: Android Push Server & MQTT

MQTT-For-Twisted-Pythonhttps://github.com/adamvr/MQTT-For-Twisted-Python/blob/master/MQTT.py

MQTT4PythonTwistedDemohttps://code.google.com/p/beaglebone-iot/wiki/MQTT4PythonTwistedDemo

from twisted.internet import kqreactorkqreactor.install()

....

if __name__ == '__main__':

mqttMessageBuffer = []

clientCountRange = range(1, 1200) for r in clientCountRange: try: mqttFactory = MQTTListenerFactory() reactor.connectTCP("192.168.10.32", 1883, mqttFactory) except: log.msg("except " + sys.exec_info())

reactor.run()

13년 1월 28일 월요일

Page 30: Android Push Server & MQTT

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data)

class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return Echo()

reactor.listenTCP(1234, EchoFactory())reactor.run()

What is Twisted?

Twisted is an event-driven networking engine written in Python

from twisted.web import server, resourcefrom twisted.internet import reactor

class HelloResource(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader("content-type", "text/plain") return "I am request #" + str(self.numberRequests) + "\n"

reactor.listenTCP(8080, server.Site(HelloResource()))reactor.run()

13년 1월 28일 월요일

Page 31: Android Push Server & MQTT

Choosing a Reactor and GUI Toolkit Integration1. Overview2. Reactor Functionality3. General Purpose Reactors

◦ Select()-based Reactor4. Platform-Specific Reactors

◦ Poll-based Reactor◦ KQueue◦ WaitForMultipleObjects (WFMO) for Win32◦ Input/Output Completion Port (IOCP) for Win32◦ Epoll-based Reactor

5. GUI Integration Reactors◦ GTK+◦ wxPython◦ CoreFoundation

6. Non-Reactor GUI Integration◦ Tkinter◦ PyUI

Reactor Overview1. Reactor Basics2. Using the reactor object

This HOWTO introduces the Twisted reactor, describes the basics of the reactor and links to the various reactor interfaces.

Reactor BasicsThe reactor is the core of the event loop within Twisted -- the loop which drives applications using Twisted. The event loop is a programming construct that waits for and dispatches events or messages in a program. It works by calling some internal or external "event provider", which generally blocks until an event has arrived, and then calls the relevant event handler ("dispatches the event"). The reactor provides basic interfaces to a number of services, including network communications, threading, and event dispatching.

Twisted Document - Reactor

13년 1월 28일 월요일

Page 32: Android Push Server & MQTT

Reactor Pattern?

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers

All reactor systems are single threaded by definition, but can exist in a multithreaded environment.

C - libeventC++ - POCO C++ LibrariesJava - Apache MINA, JBoss Netty, Apache Cocoon, DisruptorJavaScript - Node.jsPerl - POEPython - TwistedRuby - EventMachine

http://en.wikipedia.org/wiki/Reactor_pattern

13년 1월 28일 월요일

Page 33: Android Push Server & MQTT

POSA 2 Patterns for Concurrent and Networked Objects

이미지 출처 : http://www.cs.wustl.edu/~schmidt/POSA/POSA2/

13년 1월 28일 월요일

Page 34: Android Push Server & MQTT

이미지 출처 : http://goo.gl/HUiFZ

Opps !

13년 1월 28일 월요일

Page 35: Android Push Server & MQTT

만드는 이유?

* 기업 => Push 마케팅 Tool

* 기업 기간계 시스템 연동 : 보험, 증권, 은행, 쇼핑몰

=> SMS/MMS = 비용

* 기업 자신들의 모바일 앱 고객 분석

13년 1월 28일 월요일

Page 36: Android Push Server & MQTT

* 이런 걸 왜 사?

* 내가 이틀이면 만들겠다 = 자체 개발? => 몇 달 뒤 전화 옴

* 발송만 보면 이틀이 아니라 몇 시간 내에 가능 => 맞는 말

* 그러나 기업 자체 개발 인력 부족, 자체 문제 및 요구사항

* History : 메일 = PUSH

기업 개발 부서 반응

13년 1월 28일 월요일

Page 37: Android Push Server & MQTT

헉 !!!

http://count.ly/https://github.com/Countlyhttps://github.com/isaacs/node-supervisorhttp://countly.firejune.io/loginhttp://goo.gl/U63L1

node.js, mongodb, nginxsupervisor, python

13년 1월 28일 월요일

Page 38: Android Push Server & MQTT

* Private Push Server 구축은 어렵지 않게 시작 할 수 있다.

* 이를 구축하는데 MQTT 좋은 선택

* Python IO Server/Client => twisted 시작

* 국내 Push 독보적인 솔루션 없음 => 메일 시장 History 반복

정리

13년 1월 28일 월요일

Page 40: Android Push Server & MQTT

감사합니다.

13년 1월 28일 월요일

Page 41: Android Push Server & MQTT

* 참고* 모바일 Push와 nPush - http://helloworld.naver.com/helloworld/1119* Android PUSH와 카카오톡 사건 - http://mobizen.pe.kr/1074* 3G에 대한 이해 - http://helloworld.naver.com/helloworld/111111* C500k - http://www.mimul.com/pebble/default/2012/07/18/1342602654675.html* 어반 에어쉽 Architecture - http://www.slideshare.net/eonnen/from-100s-to-100s-of-millions* MQTT

* Test Server - http://test.mosquitto.org/ * http://www.slideshare.net/andypiper/introducing-mqtt * ftp://public.dhe.ibm.com/software/kr/ik2012/mqtt.pdf * 활용 : http://mqtt.org/wiki/doku.php/example_uses * http://stephendnicholas.com/archives/1217* XMPP * Google App Engine - http://goo.gl/lXHcM * Android Push - http://blog.softwaregeeks.org/archives/530 * http://blog.doortts.com/148 * IBM 웹기반 알림 - http://goo.gl/O8UEy* POSA 2 무료 강좌 - http://www.devpia.com/NET2/EvaCast/Lecture/?fCode=1&sCode=2&c=4

13년 1월 28일 월요일