Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- //var express=require('express');
- var utils=require('./utils');//not related to node, just a bunch of utility function
- var logger=require('./logger');//my version of console.log
- var EventEmitter = require('events');
- function Client(socket){
- var that=this;
- that.socket=socket;
- var counter=0;
- socket.on("error", function(err) {
- logger.logDate("Error:");
- logger.logDate(err.stack)
- })
- var messageId=0;
- socket.on('data',function(data){
- logger.logDate('data received:',data);
- logger.logDate('socket.id:',socket.id);
- var messages = data.split('\n');
- messages.forEach(function(message){
- if (message.trim() === '')
- return;
- message = JSON.parse(message);
- messageId=message.id;
- ////////////////////
- //subscribe
- if(message.method=='mining.subscribe'){
- logger.logDate('subscription, sending response');
- that.sendJson({
- id : messageId,
- result : [
- [
- ["mining.set_difficulty", "b4b6693b72a50c7116db18d6497cac52"],
- ["mining.notify", socket.id.toString()] /*tak, musi być kurde toString, przynajmniej jeśli chodzi o cpuminer!*/
- ],
- "08000002", 4],
- error : null
- },function() {
- });
- }
- ///////////////////
- //authorize
- if(message.method=='mining.authorize'){
- logger.logDate('authorizing: always true');
- that.workerIP = socket.address().address;
- logger.logDate('workerIP:',that.workerIP);
- that.workerName = message.params[0];
- that.workerPass = message.params[1];
- //options.authorizeFn(that.workerIP, that.workerName, that.workerPass, function(result)
- that.authorized = true;
- that.sendJson({
- id : messageId,
- result : that.authorized,
- error : null
- },function(){
- logger.logDate('sending a job');
- that.sendJob();
- });
- };
- ////////////
- if(message.method=='mining.submit'){
- logger.logDate('submit:');
- }
- //
- logger.logDate('message:',message);
- function sendJobsLoop() {
- }
- })
- })
- socket.on('end',function(){
- logger.logDate('end of',socket.id);
- })
- this.sendJson=function (){
- var callback=false;
- var strJson = '';
- for (var i = 0; i < arguments.length; i++){
- if(utils.isFunction(arguments[i])) {
- callback = arguments[i];
- break;
- }
- strJson += JSON.stringify(arguments[i]) + '\n';
- }
- that.socket.write(strJson,function(){
- //logger.logDate('msg sent:',strJson)
- if(callback)
- callback();
- });
- }
- this.sendJob=function() {
- that.sendJson({"params": ["konradp", "bf", "00000001", "504e86ed", "b2957c02"], "id": that.messageId, "method": "mining.submit"});
- that.sendJson({"error": null, "id": messageId, "result": true});
- //that.sendJson({"params": ["konradp", "bf", "00000001", "504e86ed", "b2957c02"], "id": that.messageId, "method": "mining.submit"},{"error": null, "id": messageId, "result": true});
- }
- }
- function Stratum() {
- var counter=0;
- var that=this;
- var server = require("net").createServer();
- //var server = express.createServer();
- var clients={};
- server.listen(3333, '192.168.1.2');
- var socket=null;
- server.on('connection',function(socket){
- logger.logDate('--------connection----------');
- socket.setEncoding('utf8');//bo inaczej dane binarne
- socket.id = Math.floor(Math.random() * 1000);//@todo: w przyszłości chyba jakiś incrementacyjny.
- var curTime = new Date();
- curTime=curTime.getHours() + ":"
- + curTime.getMinutes() + ":"
- + curTime.getSeconds();
- logger.logDate('-----------');
- logger.logDate('counter:',counter++);
- logger.logDate('id:',socket.id);
- logger.logDate('time:',curTime);
- clients[socket.id]=new Client(socket);
- //logger.logDate('all clients:',clients);
- return;
- //console.log(socket);
- /*that.socket=socket;
- console.log('connected');
- socket.on('data',function(data){
- console.log('data:',typeof(data));return;
- var messages = data.split('\n');
- messages.forEach(function(message){
- if (message.trim() === '')
- return;
- message = JSON.parse(message);
- if(message.method=='mining.subscribe'){
- console.log('subscribtion, sending response');
- that.response({
- id : messageId,
- result : [
- [
- ["mining.set_difficulty", socket.id], ["mining.notify", socket.id]
- ],
- "08000002", 4],
- error : null
- });
- }
- var curTime = new Date();
- curTime=curTime.getHours() + ":"
- + curTime.getMinutes() + ":"
- + curTime.getSeconds();
- console.log('-----------');
- console.log('counter:',counter++);
- console.log('id:',socket.id);
- console.log('time:',curTime);
- console.log('message:',message);
- })
- }) */
- })
- this.test = function test() {
- logger.logDate('Stratum');
- }
- }
- Stratum.prototype.__proto__ = EventEmitter.prototype;
- module.exports = Stratum;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement