1 /**
  2  * 2011 Peter 'Pita' Martischka
  3  *
  4  * Licensed under the Apache License, Version 2.0 (the "License");
  5  * you may not use this file except in compliance with the License.
  6  * You may obtain a copy of the License at
  7  *
  8  *      http://www.apache.org/licenses/LICENSE-2.0
  9  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS-IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 
 17 var http = require('http')
 18   , url = require('url')
 19   , fs = require('fs')
 20   , io = require('socket.io')
 21   , sys = require('sys')
 22   , server;
 23 
 24 server = http.createServer(function(req, res){
 25   var path = url.parse(req.url).pathname;
 26   
 27   if(path.substring(0,"/static".length) == "/static" || path.substring(0,"/p/".length) == "/p/")
 28   {
 29     if(path.substring(0,"/p/".length) == "/p/")
 30     {
 31       if(path.length < 7)
 32         send404(res, path);
 33     
 34       path = "/static/padhtml";
 35     }
 36     
 37     sendFile(res, path, __dirname + "/.." + path);
 38   }
 39   else if(path == "/")
 40   {
 41     sendRedirect(res, path, "/p/test");
 42   }
 43   else if(path == "/newpad")
 44   {
 45     sendRedirect(res, path, "/p/" + randomPadName());
 46   }
 47   else if(path == "/ep/pad/reconnect")
 48   {
 49     if(req.headers.referer != null)
 50       sendRedirect(res, path, req.headers.referer);
 51     else
 52       send404(res, path);
 53   }
 54   else
 55   {
 56     send404(res, path);
 57   }
 58 });
 59 server.listen(9001);
 60 
 61 function randomPadName() {
 62 	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
 63 	var string_length = 10;
 64 	var randomstring = '';
 65 	for (var i=0; i<string_length; i++) {
 66 		var rnum = Math.floor(Math.random() * chars.length);
 67 		randomstring += chars.substring(rnum,rnum+1);
 68 	}
 69 	return randomstring;
 70 }
 71 
 72 function sendFile(res, reqPath, path)
 73 {
 74   fs.readFile(path, function(err, data){
 75     if (err){
 76       send404(res, reqPath);
 77     } else {
 78       var contentType = "text/html";
 79     
 80       if (path.substring(path.length -3, path.length) == ".js")
 81         contentType = "text/javascript";
 82       else if (path.substring(path.length -4, path.length) == ".css")
 83         contentType = "text/css";
 84       else if (path.substring(path.length -4, path.length) == ".gif")
 85         contentType = "image/gif";
 86     
 87       res.writeHead(200, {'Content-Type': contentType});
 88       res.write(data, 'utf8');
 89       res.end();
 90       
 91       requestLog(200, reqPath, "-> " + path);
 92     }
 93   });
 94 }
 95 
 96 function send404(res, reqPath)
 97 {
 98   res.writeHead(404);
 99   res.write("404 - Not Found");
100   res.end();
101   
102   requestLog(404, reqPath, "NOT FOUND!");
103 }
104 
105 function sendRedirect(res, reqPath, location)
106 {
107   res.writeHead(302, {'Location': location});
108   res.end();
109   
110   requestLog(302, reqPath, "-> " + location);
111 }
112 
113 function requestLog(code, path, desc)
114 {
115   console.log(code +", " + path + ", " + desc);
116 }
117 
118 var io = io.listen(server);
119 var messageHandler = require("./MessageHandler");
120 messageHandler.setSocketIO(io);
121 
122 io.on('connection', function(client){
123   try{
124     messageHandler.handleConnect(client);
125   }catch(e){console.error(e);}
126   
127   client.on('message', function(message){
128     try{
129       messageHandler.handleMessage(client, message);
130     }catch(e){console.error(e);}
131   });
132 
133   client.on('disconnect', function(){
134     try{
135       messageHandler.handleDisconnect(client);
136     }catch(e){console.error(e);}
137   });
138 });
139 
140 
141 
142 
143