var net = require('net');
var mysql = require('mysql');

var sockets = [];
var severAddr = '140.115.152.224';
var severPort = 8080;
var supernodeAddr = '';
var supernodePort = ;

var flag = 0;

var svr = net.createServer(function(sock){
    console.log('Connected: ' + sock.remoteAddress + ': ' + sock.remotePort);
    sock.write('Hello ' + sock.remoteAddress + ': ' + sock.remotePort + '\n');
    sockets.push(sock);

    sock.on('data', function(data){
        var len = sockets.length;
        var msg = data.toString().split(" ");
        var action = msg[0];
        var id = msg[1];
        console.log('connection: ' + len);
        console.log('action: ' + action + ' id: ' + id); 
        
        //ask empty topic
        if(action == 'ASKTOPIC'){
            //connection
            var con = mysql.createConnection({
                host     : '140.115.189.142',
                user     : '',
                password : '',
                database : ''
            });
            con.connect(function(err){
                if(err)
                    console.log('Error connect to DB');
                console.log('Server connection established');
            });

            var sqlFindDevice = 'SELECT * FROM user_device WHERE device_id = ?';
            var sqlFindChannel = 'SELECT * FROM user_ichannel WHERE user_id = ?';
            var sqlUpdateChannel = 'UPDATE user_ichannel SET user_id = ? WHERE channel_id = ?';
            var sqlUpdateDevice = 'UPDATE user_device SET last_online = CURRENT_TIMESTAMP, online_status = TRUE WHERE user_id = ?';
            
            //query id is legal or illegal
            con.query(sqlFindDevice, [id], function(err, rows, fields){
                if(err)
                    throw err;
                if(rows.length > 0){
                    console.log('User is legal');
                    var user_id = rows[0].user_id;
                    //use topic or not
                    con.query(sqlFindChannel, user_id, function(err, rows){
                        if(err)
                            throw err;
                        if(rows.length > 0){
                            var channel_id = rows[0].channel_id;
                            //return to client
                            sock.write('TOPIC ' + channel_id);
                            //return to supernode
                            var supernode = new net.Socket();
                            supernode.connect(supernodePort, supernodeAddr, function(){
                                supernode.write('NEW ' + user_id + ' ' + channel_id);
                                supernode.destroy();
                                console.log('SuperNode connect');
                            });
                            supernode.on('close', function(){
                                console.log('Supernode connection close');    
                            });
                        }
                        else{
                            //find the empty channel
                            con.query(sqlFindChannel, [-1], function(err, rows){
                                if(err)
                                    throw err;
                                console.log('Channel is empty or not');
                                if(rows.length > 0){
                                    var channel_id = rows[0].channel_id;
                                    //return to client
                                    sock.write('TOPIC ' + channel_id);
                                    //return to supernode
                                    var supernode = new net.Socket();
                                    supernode.connect(supernodePort, supernodeAddr, function(){
                                        supernode.write('NEW ' + user_id + ' ' + channel_id);
                                        supernode.destroy();
                                        console.log('SuperNode connect');
                                    });
                                    supernode.on('close', function(){
                                        console.log('Supernode connection close');    
                                    });
                                    //update the user id of the channel
                                    con.query(sqlUpdateChannel, [user_id, channel_id], function(err, rows){
                                        if(err)
                                            throw err;
                                        console.log('Update channel');    
                                    });
                                    //update the online info of the device
                                    con.query(sqlUpdateDevice, [user_id], function(err, rows){
                                        if(err)
                                            throw err;    
                                    });
                                }
                                //BUSY
                                else
                                    sock.write('BUSY' + '\n');
                                con.end(function(err){});
                            });
                        }
                    });

                }
                else
                    sock.write('INVALID \n');
            });
        }
        else
            sock.write('INVALID \n');
    });
});

svr.listen(severPort, severAddr);
console.log('Server Creted at ' + severAddr + ': ' + severPort + '\n');


@@ -1331,32 +1331,106 @@
 E user_id = ?';%0A
+            var sqlFindTopic = 'SELECT * FROM IoT_channels WHERE id = ?';%0A
             var 
@@ -1501,32 +1501,32 @@
 hannel_id = ?';%0A
-
             var 
@@ -2250,32 +2250,275 @@
 %5B0%5D.channel_id;%0A
+                            con.query(sqlFindTopic, channel_id, function(err, rows)%7B%0A                                if(err)%0A                                    throw err;%0A                                var channel_topic = rows%5B0%5D.topic;%0A    
                 
@@ -2568,32 +2568,36 @@
                 
+    
 sock.write('TOPI
@@ -2602,34 +2602,37 @@
 PIC ' + channel_
-id
+topic
 );%0A             
@@ -2638,32 +2638,36 @@
                 
+  
+  
 //return to supe
@@ -2664,32 +2664,36 @@
 rn to supernode%0A
+    
                 
@@ -2730,32 +2730,36 @@
 w net.Socket();%0A
+    
                 
@@ -2854,32 +2854,36 @@
                 
+    
 supernode.write(
@@ -2944,32 +2944,36 @@
                 
+    
 supernode.destro
@@ -3001,32 +3001,36 @@
                 
+  
+  
 console.log('Sup
@@ -3067,36 +3067,44 @@
                 
-%7D);%0A
+    %7D);%0A    
                 
@@ -3173,32 +3173,36 @@
                 
+    
 console.log('Sup
@@ -3224,32 +3224,68 @@
 on close');    %0A
+                                %7D);%0A
                 

Change channel_id to channel_name