Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Working on a basic client/server communication system. In DIRE need o…
Browse files Browse the repository at this point in the history
…f a refactor.
  • Loading branch information
Daniel committed Dec 1, 2009
1 parent dc209c0 commit cec61f6
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 10 deletions.
68 changes: 68 additions & 0 deletions client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'rubygems'
require 'eventmachine'
require 'socket'
require 'json'

module Sails

class ClientConnection < EventMachine::Connection
attr_accessor :port, :ip, :username, :address

def initialize username, password
super

send_object 'login', {'user' => username, 'pass' => password}

sleep 0.2
@port, @ip = Socket.unpack_sockaddr_in get_peername
puts "connected to #{@ip}:#{@port}"
@buffer = ''
end

def request_wave_info wave_id
send_object 'wave_info', {'wave_id' => wave_id}
end

def receive_data data
@buffer += data
while @buffer.include? "\n"
got_line @buffer.slice!(0, @buffer.index("\n")+1).chomp
end
end

def send_object action, hash
hash['action'] = action
send_data hash.to_json + "\n"
end

def got_line line
data = JSON.parse line

action = data['action']
case action
when 'loggin'
puts "Logged in."
@username = data['username']
@address = data['address']

when 'wavelist'
puts "Got list of waves:"
puts data['wave_ids']
data['wave_ids'].each do |id|
request_wave_info id
end

else
p data
end
end

def unbind
puts "connection closed to #{@ip}:#{@port}"
end
end # class
end # module

EM.run do
EM.connect "127.0.0.1", 7849, Sails::ClientConnection, 'danopia', 'test'
end
20 changes: 10 additions & 10 deletions database.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defaults: &defaults
adapter: mysql
encoding: utf8
username: sails
username: sails2
password: JapmMF6CYR7wvhHd
host: localhost
host: danopia

#development:
#<<: *defaults
#database: sails_dev
development:
<<: *defaults
database: sails_dev

production:
<<: *defaults
Expand All @@ -19,11 +19,11 @@ production:

# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
#development:
#adapter: sqlite3
#database: db/development.sqlite3
#pool: 5
#timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
Expand Down
41 changes: 41 additions & 0 deletions run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rubygems'
require 'hpricot'

require 'server'

require 'pp'
require 'yaml'

require 'sails'

#require 'agents/echoey'

Sails::Utils.connect_db

#trap("INT") do
# provider.remote.stop_service
# puts 'OBAI'
# exit
#end

#Thread.new do
#provider.send_data ' ' while sleep 60
#end

puts 'Entering program loop'

require 'lib/xmpp/packet'
require 'lib/xmpp/connection'
require 'lib/xmpp/component'
require 'lib/xmpp/waveserver'

EventMachine.run {
provider = Sails::XMPP::WaveServer.load_and_connect 'sails.conf'

if provider.config['ping']
puts "Sending a ping to #{provider.config['ping']} due to configuration."
Sails::Server.new(provider, provider.config['ping'], provider.config['ping'], false)
end

EM.start_server "127.0.0.1", 7849, Sails2::Server, provider
}
109 changes: 109 additions & 0 deletions server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'rubygems'
require 'eventmachine'
require 'socket'
require 'json'

module Sails2

class Client
attr_accessor :username, :address, :record, :server, :connected_at, :last_action

def initialize record, server=nil,a=nil,b=nil # TODO: Database
@username = a#record.login
@address = b#record.address
@record = record
@server = server
@connected_at = Time.now
@last_action = Time.now
end

def self.check_login username, password
%w{danopia test osirisx loonacy l3reak eggy}.include?(username) &&
%W{password password1 12345678 test}.include?(password)
end

def self.login username, password, server=nil
if check_login username, password
Client.new nil, server, username, "#{username}@danopia.net"
else
nil # TODO: Raise error
end
end
end

class Server < EventMachine::Connection
attr_accessor :port, :ip, :name, :client, :provider
INSTANCES = []

def initialize provider
@provider = provider
@buffer = ''
#@clients = []

INSTANCES << self

sleep 0.2
@port, @ip = Socket.unpack_sockaddr_in get_peername
puts "connection from #{@ip}:#{@port}"
end

def receive_data data
@buffer += data
while @buffer.include? "\n"
got_line @buffer.slice!(0, @buffer.index("\n")+1).chomp
end
end

def send_object action, hash
hash['action'] = action
send_data hash.to_json + "\n"
end

def send_wave_list amount=50, page=1 # TODO: Use limits
waves = @provider.servers.values.uniq.map {|server| server.waves.keys}.flatten
send_object 'wavelist', {'wave_ids' => waves}
end

def got_line line
data = JSON.parse line
action = data['action']

case action
when 'login'
@client = Client.login data['user'], data['pass'], self
if @client
puts "#{@client.address} logged in (#{data['pass']})."
send_object 'loggedin', {'username' => @client.username, 'address' => @client.address}
send_wave_list
else
puts "Bad login from #{@ip}:#{@port}... #{data['user']}:#{data['pass']}"
end

when 'wave_info'
wave = @provider[data['wave_id']]
puts wave.inspect[0,100]

blips = {}
wave.blips.each_pair do |id, blip|
blips[id] = blip.digest
end

send_object 'wave_info', {
'id' => wave.name,
'participants' => wave.participants.map {|user| user.to_s},
'blips' => blips
}
end
end

def unbind
puts "connection closed from #{@ip}:#{@port}"
INSTANCES.delete self
end
end # class
end # module

#EM.run do
# EM.start_server "127.0.0.1", 7849, Sails::Server
# puts "server started"
#end

0 comments on commit cec61f6

Please sign in to comment.