Skip to content

Commit

Permalink
Patch for connection state
Browse files Browse the repository at this point in the history
ProcessPacket converted for new API
Status renamed to state
State codes simplified
  • Loading branch information
Universal Web committed Apr 29, 2023
1 parent b3ab02f commit 8a8250e
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 75 deletions.
6 changes: 3 additions & 3 deletions client/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export async function connect(requestObject) {
console.log(result);
const {
body,
stage,
state,
time,
scid
} = result.response;
if (stage === 101 && scid) {
if (state === 1 && scid) {
connected(body);
thisClient.stage.code = 1;
thisClient.state = 1;
thisClient.serverId = scid;
thisClient.lastPacketTime = Date.now();
thisClient.lastPacketGivenTime = time;
Expand Down
23 changes: 18 additions & 5 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
omit,
assign,
construct,
UniqID
UniqID,
isString
} from 'Acid';
import dgram from 'dgram';
// Default utility imports
Expand All @@ -38,6 +39,7 @@ import { processMessage } from './processMessage.js';
import { onMessage } from './onMessage.js';
import { connect } from './connect.js';
import { onListening, listen } from './listening.js';
import { currentPath } from '#directory';
// UNIVERSAL WEB Client Class
export class Client {
type = 'client';
Expand Down Expand Up @@ -117,9 +119,7 @@ export class Client {
encoding = 'binary';
max = 1280;
static connections = new Map();
state = {
code: 0
};
state = 0;
server = dgram.createSocket('udp4');
requests = new Map();
close() {
Expand All @@ -144,12 +144,25 @@ export function getClient(configuration) {
return client;
}
}
export function createClient(configuration, ignoreConnections) {
export async function createClient(configuration, ignoreConnections) {
console.log(configuration);
if (isString(configuration.service)) {
configuration.service = await getCertificate(configuration.service);
}
if (isString(configuration.profile)) {
configuration.profile = await getCertificate(configuration.profile);
}
const result = getClient(configuration, Client);
if (result) {
return result;
}
return construct(Client, [configuration]);
}
export async function udsp(configuration, ignoreConnections) {
const uwClient = await createClient(configuration);
console.time('CONNECTING');
const connectRequest = await uwClient.connect({});
console.timeEnd('CONNECTING');
return uwClient;
}
export { getCertificate };
2 changes: 1 addition & 1 deletion client/processMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function processMessage(response, headers) {
console.log(`STATE CODE: ${state}`);
}
if (response) {
if (response.state === 580) {
if (response.state === 3) {
thisContext.close();
return failed(`End event sent disconnected socket`);
}
Expand Down
2 changes: 1 addition & 1 deletion client/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function send(message, priority) {
port,
} = thisContext;
const headers = {};
const clientStatusCode = thisContext.state.code;
const clientStatusCode = thisContext.state;
console.log(`client Status Code is ${clientStatusCode}`);
if (clientStatusCode === 0) {
if (!message.head) {
Expand Down
25 changes: 6 additions & 19 deletions scripts/simulateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,20 @@ console.clear();
console.log('STARTING CLIENT');
console.time('FULL');
import { currentPath } from '#directory';
import { createClient, getCertificate } from '../client/index.js';
const service = await getCertificate(`${currentPath(import.meta)}/../services/universal.web.cert`);
const profile = await getCertificate(`${currentPath(import.meta)}/../profiles/default.cert`);
import { udsp } from '../client/index.js';
// Universal Web Socket
const uwClient = await createClient({
service,
profile,
const uwClient = await udsp({
service: `${currentPath(import.meta)}/../services/universal.web.cert`,
profile: `${currentPath(import.meta)}/../profiles/default.cert`,
ip: 'localhost',
port: 8888
});
console.time('CONNECTING');
const connectRequest = await uwClient.connect({
// evnt: 'state',
body: {
agent: 'node',
entity: 'bot',
state: '/'
}
});
console.timeEnd('CONNECTING');
console.log('Connected', uwClient);
console.log('INTRO =>', connectRequest.response.body);
console.log('INTRO =>', uwClient);
console.time('Request');
const stateRequest = await uwClient.request({
act: 'file',
body: {
path: '/'
path: 'index.js'
}
});
console.timeEnd('Request');
Expand Down
2 changes: 1 addition & 1 deletion server/actions/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function opn(socket, request, response) {
response.body = onConnectMessage;
}
// connection status - backwards compatibility
response.status = 101;
response.state = 1;
// Server connection id
response.scid = socket.serverIdRaw;
return true;
Expand Down
32 changes: 16 additions & 16 deletions server/actions/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import path from 'path';
export async function file(socket, request, response) {
const { resourceDirectory } = this;
info(request);
// const { path: requestPath } = request.body;
// response.head = {};
// if (!isString(requestPath) || isEmpty(requestPath)) {
// console.log('No valid state request received - Returning empty data');
// response.head.status = 404;
// return true;
// }
// const cleanedPath = cleanPath(`${resourceDirectory}/${requestPath}`);
// const data = await read(cleanedPath);
// const ext = path.extname(cleanedPath);
// console.log(`EXT => ${ext}`);
// response.body = {
// ext,
// data
// };
// return true;
const { path: requestPath } = request.body;
response.head = {};
if (!isString(requestPath) || isEmpty(requestPath)) {
console.log('No valid state request received - Returning empty data');
response.head.status = 404;
return true;
}
const cleanedPath = cleanPath(`${resourceDirectory}/${requestPath}`);
const data = await read(cleanedPath);
const ext = path.extname(cleanedPath);
console.log(`EXT => ${ext}`);
response.body = {
ext,
data
};
return true;
}
2 changes: 1 addition & 1 deletion server/clients/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
export async function destroy(client, reason, server) {
if (reason === 1) {
await client.send({
status: 580
status: 3
});
failed(`client ended from inactivity. Grace period ended.
ID: ${client.id}
Expand Down
3 changes: 1 addition & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class Server {
this.bindActions(actions);
this.profile = await getCertificate(configuration.profile);
configure(this);
this.status = 1;
this.server.on('error', this.onError);
this.server.on('listening', this.onListen);
this.server.on('message', this.onPacket);
Expand All @@ -78,7 +77,7 @@ export class Server {
socketCount = 0;
clientCount = 0;
actions = construct(Map);
statusDescriptions = ['initializing', 'initialized', 'failed to initialize'];
stateCodeDescriptions = ['initializing', 'initialized', 'failed to initialize'];
state = 0;
/*
* A puzzle used to challenge clients to ensure authenticity, connection liveliness, and congestion control.
Expand Down
7 changes: 5 additions & 2 deletions server/processPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
decrypt
} from '#crypto';
import { parsePacket } from './parsePacket.js';
import { processPacketEvent } from './processPacketEvent.js';
export async function processPacket(server, connection, headersBuffer, headers, packet) {
const clientId = headers.id;
const client = clients.get(toBase64(clientId));
const { nodes } = server;
const client = nodes.get(toBase64(clientId));
if (!client) {
return false;
}
Expand All @@ -28,6 +30,7 @@ export async function processPacket(server, connection, headersBuffer, headers,
}
msgReceived(message);
server.packetCount++;
await server.events.onMessage(client, message);
info(`Packet Count: ${server.packetCount}`);
await processPacketEvent(server, client, message);
success(`Messages Received: ${server.packetCount}`);
}
40 changes: 18 additions & 22 deletions server/processPacketEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,26 @@ export async function processPacketEvent(server, socket, message) {
const eventName = act || evnt;
const method = (act) ? actions.get(act) : events.get(evnt);
if (method) {
if (body) {
if (hasValue(sid)) {
info(`Request:${eventName} RequestID: ${sid}`);
console.log(message.body);
const response = {
sid
};
console.log(socket);
const hasResponse = await method(socket, message, response);
if (hasResponse) {
socket.send(response);
}
return;
} else {
const eid = message.eid;
if (hasValue(eid)) {
success(`Request:${method} Emit ID:${eid} ${stringify(message)}`);
return method(socket, body, message);
} else {
return failed(`Invalid Request type. No Emit ID was given. ${stringify(message)}`);
}
if (hasValue(sid)) {
info(`Request:${eventName} RequestID: ${sid}`);
console.log(message);
const response = {
sid
};
console.log(socket);
const hasResponse = await method(socket, message, response);
if (hasResponse) {
socket.send(response);
}
return;
} else {
return failed(`Invalid Request no body was sent. ${stringify(message)}`);
const eid = message.eid;
if (hasValue(eid)) {
success(`Request:${method} Emit ID:${eid} ${stringify(message)}`);
return method(socket, body, message);
} else {
return failed(`Invalid Request type. No Emit ID was given. ${stringify(message)}`);
}
}
} else {
return failed(`Invalid method name given. ${stringify(message)}`);
Expand Down
12 changes: 10 additions & 2 deletions utilities/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import chalk from 'chalk';
import {
stringify,
each,
isPlainObject
isPlainObject,
initialString
} from 'Acid';
const arrayNumberRegex = /\[([\d\s,]*?)\]/gm;
function truncateArray(match) {
return match.replace(/\s/gm, '');
}
function shortenArrays(item) {
return item.replace(arrayNumberRegex, truncateArray);
}
export function prettyObjects(args, consoleBase) {
return each(args, (item) => {
console.log(consoleBase((isPlainObject(item)) ? stringify(item, null, ` `) : item));
console.log(consoleBase((isPlainObject(item)) ? shortenArrays(stringify(item, null, ` `)) : item));
});
}
function logFactory(bg, color, header, footer) {
Expand Down

0 comments on commit 8a8250e

Please sign in to comment.