Skip to content
This repository
Browse code

Working on a basic client/server communication system. In DIRE need o…

…f a refactor.
  • Loading branch information...
commit cec61f6a18d8fabd19e4196da52beeb327a19cee 1 parent dc209c0
Daniel Lamando authored

Showing 4 changed files with 228 additions and 10 deletions. Show diff stats Hide diff stats

  1. +68 0 client.rb
  2. +10 10 database.yml
  3. +41 0 run.rb
  4. +109 0 server.rb
68 client.rb
... ... @@ -0,0 +1,68 @@
  1 +require 'rubygems'
  2 +require 'eventmachine'
  3 +require 'socket'
  4 +require 'json'
  5 +
  6 +module Sails
  7 +
  8 +class ClientConnection < EventMachine::Connection
  9 + attr_accessor :port, :ip, :username, :address
  10 +
  11 + def initialize username, password
  12 + super
  13 +
  14 + send_object 'login', {'user' => username, 'pass' => password}
  15 +
  16 + sleep 0.2
  17 + @port, @ip = Socket.unpack_sockaddr_in get_peername
  18 + puts "connected to #{@ip}:#{@port}"
  19 + @buffer = ''
  20 + end
  21 +
  22 + def request_wave_info wave_id
  23 + send_object 'wave_info', {'wave_id' => wave_id}
  24 + end
  25 +
  26 + def receive_data data
  27 + @buffer += data
  28 + while @buffer.include? "\n"
  29 + got_line @buffer.slice!(0, @buffer.index("\n")+1).chomp
  30 + end
  31 + end
  32 +
  33 + def send_object action, hash
  34 + hash['action'] = action
  35 + send_data hash.to_json + "\n"
  36 + end
  37 +
  38 + def got_line line
  39 + data = JSON.parse line
  40 +
  41 + action = data['action']
  42 + case action
  43 + when 'loggin'
  44 + puts "Logged in."
  45 + @username = data['username']
  46 + @address = data['address']
  47 +
  48 + when 'wavelist'
  49 + puts "Got list of waves:"
  50 + puts data['wave_ids']
  51 + data['wave_ids'].each do |id|
  52 + request_wave_info id
  53 + end
  54 +
  55 + else
  56 + p data
  57 + end
  58 + end
  59 +
  60 + def unbind
  61 + puts "connection closed to #{@ip}:#{@port}"
  62 + end
  63 +end # class
  64 +end # module
  65 +
  66 +EM.run do
  67 + EM.connect "127.0.0.1", 7849, Sails::ClientConnection, 'danopia', 'test'
  68 +end
20 database.yml
... ... @@ -1,13 +1,13 @@
1 1 defaults: &defaults
2 2 adapter: mysql
3 3 encoding: utf8
4   - username: sails
  4 + username: sails2
5 5 password: JapmMF6CYR7wvhHd
6   - host: localhost
  6 + host: danopia
7 7
8   -#development:
9   - #<<: *defaults
10   - #database: sails_dev
  8 +development:
  9 + <<: *defaults
  10 + database: sails_dev
11 11
12 12 production:
13 13 <<: *defaults
@@ -19,11 +19,11 @@ production:
19 19
20 20 # SQLite version 3.x
21 21 # gem install sqlite3-ruby (not necessary on OS X Leopard)
22   -development:
23   - adapter: sqlite3
24   - database: db/development.sqlite3
25   - pool: 5
26   - timeout: 5000
  22 +#development:
  23 + #adapter: sqlite3
  24 + #database: db/development.sqlite3
  25 + #pool: 5
  26 + #timeout: 5000
27 27
28 28 # Warning: The database defined as "test" will be erased and
29 29 # re-generated from your development database when you run "rake".
41 run.rb
... ... @@ -0,0 +1,41 @@
  1 +require 'rubygems'
  2 +require 'hpricot'
  3 +
  4 +require 'server'
  5 +
  6 +require 'pp'
  7 +require 'yaml'
  8 +
  9 +require 'sails'
  10 +
  11 +#require 'agents/echoey'
  12 +
  13 +Sails::Utils.connect_db
  14 +
  15 +#trap("INT") do
  16 +# provider.remote.stop_service
  17 +# puts 'OBAI'
  18 +# exit
  19 +#end
  20 +
  21 +#Thread.new do
  22 + #provider.send_data ' ' while sleep 60
  23 +#end
  24 +
  25 +puts 'Entering program loop'
  26 +
  27 +require 'lib/xmpp/packet'
  28 +require 'lib/xmpp/connection'
  29 +require 'lib/xmpp/component'
  30 +require 'lib/xmpp/waveserver'
  31 +
  32 +EventMachine.run {
  33 + provider = Sails::XMPP::WaveServer.load_and_connect 'sails.conf'
  34 +
  35 + if provider.config['ping']
  36 + puts "Sending a ping to #{provider.config['ping']} due to configuration."
  37 + Sails::Server.new(provider, provider.config['ping'], provider.config['ping'], false)
  38 + end
  39 +
  40 + EM.start_server "127.0.0.1", 7849, Sails2::Server, provider
  41 +}
109 server.rb
... ... @@ -0,0 +1,109 @@
  1 +require 'rubygems'
  2 +require 'eventmachine'
  3 +require 'socket'
  4 +require 'json'
  5 +
  6 +module Sails2
  7 +
  8 +class Client
  9 + attr_accessor :username, :address, :record, :server, :connected_at, :last_action
  10 +
  11 + def initialize record, server=nil,a=nil,b=nil # TODO: Database
  12 + @username = a#record.login
  13 + @address = b#record.address
  14 + @record = record
  15 + @server = server
  16 + @connected_at = Time.now
  17 + @last_action = Time.now
  18 + end
  19 +
  20 + def self.check_login username, password
  21 + %w{danopia test osirisx loonacy l3reak eggy}.include?(username) &&
  22 + %W{password password1 12345678 test}.include?(password)
  23 + end
  24 +
  25 + def self.login username, password, server=nil
  26 + if check_login username, password
  27 + Client.new nil, server, username, "#{username}@danopia.net"
  28 + else
  29 + nil # TODO: Raise error
  30 + end
  31 + end
  32 +end
  33 +
  34 +class Server < EventMachine::Connection
  35 + attr_accessor :port, :ip, :name, :client, :provider
  36 + INSTANCES = []
  37 +
  38 + def initialize provider
  39 + @provider = provider
  40 + @buffer = ''
  41 + #@clients = []
  42 +
  43 + INSTANCES << self
  44 +
  45 + sleep 0.2
  46 + @port, @ip = Socket.unpack_sockaddr_in get_peername
  47 + puts "connection from #{@ip}:#{@port}"
  48 + end
  49 +
  50 + def receive_data data
  51 + @buffer += data
  52 + while @buffer.include? "\n"
  53 + got_line @buffer.slice!(0, @buffer.index("\n")+1).chomp
  54 + end
  55 + end
  56 +
  57 + def send_object action, hash
  58 + hash['action'] = action
  59 + send_data hash.to_json + "\n"
  60 + end
  61 +
  62 + def send_wave_list amount=50, page=1 # TODO: Use limits
  63 + waves = @provider.servers.values.uniq.map {|server| server.waves.keys}.flatten
  64 + send_object 'wavelist', {'wave_ids' => waves}
  65 + end
  66 +
  67 + def got_line line
  68 + data = JSON.parse line
  69 + action = data['action']
  70 +
  71 + case action
  72 + when 'login'
  73 + @client = Client.login data['user'], data['pass'], self
  74 + if @client
  75 + puts "#{@client.address} logged in (#{data['pass']})."
  76 + send_object 'loggedin', {'username' => @client.username, 'address' => @client.address}
  77 + send_wave_list
  78 + else
  79 + puts "Bad login from #{@ip}:#{@port}... #{data['user']}:#{data['pass']}"
  80 + end
  81 +
  82 + when 'wave_info'
  83 + wave = @provider[data['wave_id']]
  84 + puts wave.inspect[0,100]
  85 +
  86 + blips = {}
  87 + wave.blips.each_pair do |id, blip|
  88 + blips[id] = blip.digest
  89 + end
  90 +
  91 + send_object 'wave_info', {
  92 + 'id' => wave.name,
  93 + 'participants' => wave.participants.map {|user| user.to_s},
  94 + 'blips' => blips
  95 + }
  96 + end
  97 + end
  98 +
  99 + def unbind
  100 + puts "connection closed from #{@ip}:#{@port}"
  101 + INSTANCES.delete self
  102 + end
  103 +end # class
  104 +end # module
  105 +
  106 +#EM.run do
  107 +# EM.start_server "127.0.0.1", 7849, Sails::Server
  108 +# puts "server started"
  109 +#end

0 comments on commit cec61f6

Please sign in to comment.
Something went wrong with that request. Please try again.