This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on a basic client/server communication system. In DIRE need o…
…f a refactor.
- Loading branch information
Daniel
committed
Dec 1, 2009
1 parent
dc209c0
commit cec61f6
Showing
4 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |