@@ -2,18 +2,100 @@ var dgram = require('dgram');
2
2
var message = new Buffer ( JSON . stringify ( { "+end" :"38666817e1b38470644e004b9356c1622368fa57" } ) ) ;
3
3
var socket = dgram . createSocket ( "udp4" ) ;
4
4
var ezcrypto = require ( '../ezcrypto-js/ezcrypto.js' ) . ezcrypto ;
5
+ var Mu = require ( '../Mu/lib/mu' ) ;
6
+ var fs = require ( 'fs' ) ;
7
+ var sqlite = require ( '../node-sqlite/sqlite' ) ;
8
+ var db = new sqlite . Database ( ) ;
9
+ var http = require ( 'http' ) ;
10
+ var parser = require ( 'url' ) . parse ;
11
+ Mu . templateRoot = './templates' ;
12
+ var index ;
13
+ var logic = {
14
+ servername : "Yahoo"
15
+ } ;
5
16
6
17
18
+ db . open ( "tweets.db" , function ( error ) {
19
+ if ( error ) {
20
+ console . log ( "Error opening database" ) ;
21
+ throw error ;
22
+ }
23
+ db . getTweets = function ( res ) {
24
+ db . execute ( "select name,message,timestamp from tweets join names on tweets.key = names.key" , function ( error , rows ) {
25
+ if ( error && error . message . search ( "no such table" ) != - 1 ) {
26
+ console . log ( error ) ; //throw error;
27
+ db . execute ( "CREATE TABLE tweets (key, message, timestamp)" , function ( ) { } ) ;
28
+ db . getTweets ( ) ;
29
+ }
30
+ //res.end(JSON.stringify(rows));
31
+ http . renderTweets ( res , rows ) ;
32
+ console . log ( rows ) ;
33
+ } ) ;
34
+ } ;
35
+ //db.getTweets();
36
+
37
+ } ) ;
7
38
8
- socket . sendData = function ( message ) { console . log ( message . toString ( ) ) ; this . send ( message , 0 , message . length , 42424 , "telehash.org" ) ; }
39
+ Mu . render ( 'index.html' , logic , { } , function ( err , output ) {
40
+ if ( err ) {
41
+ throw err ;
42
+ }
43
+
44
+ var buffer = '' ;
45
+
46
+ output . addListener ( 'data' , function ( c ) { buffer += c ; } )
47
+ . addListener ( 'end' , function ( ) { index = buffer ; } ) ;
48
+ } ) ;
49
+
50
+
51
+ http . renderTweets = function ( res , rows ) {
52
+ var tweetLogic = {
53
+ tweets : rows
54
+ } ;
55
+
56
+ Mu . render ( 'tweets.html' , tweetLogic , { } , function ( err , output ) {
57
+ if ( err ) {
58
+ throw err ;
59
+ }
60
+
61
+ var buffer = '' ;
62
+
63
+ output . addListener ( 'data' , function ( c ) { buffer += c ; } )
64
+ . addListener ( 'end' , function ( ) { res . end ( buffer ) } ) ;
65
+ } ) ;
66
+ } ;
67
+
68
+ http . createServer ( function ( req , res ) {
69
+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
70
+ var output = ( "An error occured" ) ;
71
+ var reqdict = parser ( req . url , true ) ;
72
+ var pathname = reqdict . pathname ;
73
+ console . log ( reqdict ) ;
74
+ if ( pathname == "/" ) {
75
+ output = index ;
76
+ }
77
+ if ( pathname == "/message" ) {
78
+ var data = reqdict . query . data
79
+ } ;
80
+ if ( pathname == "/getmessage" ) { } ;
81
+ if ( pathname == "/tweets" ) {
82
+ db . getTweets ( res ) ;
83
+ return ;
84
+ }
85
+
86
+ res . end ( output ) ;
87
+ } ) . listen ( 8080 , "127.0.0.01" ) ;
9
88
89
+ socket . sendData = function ( message ) { console . log ( message . toString ( ) ) ; this . send ( message , 0 , message . length , 42424 , "telehash.org" ) ; }
90
+ //socket.formatter = function(message){ new Buffer(JSON.stringify({".tap":[{"has":["+key"]}],"_line":ring, "_to":"208.68.163.247:42424"}));
10
91
socket . on ( "message" , function ( data , rinfo ) {
11
92
console . log ( data . toString ( ) ) ;
12
93
telex = JSON . parse ( data . toString ( ) ) ;
13
94
//console.log("TELEX: " + JSON.stringify(telex));
14
95
if ( telex [ "_ring" ] ) {
15
96
ring = telex [ "_ring" ] ;
16
97
console . log ( "LINE: " + ring ) ;
98
+ this . line = ring ;
17
99
var response = new Buffer ( JSON . stringify ( { ".tap" :[ { "has" :[ "+key" ] } ] , "_line" :ring , "_to" :"208.68.163.247:42424" } ) ) ;
18
100
this . sendData ( response ) ;
19
101
}
@@ -31,6 +113,11 @@ socket.on("message", function(data, rinfo){
31
113
//console.log(bool);
32
114
if ( test ) {
33
115
console . log ( "Key validates" ) ;
116
+ var timestamp = new Date ( ) . toString ( ) ;
117
+ db . execute ( "INSERT INTO tweets (key, message, timestamp) VALUES (?,?,?)" , [ key , message , timestamp ] , function ( error , rows ) {
118
+ if ( error ) throw error ;
119
+ } ) ;
120
+
34
121
} else {
35
122
console . log ( "Key doesn't validate" ) ;
36
123
}
@@ -40,11 +127,10 @@ socket.on("message", function(data, rinfo){
40
127
41
128
socket . on ( "listening" , function ( ) {
42
129
console . log ( "Now listening" ) ;
43
- socket . send ( message , 0 , message . length , 42424 , "telehash.org" ) ;
130
+ setTimeout ( function ( ) { socket . send ( message , 0 , message . length , 42424 , "telehash.org" ) ; socket . ping ( ) } , 1000 ) ;
44
131
45
132
} ) ;
46
133
47
134
socket . bind ( 12345 ) ;
48
-
49
-
50
-
135
+ var pingmsg = new Buffer ( JSON . stringify ( { "_line" :socket . line , "_to" :"208.68.163.247:42424" } ) ) ;
136
+ socket . ping = function ( ) { setTimeout ( function ( ) { socket . sendData ( pingmsg ) ; socket . ping ( ) ; } , 30000 ) } ;
0 commit comments