Skip to content

Commit

Permalink
refactor(transport-tcp): rename object to data in Payload
Browse files Browse the repository at this point in the history
  • Loading branch information
adrigzr committed Mar 23, 2023
1 parent 49eaaa3 commit c3e657e
Show file tree
Hide file tree
Showing 41 changed files with 158 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { PacketSocket } from '@agnoc/transport-tcp';
import type { PacketEventBus } from '../packet.event-bus';
import type { PacketMessage } from '../packet.message';
import type { ConnectionProps } from '@agnoc/domain';
import type {
Packet,
PacketFactory,
PayloadObjectName,
PayloadObjectFrom,
CreatePacketProps,
} from '@agnoc/transport-tcp';
import type { Packet, PacketFactory, PayloadDataName, PayloadDataFrom, CreatePacketProps } from '@agnoc/transport-tcp';

export interface PacketConnectionProps extends ConnectionProps {
socket: PacketSocket;
Expand All @@ -31,25 +25,25 @@ export class PacketConnection extends Connection<PacketConnectionProps> {
return this.props.socket;
}

send<Name extends PayloadObjectName>(name: Name, object: PayloadObjectFrom<Name>): Promise<void> {
send<Name extends PayloadDataName>(name: Name, object: PayloadDataFrom<Name>): Promise<void> {
const packet = this.packetFactory.create(name, object, this.getPacketProps());

return this.write(packet);
}

respond<Name extends PayloadObjectName>(name: Name, object: PayloadObjectFrom<Name>, packet: Packet): Promise<void> {
respond<Name extends PayloadDataName>(name: Name, object: PayloadDataFrom<Name>, packet: Packet): Promise<void> {
return this.write(this.packetFactory.create(name, object, packet));
}

sendAndWait<Name extends PayloadObjectName>(name: Name, object: PayloadObjectFrom<Name>): Promise<PacketMessage> {
sendAndWait<Name extends PayloadDataName>(name: Name, object: PayloadDataFrom<Name>): Promise<PacketMessage> {
const packet = this.packetFactory.create(name, object, this.getPacketProps());

return this.writeAndWait(packet);
}

respondAndWait<Name extends PayloadObjectName>(
respondAndWait<Name extends PayloadDataName>(
name: Name,
object: PayloadObjectFrom<Name>,
object: PayloadDataFrom<Name>,
packet: Packet,
): Promise<PacketMessage> {
return this.writeAndWait(this.packetFactory.create(name, object, packet));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ClientLoginEventHandler implements PacketEventHandler {
if (!message.device) {
const data = {
result: 12002,
reason: `Device not registered(devsn: ${message.packet.payload.object.deviceSerialNumber})`,
reason: `Device not registered(devsn: ${message.packet.payload.data.deviceSerialNumber})`,
};

return message.respond('CLIENT_ONLINE_RSP', data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DeviceBatteryUpdateEventHandler implements PacketEventHandler {
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;

message.device.updateBattery(this.deviceBatteryMapper.toDomain(data.battery.level));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class DeviceCleanMapDataReportEventHandler implements PacketEventHandler
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;

// TODO: save device clean map data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class DeviceCleanMapReportEventHandler implements PacketEventHandler {
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;

// TODO: save device clean map data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DeviceMapChargerPositionUpdateEventHandler implements PacketEventHa
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;

message.device.map?.updateCharger(
new MapPosition({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DeviceMapUpdateEventHandler implements PacketEventHandler {
spotInfo,
cleanPlanList,
currentPlanId,
} = message.packet.payload.object;
} = message.packet.payload.data;

if (statusInfo) {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class DeviceMapWorkStatusUpdateEventHandler implements PacketEventHandler
mopType,
cleanSize,
cleanTime,
} = message.packet.payload.object;
} = message.packet.payload.data;

message.device.updateCurrentClean(
new DeviceCleanWork({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class DeviceRegisterEventHandler implements PacketEventHandler {
constructor(private readonly deviceRepository: DeviceRepository) {}

async handle(message: PacketMessage<'DEVICE_REGISTER_REQ'>): Promise<void> {
const data = message.packet.payload.object;
const data = message.packet.payload.data;
const device = new Device({
id: ID.generate(),
userId: ID.generate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class DeviceSettingsUpdateEventHandler implements PacketEventHandler {
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;
const deviceSettings = new DeviceSettings({
voice: this.deviceVoiceMapper.toDomain({
isEnabled: data.voice.voiceMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DeviceVersionUpdateEventHandler implements PacketEventHandler {
throw new DomainException('Device not found');
}

const data = message.packet.payload.object;
const data = message.packet.payload.data;

message.device.updateVersion(new DeviceVersion({ software: data.softwareVersion, hardware: data.hardwareVersion }));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DeviceWlanUpdateEventHandler implements PacketEventHandler {
throw new DomainException('Device not found');
}

const data = message.packet.payload.object.body;
const data = message.packet.payload.data.body;

message.device.updateWlan(
new DeviceWlan({
Expand Down
12 changes: 6 additions & 6 deletions packages/adapter-tcp/src/packet-server.connection-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PacketConnection } from './aggregate-roots/packet-connection.aggre
import type { PacketConnectionFactory } from './factories/connection.factory';
import type { PacketEventBus, PacketEventBusEvents } from './packet.event-bus';
import type { DeviceRepository, Device, Connection, ConnectionRepository } from '@agnoc/domain';
import type { PacketServer, Packet, PayloadObjectName } from '@agnoc/transport-tcp';
import type { PacketServer, Packet, PayloadDataName } from '@agnoc/transport-tcp';

export class PackerServerConnectionHandler {
private readonly servers = new Map<PacketServer, Set<PacketConnection>>();
Expand Down Expand Up @@ -55,21 +55,21 @@ export class PackerServerConnectionHandler {
});
}

private async emitPacketEvent(message: PacketMessage<PayloadObjectName>) {
const name = message.packet.payload.opcode.name as PayloadObjectName;
private async emitPacketEvent(message: PacketMessage<PayloadDataName>) {
const name = message.packet.payload.opcode.name as PayloadDataName;
const sequence = message.packet.sequence.toString();

this.checkForPacketEventHandler(name);

// Emit the packet event by the sequence string.
// This is used to wait for a response from a packet.
await this.packetEventBus.emit(sequence, message as PacketEventBusEvents[PayloadObjectName]);
await this.packetEventBus.emit(sequence, message as PacketEventBusEvents[PayloadDataName]);

// Emit the packet event by the opcode name.
await this.packetEventBus.emit(name, message as PacketEventBusEvents[PayloadObjectName]);
await this.packetEventBus.emit(name, message as PacketEventBusEvents[PayloadDataName]);
}

private checkForPacketEventHandler(event: PayloadObjectName) {
private checkForPacketEventHandler(event: PayloadDataName) {
const count = this.packetEventBus.listenerCount(event);

// Throw an error if there is no event handler for the packet event.
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-tcp/src/packet.event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EventBus } from '@agnoc/toolkit';
import type { PacketMessage } from './packet.message';
import type { PayloadObjectName } from '@agnoc/transport-tcp';
import type { PayloadDataName } from '@agnoc/transport-tcp';

/** Events for the packet event bus. */
export type PacketEventBusEvents = {
[Name in PayloadObjectName]: PacketMessage<Name>;
[Name in PayloadDataName]: PacketMessage<Name>;
} & {
[key: string]: PacketMessage;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-tcp/src/packet.event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { PacketMessage } from './packet.message';
import type { EventHandler } from '@agnoc/toolkit';
import type { PayloadObjectName } from '@agnoc/transport-tcp';
import type { PayloadDataName } from '@agnoc/transport-tcp';

/** Base class for packet event handlers. */
export abstract class PacketEventHandler implements EventHandler {
/** The name of the event to listen to. */
abstract forName: PayloadObjectName;
abstract forName: PayloadDataName;

/** Handle the event. */
abstract handle(message: PacketMessage<this['forName']>): void;
Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-tcp/src/packet.message.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { PacketConnection } from './aggregate-roots/packet-connection.aggregate-root';
import type { Device } from '@agnoc/domain';
import type { Packet, PayloadObjectFrom, PayloadObjectName } from '@agnoc/transport-tcp';
import type { Packet, PayloadDataFrom, PayloadDataName } from '@agnoc/transport-tcp';

export class PacketMessage<Name extends PayloadObjectName = PayloadObjectName> {
export class PacketMessage<Name extends PayloadDataName = PayloadDataName> {
constructor(readonly connection: PacketConnection, readonly packet: Packet<Name>) {}

get device(): Device | undefined {
return this.connection.device;
}

respond<Name extends PayloadObjectName>(name: Name, object: PayloadObjectFrom<Name>): Promise<void> {
respond<Name extends PayloadDataName>(name: Name, object: PayloadDataFrom<Name>): Promise<void> {
return this.connection.respond(name, object, this.packet);
}

respondAndWait<Name extends PayloadObjectName>(name: Name, object: PayloadObjectFrom<Name>): Promise<PacketMessage> {
respondAndWait<Name extends PayloadDataName>(name: Name, object: PayloadDataFrom<Name>): Promise<PacketMessage> {
return this.connection.respondAndWait(name, object, this.packet);
}
}
4 changes: 2 additions & 2 deletions packages/adapter-tcp/src/tcp.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PacketMapper,
PacketServer,
PayloadMapper,
PayloadObjectParserService,
PayloadDataParserService,
PacketFactory,
} from '@agnoc/transport-tcp';
import { LocateDeviceEventHandler } from './command-handlers/locate-device.event-handler';
Expand Down Expand Up @@ -59,7 +59,7 @@ export class TCPServer implements Server {
private readonly commandQueryHandlerRegistry: TaskHandlerRegistry<CommandsOrQueries>,
) {
// Packet foundation
const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders()));
const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders()));
const packetMapper = new PacketMapper(payloadMapper);
const packetFactory = new PacketFactory();

Expand Down
14 changes: 7 additions & 7 deletions packages/adapter-tcp/test/integration/tcp.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PacketMapper,
PacketSocket,
PayloadMapper,
PayloadObjectParserService,
PayloadDataParserService,
} from '@agnoc/transport-tcp';
import { expect } from 'chai';
import { TCPServer } from '@agnoc/adapter-tcp';
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Integration', function () {
);

// Client blocks
const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders()));
const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders()));
const packetMapper = new PacketMapper(payloadMapper);

packetSocket = new PacketSocket(packetMapper);
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Integration', function () {
expect(receivedPacket.deviceId.equals(sentPacket.deviceId)).to.be.true;
expect(receivedPacket.userId.equals(sentPacket.userId)).to.be.true;
expect(receivedPacket.payload.opcode.value).to.be.equal('CLIENT_HEARTBEAT_RSP');
expect(receivedPacket.payload.object).to.be.deep.equal({});
expect(receivedPacket.payload.data).to.be.deep.equal({});
});

it('should handle heartbeat packets on map server', async function () {
Expand All @@ -100,7 +100,7 @@ describe('Integration', function () {
expect(receivedPacket.deviceId.equals(sentPacket.deviceId)).to.be.true;
expect(receivedPacket.userId.equals(sentPacket.userId)).to.be.true;
expect(receivedPacket.payload.opcode.value).to.be.equal('CLIENT_HEARTBEAT_RSP');
expect(receivedPacket.payload.object).to.be.deep.equal({});
expect(receivedPacket.payload.data).to.be.deep.equal({});
});

it('should handle ntp connections', async function () {
Expand All @@ -117,8 +117,8 @@ describe('Integration', function () {
expect(receivedPacket.deviceId.value).to.be.equal(0);
expect(receivedPacket.userId.value).to.be.equal(0);
expect(receivedPacket.payload.opcode.value).to.be.equal('DEVICE_TIME_SYNC_RSP');
expect(receivedPacket.payload.object.result).to.be.equal(0);
expect(receivedPacket.payload.object.body.time).to.be.greaterThanOrEqual(now);
expect(receivedPacket.payload.data.result).to.be.equal(0);
expect(receivedPacket.payload.data.body.time).to.be.greaterThanOrEqual(now);
});
});

Expand All @@ -136,7 +136,7 @@ describe('Integration', function () {

expect(receivedPacket.payload.opcode.value).to.be.equal('DEVICE_REGISTER_RSP');

const device = await deviceRepository.findOneById(new ID(receivedPacket.payload.object.device.id));
const device = await deviceRepository.findOneById(new ID(receivedPacket.payload.data.device.id));

expect(device).to.exist;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */
import {
PayloadObjectParserService,
PayloadDataParserService,
getProtobufRoot,
PacketMapper,
getCustomDecoders,
Expand Down Expand Up @@ -33,7 +33,7 @@ export function main(): void {
stderr: process.stderr,
};

const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders()));
const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders()));
const packetMapper = new PacketMapper(payloadMapper);
const decodeCommand = new DecodeCommand(stdio, packetMapper);
const readCommand = new ReadCommand(stdio, packetMapper);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/decode.command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('DecodeCommand', function () {
const [ret] = await readStream(stdio.stdout, 'utf8');

expect(ret).to.equal(
'[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n',
'[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n',
);

verify(packetMapper.toDomain(deepEqual(buffer))).once();
Expand All @@ -73,7 +73,7 @@ describe('DecodeCommand', function () {
sequence: 'fb3dd1ebc0e6c58f',
payload: {
opcode: 'DEVICE_GETTIME_RSP',
object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } },
data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } },
},
},
]);
Expand All @@ -91,7 +91,7 @@ describe('DecodeCommand', function () {
const [ret] = await readStream(stdio.stdout, 'utf8');

expect(ret).to.equal(
'[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n',
'[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n',
);

verify(packetMapper.toDomain(deepEqual(buffer))).once();
Expand All @@ -115,7 +115,7 @@ describe('DecodeCommand', function () {
sequence: 'fb3dd1ebc0e6c58f',
payload: {
opcode: 'DEVICE_GETTIME_RSP',
object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } },
data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } },
},
},
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/encode.command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('EncodeCommand', function () {
sequence: PacketSequence.fromString(jsonPacket.sequence),
payload: new Payload({
opcode: OPCode.fromName(jsonPacket.payload.opcode),
object: jsonPacket.payload.object,
data: jsonPacket.payload.data,
}),
}),
),
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('EncodeCommand', function () {
sequence: PacketSequence.fromString(jsonPacket.sequence),
payload: new Payload({
opcode: OPCode.fromName(jsonPacket.payload.opcode),
object: jsonPacket.payload.object,
data: jsonPacket.payload.data,
}),
}),
),
Expand Down
Loading

0 comments on commit c3e657e

Please sign in to comment.