// ---------------------------------------------------------------- // Carivore Client Object v 0.2 // // Provides abstract mechanism to allow flash clients to connect to // CarnivorePE. // ---------------------------------------------------------------- // Created by Branden J. Hall // November 12, 2001 // Modified by Jeffrey Crouse // July 8, 2003 // ---------------------------------------------------------------- // // Use: // // 1. Create an instance of the object: // // foo = new Object.Carnivore(ip, port, channel); // // 2. Override the onPacket method, this will get called when packets arrive: // // foo.onPacket = function( packet ){ // trace("got a packet!"); // } // // The object will also automatically run onError if a connection // error occurs and onClose if the connection is aborted. // // There are also two internal "switches", debug and parseDates. // If debug is set to true, debug information will be printed to the output window // If parseDates is set to true the onPacket function will be pased an actual Date // object rather than the string it would normally recieve // // ---------------------------------------------------------------- // // Copyright (C) July 2003 by Jeffrey Crouse // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General // Public License for more details. // //------------------------------------------------------------------ // Keep track of how many packets we've recieved; var numPackets = 0; // Carnivore constructor Object.Carnivore = function(ip, port, channel){ this.state = "disconnected"; this.ip = ip; this.port = port; this.channel = channel; this.debug = false; this._super(); this.packets = new Array(); } // Packet constructor Object.packet = function(id, time, senderIP, senderPort, receiverIP, receiverPort, content, raw){ this.id = id; this.time = time; this.senderIP = senderIP; this.senderPort = senderPort; this.receiverIP = receiverIP; this.receiverPort = receiverPort; this.content = content; this.raw = raw; } // Connect method Object.Carnivore.prototype.connect = function(){ if (this.debug) trace("[carnivore] Connecting..."); this._connect(this.ip, this.port); this.state = "connecting"; } // Connection handler Object.Carnivore.prototype.onConnect = function(ok){ // If connection is ok, join specified channel if (ok){ if (this.debug){ trace("[carnivore] Connection established"); } this.state = "join"; // If not run onError callback and pass it an error object }else{ var e = new Object(); e.type = "connection"; e.description = "Could not connect to server"; this.onError(e); if (this.debug) trace("[carnivore] Error: Could not connect to server"); this.state = "disconnected"; } } // Server data handler Object.Carnivore.prototype.onData = function(raw){ // deal with incoming data in the appropriate way. switch( this.state ) { // deal with actual packet data case "data": part = raw.split(" "); // Make sure that the data is coming from Carnivore if( part[0].substring(0, 13) == ":CarnivorePE:" ) { id = numPackets++;; t = part[0].substring(13).split(":"); time = t.join(":"); if (this.debug) trace( "[carnivore] Packet "+ id +" recieved at "+ time ); if( this.parseDates ) { time = new Date(); time.setHours( t[0] ); time.setMinutes( t[1] ); time.setSeconds( t[2] ); } sender = part[1].split("."); senderPort = sender.pop(); senderIP = sender.join("."); receiver = part[3].split("."); receiverPort = parseInt( receiver.pop() ); receiverIP = receiver.join("."); content = part[5]; if (this.debug) { trace("\tSender: " + senderIP + " Port: " + senderPort); trace("\tReciever: " + receiverIP + " Port: " + receiverPort); if(this.channel != "minivore") trace("\tPacket: " + packet ); } // Construct a packet object with the packet data. newPacket = new Object.packet(id, time, senderIP, senderPort, receiverIP, receiverPort, content, raw); // Add the packet to the end of the array. this.packets.push( newPacket ); // Send the most recent packet to the listener function. this.onPacket( newPacket ); } else if (this.debug) trace("[carnivore] Received invalid packet"); break; case "join": // Attempt to join specified channel. this.send("JOIN #"+this.channel+"\n"); if (this.debug) trace("[carnivore] Joining #"+this.channel+" channel"); this.state = "joined"; break; case "joined": // If we are allowed to join the channel, start getting packets. if( raw.substring(0,6) == "JOINED" ) { if (this.debug) trace("[carnivore] Join successful."); if (this.debug) trace("[carnivore] Ready to receive packets"); this.state = "data"; } else { var e = new Object(); e.type = "join"; e.description = "Could not join channel " + this.channel; this.onError(e); if (this.debug) trace("[carnivore] Error: Could not join channel " + this.channel); this.state = "disconnected"; } break; } } // Handle of the gee-wiz inheritence stuff Object.Carnivore.prototype.__proto__ = XMLSocket.prototype; Object.Carnivore.prototype._super = XMLSocket.prototype.constructor; Object.Carnivore.prototype._connect = XMLSocket.prototype.connect; Object.Carnivore.prototype._close = XMLSocket.prototype.close; Object.Carnivore.prototype._send = XMLSocket.prototype.send;