Skip to content

Commit 9c18f89

Browse files
58 ipfs support system (#60)
* ADD IPFS TO UTIL AND PUT/GET * add msg 2 websocket demo to test cid using awesome * add cat to ipfs util * working websocket to ipfs
1 parent e8778aa commit 9c18f89

File tree

7 files changed

+5947
-61
lines changed

7 files changed

+5947
-61
lines changed

examples/websocket_demo.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { WebSocket } from "ws";
22

3-
const socket = new WebSocket("ws://localhost:8082");
3+
var socket = new WebSocket("ws://localhost:8082");
44
const entities = [];
55
const components = [];
66

7+
78
socket.onmessage = (event) => {
89
const message = JSON.parse(event.data);
910
console.log(message);
@@ -36,6 +37,21 @@ setInterval(()=> {
3637
socket.send(JSON.stringify({ type: "getAllComponents" }));
3738
}, 5000);
3839

40+
41+
setInterval(()=> {
42+
socket.send(JSON.stringify({ type: "saveData" , componentName: 'IPFScid', data: "this is a test!" }));
43+
}, 3000);
44+
45+
//lazy reload of websocket if I crash the server
46+
setInterval(()=> {
47+
//console.log(socket.State);
48+
try {
49+
//if (socket.State == WebSocketState.Close) {socket = new WebSocket("ws://localhost:8082")}
50+
//console.log(socket.State);
51+
} catch (error) {
52+
console.log(error)
53+
}
54+
}, 5000)
3955
/*
4056
setTimeout(()=> {
4157
socket.send(JSON.stringify({ type: "createObjective", data: "Write the worlds greatest robot detective novel!" }));

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"dependencies": {
2323
"crypto-js": "^4.1.1",
2424
"dotenv": "^16.0.3",
25+
"ipfs": "^0.66.0",
26+
"ipfs-core": "^0.18.0",
2527
"natural": "^6.2.0",
2628
"node-fetch": "^3.3.1",
2729
"openai": "^3.2.1",

src/components/active/IPFScid.mjs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CHIPPRAGI } from "../../index.js";
2+
import { SchemaFieldTypes } from "redis";
3+
4+
CHIPPRAGI.registerComponent('IPFScid', {
5+
schema:{
6+
'$.fileCID': {
7+
type: SchemaFieldTypes.TEXT,
8+
AS: 'cid'
9+
},
10+
},
11+
12+
info: {
13+
version : "0.1.0",
14+
license : "APACHE-2.0",
15+
developer: "CHIPPRBOTS",
16+
multi: true,
17+
description : "This component stores a files cid on the ipfs network",
18+
},
19+
});

src/core/Util/Util.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import CryptoJS from 'crypto-js';
2+
import * as ipfs from 'ipfs';
23

34
export class Utility {
45
constructor(chipprConfig) {
6+
this.node;
7+
this.init();
8+
}
9+
10+
async init() {
11+
this.node = await ipfs.create();
12+
//console.log(this.node)
513
}
614

715
getHashId( _dataToHash ){
@@ -40,6 +48,25 @@ export class Utility {
4048
},
4149
});
4250
};
51+
52+
async storeData( data ){
53+
// takes a data object in and returns a cid string
54+
const results = await this.node.add(data)
55+
//do something with results maybe
56+
return results;
57+
}
58+
59+
async getData( cid ){
60+
for await (const buf of this.node.get(cid)) {
61+
return buf;
62+
}
63+
}
64+
65+
async readData( cid ){
66+
for await (const ary of this.node.cat(cid)) {
67+
return ary;
68+
}
69+
}
4370
}
4471

4572

src/systems/active/AmazingSystem.mjs

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ CHIPPRAGI.registerSystem('AmazingSystem', {
55
version : "0.1.0",
66
license : "APACHE-2.0",
77
developer: "CHIPPRBOTS",
8+
type: "core",
89
description : "This is an system that can solve any problem.",
910
},
1011

1112
init: function () {
12-
//NOTE THIS IS FOR TESTING THE SYSTEM SELECTOR ONLY
13-
CHIPPRAGI.subscribe('SYSTEM', (type, eventData) => {
14-
//if( type == 'systemSelected' ) //CHIPPRAGI.Logger.debug({log:`AMAZING SYSTEM WAS CORRECTLY SELECTED!`, system: 'amazingSystem'});
15-
});
13+
//NOTE THIS IS FOR TESTING THE SYSTEM ONLY
14+
CHIPPRAGI.subscribe('UPDATE', (eventData) => {this.update(eventData)});
1615
},
1716

18-
update: function (eventData) {
17+
update: async function (message) {
1918
// Do something when the component's data is updated, if needed.
2019
// entityId is the ID of the entity this component is attached to.
2120
// componentData contains the updated data for the component.
21+
let eventData = JSON.parse(message);
22+
if (eventData.eventType === 'saveData') {
23+
let cid = await CHIPPRAGI.Util.storeData(eventData.payload.data);
24+
CHIPPRAGI.Logger.error({ systemName: 'awesome system', log: cid});
25+
let file = await CHIPPRAGI.Util.readData(cid.path);
26+
CHIPPRAGI.Logger.error({ systemName: 'awesome system', log: file.toString('utf8')});
27+
}
2228
},
2329

2430
remove: function (eventData) {

src/systems/active/WebsocketServerSystem.mjs

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ CHIPPRAGI.registerSystem("WebsocketServerSystem", {
6565
break;
6666
default:
6767
//dump on the message bus
68-
CHIPPRAGI.MessageBus.publish('UPDATE', message);
68+
let entityID = parsedMessage.entityID || null;
69+
let componentName = parsedMessage.componentName || 'websocket';
70+
let sourceSystem = parsedMessage.sourceSystem || 'WebsocketServerSystem';
71+
let data = parsedMessage.data || {};
72+
CHIPPRAGI.MessageBus.updateMessage( parsedMessage.type, entityID, componentName, sourceSystem, data );
6973
}
7074
},
7175

0 commit comments

Comments
 (0)