-
Notifications
You must be signed in to change notification settings - Fork 7
/
GraphManipulationServiceBinaryHttp.ts
166 lines (141 loc) · 6.53 KB
/
GraphManipulationServiceBinaryHttp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// =========================================================================================
// ExecutionGraph
// Copyright (C) 2014 by Gabriel Nützi <gnuetzi (at) gmail (døt) com>
//
// @date Sun Jul 29 2018
// @author Simon Spoerri, simon (døt) spoerri (at) gmail (døt) com
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// =========================================================================================
import { Injectable, Inject } from '@angular/core';
import { VERBOSE_LOG_TOKEN } from '../tokens';
import { flatbuffers } from 'flatbuffers';
import { ILogger, LoggerFactory, stringify } from '@eg/logger';
import { Id } from '@eg/common';
import { GraphManipulationService, sz } from './GraphManipulationService';
import { BinaryHttpRouterService } from './BinaryHttpRouterService';
import { Node, NodeId, Socket, Connection } from '../model';
import * as conversions from './Conversions';
import { isDefined } from '@angular/compiler/src/util';
@Injectable()
export class GraphManipulationServiceBinaryHttp extends GraphManipulationService {
private logger: ILogger;
constructor(
loggerFactory: LoggerFactory,
private readonly binaryRouter: BinaryHttpRouterService,
@Inject(VERBOSE_LOG_TOKEN) private readonly verboseResponseLog = true
) {
super();
this.logger = loggerFactory.create('GraphManipulationServiceBinaryHttp');
}
public async addNode(graphId: Id, type: string, name: string): Promise<Node> {
// Build the AddNode request
let builder = new flatbuffers.Builder(356);
let offGraphId = builder.createString(graphId.toString());
let offType = builder.createString(type);
let offName = builder.createString(name);
sz.NodeConstructionInfo.startNodeConstructionInfo(builder);
sz.NodeConstructionInfo.addName(builder, offName);
sz.NodeConstructionInfo.addType(builder, offType);
let off = sz.NodeConstructionInfo.endNodeConstructionInfo(builder);
sz.AddNodeRequest.startAddNodeRequest(builder);
sz.AddNodeRequest.addGraphId(builder, offGraphId);
sz.AddNodeRequest.addNode(builder, off);
off = sz.AddNodeRequest.endAddNodeRequest(builder);
builder.finish(off);
let requestPayload = builder.asUint8Array();
// Send the request
const result = await this.binaryRouter.post('graph/addNode', requestPayload);
const buf = new flatbuffers.ByteBuffer(result);
const response = sz.AddNodeResponse.getRootAsAddNodeResponse(buf);
let node = response.node();
this.logger.info(`Added new node [type: '${node.type()}', name: '${node.name()}', ins: ${node.inputSocketsLength()},
outs: ${node.outputSocketsLength()}].`);
let nodeModel = conversions.toNode(node);
if (this.verboseResponseLog) {
this.logger.info(`Node: '${stringify(nodeModel, 4)}'`);
}
return nodeModel;
}
public async removeNode(graphId: Id, nodeId: NodeId): Promise<void> {
// Build the RemoveNode request
let builder = new flatbuffers.Builder(356);
let offGraphId = builder.createString(graphId.toString());
sz.RemoveNodeRequest.startRemoveNodeRequest(builder);
sz.RemoveNodeRequest.addGraphId(builder, offGraphId);
sz.RemoveNodeRequest.addNodeId(builder, builder.createLong(nodeId.low, nodeId.high));
let reqOff = sz.RemoveNodeRequest.endRemoveNodeRequest(builder);
builder.finish(reqOff);
let requestPayload = builder.asUint8Array();
// Send the request
await this.binaryRouter.post('graph/removeNode', requestPayload);
this.logger.debug(`Removed node [id: '${nodeId}'] from graph [id: '${graphId.toString()}']`);
}
public async addConnection(
graphId: Id,
source: Socket,
target: Socket,
cycleDetection: boolean
): Promise<Connection> {
const connection = Connection.create(source, target, true);
const builder = new flatbuffers.Builder(356);
const offGraphId = builder.createString(graphId.toString());
sz.AddConnectionRequest.startAddConnectionRequest(builder);
sz.AddConnectionRequest.addGraphId(builder, offGraphId);
sz.AddConnectionRequest.addSocketLink(
builder,
sz.SocketLinkDescription.createSocketLinkDescription(
builder,
conversions.toFbLong(connection.outputSocket.parent.id),
conversions.toFbLong(connection.outputSocket.index),
conversions.toFbLong(connection.inputSocket.parent.id),
conversions.toFbLong(connection.inputSocket.index),
connection.isWriteLink
)
);
sz.AddConnectionRequest.addCheckForCycles(builder, cycleDetection);
const off = sz.AddConnectionRequest.endAddConnectionRequest(builder);
builder.finish(off);
const requestPayload = builder.asUint8Array();
const result = await this.binaryRouter.post('graph/addConnection', requestPayload);
if (!result.length) {
// Succesfully added connection
this.logger.info(
`Added connection: ['${source.idString}' ⟶ '${target.idString}'] from graph [id: '${graphId.toString()}']`
);
return connection;
} else {
// Cycle detected, read the repsonse
const buf = new flatbuffers.ByteBuffer(result);
const response = sz.AddNodeResponse.getRootAsAddNodeResponse(buf);
throw 'Cycle detection not yet implemented!';
}
}
public async removeConnection(graphId: Id, source: Socket, target: Socket): Promise<void> {
const connection = Connection.create(source, target, false);
const builder = new flatbuffers.Builder(356);
const offGraphId = builder.createString(graphId.toString());
sz.RemoveConnectionRequest.startRemoveConnectionRequest(builder);
sz.RemoveConnectionRequest.addGraphId(builder, offGraphId);
sz.RemoveConnectionRequest.addSocketLink(
builder,
sz.SocketLinkDescription.createSocketLinkDescription(
builder,
conversions.toFbLong(connection.outputSocket.parent.id),
conversions.toFbLong(connection.outputSocket.index),
conversions.toFbLong(connection.inputSocket.parent.id),
conversions.toFbLong(connection.inputSocket.index),
connection.isWriteLink
)
);
const off = sz.RemoveConnectionRequest.endRemoveConnectionRequest(builder);
builder.finish(off);
const requestPayload = builder.asUint8Array();
await this.binaryRouter.post('graph/removeConnection', requestPayload);
this.logger.info(
`Removed connection: ['${source.idString}' ⟶ '${target.idString}'] from graph [id: '${graphId.toString()}']`
);
}
}