From 65cb87eea94226986b4f3e28c3dd05a77477451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bires?= Date: Mon, 3 Oct 2016 18:33:57 -0300 Subject: [PATCH] fix: Fixed the check for the originator in the isForMe method on Channel Fixed the check for the originator in the isForMe method on Channel --- src/Lime/Protocol/Client/Channel.ts | 272 ++++++++++++++-------------- 1 file changed, 136 insertions(+), 136 deletions(-) diff --git a/src/Lime/Protocol/Client/Channel.ts b/src/Lime/Protocol/Client/Channel.ts index a700261..f8a5d40 100644 --- a/src/Lime/Protocol/Client/Channel.ts +++ b/src/Lime/Protocol/Client/Channel.ts @@ -1,136 +1,136 @@ -import {Envelope} from "../Envelope"; -import {Message, MessageListener} from "../Message"; -import {Command, CommandListener, CommandMethod, CommandStatus} from "../Command"; -import {Notification, NotificationListener, NotificationEvent} from "../Notification"; -import {Session, SessionListener, SessionState} from "../Session"; -import {Transport} from "../Network/Transport"; - -export interface MessageChannel extends MessageListener { - sendMessage(message: Message): void; -} - -export interface CommandChannel extends CommandListener { - sendCommand(command: Command): void; -} - -export interface NotificationChannel extends NotificationListener { - sendNotification(notification: Notification): void; -} - -export interface SessionChannel extends SessionListener { - sendSession(session: Session): void; -} - -export abstract class Channel implements MessageChannel, CommandChannel, NotificationChannel, SessionChannel { - - private autoReplyPings: boolean; - private autoNotifyReceipt: boolean; - - transport: Transport; - remoteNode: string; - localNode: string; - sessionId: string; - state: SessionState; - - constructor(transport: Transport, autoReplyPings: boolean, autoNotifyReceipt: boolean) { - this.autoReplyPings = autoReplyPings; - this.autoNotifyReceipt = autoNotifyReceipt; - this.transport = transport; - this.state = SessionState.NEW; - - this.transport.onEnvelope = (envelope) => { - // Message - if (Envelope.isMessage(envelope)) { - const message = envelope; - this.notifyMessage(message); - this.onMessage(message); - } - // Notification - else if (Envelope.isNotification(envelope)) { - const notification = envelope; - this.onNotification(notification); - } - // Command - else if (Envelope.isCommand(envelope)) { - const command = envelope; - if (this.autoReplyPings && command.id && - command.uri === "/ping" && - command.method === CommandMethod.GET && - this.isForMe(command)) - { - const pingCommandResponse = { - id: command.id, - to: command.from, - method: CommandMethod.GET, - status: CommandStatus.SUCCESS, - type: "application/vnd.lime.ping+json", - resource: {} - } - this.sendCommand(pingCommandResponse); - } - this.onCommand(command); - } - // Session - else if (Envelope.isSession(envelope)) { - const session = envelope; - this.onSession(session); - } - }; - } - - sendMessage(message: Message) { - if (this.state !== SessionState.ESTABLISHED) { - throw new Error(`Cannot send in the '${this.state}' state`); - } - this.send(message); - } - abstract onMessage(message: Message): void; - - sendCommand(command: Command) { - if (this.state !== SessionState.ESTABLISHED) { - throw new Error(`Cannot send in the '${this.state}' state`); - } - this.send(command); - } - abstract onCommand(message: Command): void; - - sendNotification(notification: Notification) { - if (this.state !== SessionState.ESTABLISHED) { - throw new Error(`Cannot send in the '${this.state}' state`); - } - this.send(notification); - } - abstract onNotification(message: Notification): void; - - sendSession(session: Session) { - if (this.state === SessionState.FINISHED || this.state === SessionState.FAILED) { - throw new Error(`Cannot send in the '${this.state}' state`); - } - this.send(session); - } - abstract onSession(message: Session): void; - - private send(envelope: Envelope) { - this.transport.send(envelope); - } - - private notifyMessage(message: Message) { - if (this.autoNotifyReceipt && - message.id && - message.from && - this.isForMe(message)) { - const notification: Notification = { - id: message.id, - to: message.from, - event: NotificationEvent.RECEIVED - }; - this.sendNotification(notification); - } - } - - private isForMe(envelope: Envelope): boolean { - return envelope.to === null || - envelope.to === this.localNode || - this.localNode.substring(0, envelope.to.length) === envelope.to; - } -} +import {Envelope} from "../Envelope"; +import {Message, MessageListener} from "../Message"; +import {Command, CommandListener, CommandMethod, CommandStatus} from "../Command"; +import {Notification, NotificationListener, NotificationEvent} from "../Notification"; +import {Session, SessionListener, SessionState} from "../Session"; +import {Transport} from "../Network/Transport"; + +export interface MessageChannel extends MessageListener { + sendMessage(message: Message): void; +} + +export interface CommandChannel extends CommandListener { + sendCommand(command: Command): void; +} + +export interface NotificationChannel extends NotificationListener { + sendNotification(notification: Notification): void; +} + +export interface SessionChannel extends SessionListener { + sendSession(session: Session): void; +} + +export abstract class Channel implements MessageChannel, CommandChannel, NotificationChannel, SessionChannel { + + private autoReplyPings: boolean; + private autoNotifyReceipt: boolean; + + transport: Transport; + remoteNode: string; + localNode: string; + sessionId: string; + state: SessionState; + + constructor(transport: Transport, autoReplyPings: boolean, autoNotifyReceipt: boolean) { + this.autoReplyPings = autoReplyPings; + this.autoNotifyReceipt = autoNotifyReceipt; + this.transport = transport; + this.state = SessionState.NEW; + + this.transport.onEnvelope = (envelope) => { + // Message + if (Envelope.isMessage(envelope)) { + const message = envelope; + this.notifyMessage(message); + this.onMessage(message); + } + // Notification + else if (Envelope.isNotification(envelope)) { + const notification = envelope; + this.onNotification(notification); + } + // Command + else if (Envelope.isCommand(envelope)) { + const command = envelope; + if (this.autoReplyPings && command.id && + command.uri === "/ping" && + command.method === CommandMethod.GET && + this.isForMe(command)) + { + const pingCommandResponse = { + id: command.id, + to: command.from, + method: CommandMethod.GET, + status: CommandStatus.SUCCESS, + type: "application/vnd.lime.ping+json", + resource: {} + } + this.sendCommand(pingCommandResponse); + } + this.onCommand(command); + } + // Session + else if (Envelope.isSession(envelope)) { + const session = envelope; + this.onSession(session); + } + }; + } + + sendMessage(message: Message) { + if (this.state !== SessionState.ESTABLISHED) { + throw new Error(`Cannot send in the '${this.state}' state`); + } + this.send(message); + } + abstract onMessage(message: Message): void; + + sendCommand(command: Command) { + if (this.state !== SessionState.ESTABLISHED) { + throw new Error(`Cannot send in the '${this.state}' state`); + } + this.send(command); + } + abstract onCommand(message: Command): void; + + sendNotification(notification: Notification) { + if (this.state !== SessionState.ESTABLISHED) { + throw new Error(`Cannot send in the '${this.state}' state`); + } + this.send(notification); + } + abstract onNotification(message: Notification): void; + + sendSession(session: Session) { + if (this.state === SessionState.FINISHED || this.state === SessionState.FAILED) { + throw new Error(`Cannot send in the '${this.state}' state`); + } + this.send(session); + } + abstract onSession(message: Session): void; + + private send(envelope: Envelope) { + this.transport.send(envelope); + } + + private notifyMessage(message: Message) { + if (this.autoNotifyReceipt && + message.id && + message.from && + this.isForMe(message)) { + const notification: Notification = { + id: message.id, + to: message.from, + event: NotificationEvent.RECEIVED + }; + this.sendNotification(notification); + } + } + + private isForMe(envelope: Envelope): boolean { + return !envelope.to || + envelope.to === this.localNode || + this.localNode.substring(0, envelope.to.length) === envelope.to; + } +}