Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MESSAGE_TYPES } from './src/modules/libp2p/messages.model'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import { EncodeStatus } from './src/modules/encoder.model'
import logUpdate from 'log-update';
import logger from 'node-color-log'
import cli from 'cli-ux'


Expand All @@ -28,10 +29,7 @@ const ClientKey =
}

void (async () => {


console.log('P2P interface starting up')

logger.info('P2P interface starting up')
const ceramic = new CeramicClient('https://ceramic-clay.3boxlabs.com') //Using the public node for now.
const idListener = await PeerId.createFromJSON(ClientKey)
const libp2p = await Libp2p.create({
Expand Down Expand Up @@ -80,21 +78,18 @@ void (async () => {

// start libp2p
await libp2p.start()
console.log(libp2p.peerId.toJSON())
setInterval(() => {
//console.log(libp2p.connections.size)
}, 5000)
logger.info('LibP2P Peer ID', libp2p.peerId.toJSON())
const handler = async ({ connection, stream, protocol }) => {
// use stream or connection according to the needs
console.log(connection, stream, protocol)
logger.info(connection, stream, protocol)
for await(let item of stream.source) {
console.log(item)
logger.info(item)
}
}

libp2p.handle('/spk-video-encoder/1.0.0', handler)
const output = await libp2p.dialProtocol("/ip4/10.0.1.188/tcp/14445/p2p/QmctF7GPunpcLu3Fn77Ypo657TA9GpTiipSDUUMoD2k5Kq", '/spk-video-encoder/1.0.0')
console.log(output)
logger.info(output)


cli.action.start('Encoding Video')
Expand All @@ -107,7 +102,7 @@ void (async () => {
void (async () => {
for await(let item of output.stream.source) {
const decodedMessage = decode(item._bufs[0])
console.log(decodedMessage)
logger.info(decodedMessage)
if(decodedMessage.type === MESSAGE_TYPES.RESPONE_ENCODE_JOB) {
encodeId = decodedMessage.streamId
pushable.push(encode({
Expand All @@ -119,7 +114,7 @@ void (async () => {
await encodeDoc.sync()
const contentData = encodeDoc.content as any;
if(contentData.status === EncodeStatus.COMPLETE) {
console.log(`Job complete, IPFS Hash is ${contentData.outCid}`)
logger.info(`Job complete, IPFS Hash is ${contentData.outCid}`)
cli.action.stop()
process.exit(0)
}
Expand All @@ -130,10 +125,9 @@ void (async () => {
const pushable = Pushable()
pipe(pushable, output.stream)
pushable.push(encode({

type: MESSAGE_TYPES.REQUEST_ENCODE_JOB,
ipfsHash: 'Qma9ZjjtH7fdLWSrMU43tFihvSN59aQdes7f5KW6vGGk6e'
}))
//pushable.end()
//console.log(output.stream.close())
//logger.info(output.stream.close())
})()
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"libp2p-tcp": "^0.17.2",
"moment": "^2.29.3",
"mongodb": "^4.2.2",
"node-color-log": "^10.0.2",
"node-graceful-shutdown": "^1.1.0",
"node-schedule": "^2.1.0",
"nodejs-file-downloader": "^4.9.3",
Expand Down
13 changes: 7 additions & 6 deletions src/api/gateway.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { BadRequestException, Body, HttpCode, HttpStatus, Post, Put, Query } from '@nestjs/common'
import { Controller, Get, Param } from '@nestjs/common'
import logger from 'node-color-log'
import { EncoderService } from '../modules/core/encoder.service'
import { JobStatus } from '../modules/encoder.model'
import { encoderContainer } from './index'
Expand All @@ -17,9 +18,9 @@ export class GatewayApiController {

@Post('/updateNode')
async updateNode(@Body() body) {
console.log(body)
logger.info(body)
const { payload, did } = await unwrapJWS(body.jws)
console.log(payload, did)
logger.info(payload, did)
encoderContainer.self.gateway.updateNode(did, payload.node_info)
}

Expand All @@ -46,7 +47,7 @@ export class GatewayApiController {

@Get('/nodeJobs/:nodeId')
async nodeJobs(@Param('nodeId') nodeId) {
console.log(nodeId)
logger.info(nodeId)
}

/**
Expand All @@ -69,7 +70,7 @@ export class GatewayApiController {
@Post('/acceptJob')
async acceptJob(@Body() body) {
const { kid, payload, did } = await unwrapJWS(body.jws)
console.log(kid, payload, did)
logger.info(kid, payload, did)


await encoderContainer.self.gateway.acceptJob(payload.job_id, did)
Expand All @@ -88,7 +89,7 @@ export class GatewayApiController {

@Post('/failJob')
async failJob(@Body() body) {
console.log(body)
logger.info(body)
const {payload, did} = await unwrapJWS(body.jws)

await encoderContainer.self.gateway.rejectJob(payload.job_id, did)
Expand Down Expand Up @@ -148,7 +149,7 @@ export class GatewayApiController {
@Get('/sync')
async syncFile(@Param('job_id') job_id) {

console.log(job_id)
logger.info(job_id)
}

@Get('jobstatus/:job_id')
Expand Down
3 changes: 2 additions & 1 deletion src/api/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from "moment";
import { encoderContainer } from ".."
import { peerList } from "../../common/utils";
import {JobReason, JobStatus} from '../../modules/encoder.model'
import logger from 'node-color-log'

class JobInfo {
data: any;
Expand Down Expand Up @@ -102,7 +103,7 @@ export const Resolvers = {
}
}

//console.log('preferred_nodes', preferred_nodes, scoreMap)
//logger.info('preferred_nodes', preferred_nodes, scoreMap)

const nodeInfo = await encoderContainer.self.gateway.clusterNodes.findOne({
node_id: node_id
Expand Down
7 changes: 4 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Axios from 'axios'
import logger from 'node-color-log'

/**
* @template {Object} T
Expand Down Expand Up @@ -99,15 +100,15 @@ const ndjsonParse = async function* (stream) {
const stream = response.data;

// stream.on('data', data => {
// console.log(data);
// logger.info(data);
// });

// stream.on('end', () => {
// console.log("stream done");
// logger.info("stream done");
// });
let infos = []
for await (const d of ndjsonParse(stream)) {
// console.log(JSON.parse(d.toString()))
// logger.info(JSON.parse(d.toString()))
infos.push(toClusterInfo(d))
}
return infos
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CeramicClient} from '@ceramicnetwork/http-client'
import { onShutdown } from "node-graceful-shutdown";
import logger from 'node-color-log'
import { ConfigService } from './config.service'
// import IPFSHTTP from 'ipfs-http-client'
import { CoreService } from './modules/core/core.service'
Expand All @@ -12,14 +13,13 @@ async function startup(): Promise<void> {
// init ceramic
const ceramic = new CeramicClient(ConfigService.getConfig().ceramicHost) //Using the public node for now.


instance = new CoreService(ceramic)
await instance.start()

const api = new EncoderApiModule(4005, instance)
await api.listen()
} catch (ex) {
console.log(ex.message)
logger.error(ex.message)
await instance.stop()
process.exit(0)
}
Expand All @@ -30,13 +30,13 @@ void startup()


process.on('unhandledRejection', (error: Error) => {
console.log('unhandledRejection', error)
logger.error('unhandledRejection', error)
})


onShutdown(async () => {
console.log('Video encoder stopping... ')
logger.info('Video encoder stopping... ')
await instance.stop()
console.log('Exit');
logger.info('Exit');

});
Loading