module AliMNS { export class MQ implements IMQ , INotifyRecv { constructor ( name , account , region ? ) { this . _name = name ; this . _account = account ; if ( region ) { if ( typeof region === "string" ) this . _region = new Region ( region , NetworkType . Public , Zone . China ) ; else this . _region = region ; } this . _urlAttr = this . makeAttrURL ( ) ; this . _url = this . makeURL ( ) ; this . _openStack = new OpenStack ( account ) ; } public getName ( ) { return this . _name ; } public getAccount ( ) { return this . _account ; } public getRegion ( ) { return this . _region ; } public getAttrsP ( ) { debug ( "GET " + this . _urlAttr ) ; return this . _openStack . sendP ( "GET" , this . _urlAttr ) ; } public setAttrsP ( options ) { var body = { Queue : options } ; debug ( "PUT " + this . _urlAttr , body ) ; return this . _openStack . sendP ( "PUT" , this . _urlAttr + "?metaoverride=true" , body ) ; } public sendP ( msg , priority ? , delaySeconds ? ) { var b64 = this . utf8ToBase64 ( msg ) ; var body = { Message : { MessageBody : b64 } } ; if ( ! isNaN ( priority ) ) body . Message . Priority = priority ; if ( ! isNaN ( delaySeconds ) ) body . Message . DelaySeconds = delaySeconds ; debug ( "POST " + this . _url , body ) ; this . _openStack . accumulateNextGASend ( "MQ.sendP" ) ; return this . _openStack . sendP ( "POST" , this . _url , body ) ; } public getRecvTolerance ( ) { return this . _recvTolerance ; } public setRecvTolerance ( value ) { this . _recvTolerance = value ; } public recvP ( waitSeconds ? ) { var _this = this ; var url = this . _url ; if ( waitSeconds ) url += "?waitseconds=" + waitSeconds ; debug ( "GET " + url ) ; return new Promise ( function ( resolve , reject ) { var options = { timeout : 1000 * _this . _recvTolerance } ; if ( waitSeconds ) options . timeout += ( 1000 * waitSeconds ) ; _this . _openStack . accumulateNextGASend ( "MQ.recvP" ) ; _this . _openStack . sendP ( "GET" , url , null , null , options ) . done ( function ( data ) { debug ( data ) ; if ( data && data . Message && data . Message . MessageBody ) { data . Message . MessageBody = _this . base64ToUtf8 ( data . Message . MessageBody ) ; } resolve ( data ) ; } , function ( ex ) { if ( ex . code === "ETIMEDOUT" ) { var exTimeout = new Error ( "timeout" ) ; exTimeout . innerException = ex ; exTimeout . code = ex . code ; reject ( exTimeout ) ; } else { reject ( ex ) ; } } ) ; } ) ; } public peekP ( ) { var _this = this ; var url = this . _url + "?peekonly=true" ; debug ( "GET " + url ) ; this . _openStack . accumulateNextGASend ( "MQ.peekP" ) ; return this . _openStack . sendP ( "GET" , url ) . then ( function ( data ) { debug ( data ) ; _this . decodeB64Messages ( data ) ; return data ; } ) ; } public deleteP ( receiptHandle ) { var url = this . _url + "?ReceiptHandle=" + receiptHandle ; debug ( "DELETE " + url ) ; this . _openStack . accumulateNextGASend ( "MQ.deleteP" ) ; return this . _openStack . sendP ( "DELETE" , url ) ; } public reserveP ( receiptHandle , reserveSeconds ) { var url = this . _url + "?ReceiptHandle=" + receiptHandle + "&VisibilityTimeout=" + reserveSeconds ; debug ( "PUT " + url ) ; this . _openStack . accumulateNextGASend ( "MQ.reserveP" ) ; return this . _openStack . sendP ( "PUT" , url ) ; } public notifyRecv ( cb , waitSeconds ? ) { if ( this . _notifyRecv === null ) this . _notifyRecv = new NotifyRecv ( this ) ; return this . _notifyRecv . notifyRecv ( cb , waitSeconds || 5 ) ; } public notifyStopP ( ) { if ( this . _notifyRecv === null ) return Promise . resolve ( 0 ) ; else return this . _notifyRecv . notifyStopP ( ) ; } protected utf8ToBase64 ( src ) { var buf = new Buffer ( src , 'utf8' ) ; return buf . toString ( 'base64' ) ; } protected base64ToUtf8 ( src ) { var buf = new Buffer ( src , 'base64' ) ; return buf . toString ( 'utf8' ) ; } protected decodeB64Messages ( data ) { if ( data && data . Message && data . Message . MessageBody ) { data . Message . MessageBody = this . base64ToUtf8 ( data . Message . MessageBody ) ; } } private makeAttrURL ( ) { return Util . format ( this . _pattern , this . _account . getHttps ( ) ? "https" : "http" , this . _account . getAccountId ( ) , this . _region . toString ( ) , this . _name ) ; } private makeURL ( ) { return this . makeAttrURL ( ) + "/messages" ; } protected _url : string ; protected _openStack : OpenStack ; protected _notifyRecv : INotifyRecv = null ; protected _recvTolerance = 5 ; private _name : string ; private _region = new Region ( City . Hangzhou ) ; private _account : Account ; private _urlAttr : string ; private _pattern = "%s://%s.mns.%s.aliyuncs.com/queues/%s" ; } }