Advertisement
Guest User

Untitled

a guest
Apr 29th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. //var express=require('express');
  3. var utils=require('./utils');//not related to node, just a bunch of utility function
  4. var logger=require('./logger');//my version of console.log
  5. var EventEmitter = require('events');
  6.  
  7.  
  8.  
  9. function Client(socket){
  10.  
  11.     var that=this;
  12.  
  13.     that.socket=socket;
  14.     var counter=0;
  15.  
  16.  
  17.     socket.on("error", function(err) {
  18.         logger.logDate("Error:");
  19.         logger.logDate(err.stack)
  20.     })
  21.     var messageId=0;
  22.  
  23.  
  24.     socket.on('data',function(data){
  25.         logger.logDate('data received:',data);
  26.         logger.logDate('socket.id:',socket.id);
  27.  
  28.         var messages = data.split('\n');
  29.         messages.forEach(function(message){
  30.             if (message.trim() === '')
  31.                 return;
  32.             message = JSON.parse(message);
  33.             messageId=message.id;
  34.             ////////////////////
  35.             //subscribe
  36.            
  37.  
  38.             if(message.method=='mining.subscribe'){
  39.                 logger.logDate('subscription, sending response');
  40.                 that.sendJson({
  41.                     id : messageId,
  42.                     result : [
  43.                         [
  44.                             ["mining.set_difficulty", "b4b6693b72a50c7116db18d6497cac52"],
  45.                             ["mining.notify", socket.id.toString()] /*tak, musi być kurde toString, przynajmniej jeśli chodzi o cpuminer!*/
  46.                         ],
  47.                         "08000002", 4],
  48.                     error : null
  49.                 },function() {
  50.  
  51.                 });
  52.  
  53.             }
  54.  
  55.             ///////////////////
  56.             //authorize
  57.             if(message.method=='mining.authorize'){
  58.                 logger.logDate('authorizing: always true');
  59.                 that.workerIP = socket.address().address;
  60.                 logger.logDate('workerIP:',that.workerIP);
  61.                 that.workerName = message.params[0];
  62.                 that.workerPass = message.params[1];
  63.                 //options.authorizeFn(that.workerIP, that.workerName, that.workerPass, function(result)
  64.                
  65.                 that.authorized = true;
  66.                 that.sendJson({
  67.                     id : messageId,
  68.                     result : that.authorized,
  69.                     error : null
  70.                 },function(){
  71.                     logger.logDate('sending a job');
  72.                     that.sendJob();
  73.                 });
  74.  
  75.             };
  76.  
  77.             ////////////
  78.  
  79.             if(message.method=='mining.submit'){
  80.                 logger.logDate('submit:');
  81.  
  82.             }
  83.  
  84.             //
  85.             logger.logDate('message:',message);
  86.             function sendJobsLoop() {
  87.  
  88.             }
  89.  
  90.  
  91.  
  92.         })
  93.  
  94.  
  95.  
  96.     })    
  97.  
  98.     socket.on('end',function(){
  99.         logger.logDate('end of',socket.id);
  100.  
  101.     })  
  102.    
  103.  
  104.     this.sendJson=function (){
  105.         var callback=false;
  106.         var strJson = '';
  107.         for (var i = 0; i < arguments.length; i++){
  108.             if(utils.isFunction(arguments[i])) {
  109.                 callback = arguments[i];
  110.                 break;
  111.             }
  112.             strJson += JSON.stringify(arguments[i]) + '\n';
  113.  
  114.         }
  115.         that.socket.write(strJson,function(){
  116.             //logger.logDate('msg sent:',strJson)
  117.             if(callback)
  118.                 callback();
  119.         });
  120.     }
  121.     this.sendJob=function() {
  122.         that.sendJson({"params": ["konradp", "bf", "00000001", "504e86ed", "b2957c02"], "id": that.messageId, "method": "mining.submit"});
  123.         that.sendJson({"error": null, "id": messageId, "result": true});
  124.         //that.sendJson({"params": ["konradp", "bf", "00000001", "504e86ed", "b2957c02"], "id": that.messageId, "method": "mining.submit"},{"error": null, "id": messageId, "result": true});
  125.     }
  126. }
  127.  
  128. function Stratum() {
  129.     var counter=0;
  130.     var that=this;
  131.     var server = require("net").createServer();
  132.  
  133.     //var server = express.createServer();
  134.     var clients={};
  135.  
  136.  
  137.     server.listen(3333, '192.168.1.2');
  138.     var socket=null;
  139.  
  140.     server.on('connection',function(socket){
  141.  
  142.         logger.logDate('--------connection----------');
  143.         socket.setEncoding('utf8');//bo inaczej dane binarne
  144.         socket.id = Math.floor(Math.random() * 1000);//@todo: w przyszłości chyba jakiś incrementacyjny.
  145.  
  146.         var curTime = new Date();
  147.         curTime=curTime.getHours() + ":"  
  148.         + curTime.getMinutes() + ":"
  149.         + curTime.getSeconds();
  150.         logger.logDate('-----------');
  151.         logger.logDate('counter:',counter++);
  152.         logger.logDate('id:',socket.id);
  153.  
  154.         logger.logDate('time:',curTime);
  155.         clients[socket.id]=new Client(socket);
  156.  
  157.         //logger.logDate('all clients:',clients);
  158.         return;
  159.         //console.log(socket);
  160.         /*that.socket=socket;
  161.        
  162.         console.log('connected');
  163.         socket.on('data',function(data){
  164.             console.log('data:',typeof(data));return;
  165.             var messages = data.split('\n');  
  166.             messages.forEach(function(message){  
  167.                 if (message.trim() === '')
  168.                     return;
  169.                 message = JSON.parse(message);
  170.                 if(message.method=='mining.subscribe'){
  171.                     console.log('subscribtion, sending response');
  172.                     that.response({
  173.                         id : messageId,
  174.                         result : [
  175.                             [
  176.                                 ["mining.set_difficulty", socket.id], ["mining.notify", socket.id]
  177.                             ],
  178.                             "08000002", 4],
  179.                         error : null
  180.                     });
  181.                 }
  182.                 var curTime = new Date();
  183.                 curTime=curTime.getHours() + ":"  
  184.                 + curTime.getMinutes() + ":"
  185.                 + curTime.getSeconds();
  186.                 console.log('-----------');
  187.                 console.log('counter:',counter++);
  188.                 console.log('id:',socket.id);
  189.                                
  190.                 console.log('time:',curTime);
  191.                
  192.                 console.log('message:',message);               
  193.             })
  194.         })    */
  195.        
  196.        
  197.     })
  198.  
  199.     this.test = function test() {
  200.  
  201.         logger.logDate('Stratum');
  202.     }
  203.    
  204. }
  205.  
  206. Stratum.prototype.__proto__ = EventEmitter.prototype;
  207.  
  208. module.exports = Stratum;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement