Class: Flexo::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/flexo/manager.rb

Overview

One class to rule them all. And in the darkness bind them.

This singleton-class creates an instance of each other class that’s needed and manages threads, mutexes and other things that need sharing between classes. Each class can just save a reference to Flexo::Manager and get access to everything they need.

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



31
32
# File 'lib/flexo/manager.rb', line 31

def initialize
end

Instance Method Details

#configObject



134
135
136
# File 'lib/flexo/manager.rb', line 134

def config
  return @config
end

#dispatcherObject



126
127
128
# File 'lib/flexo/manager.rb', line 126

def dispatcher
  return @dispatcher
end

#find_pluginsObject



154
155
156
# File 'lib/flexo/manager.rb', line 154

def find_plugins
  return @pluginmanager.find_plugins
end

#load_plugin(plugin) ⇒ Object



150
151
152
# File 'lib/flexo/manager.rb', line 150

def load_plugin(plugin)
  return @pluginmanager.load_plugin(plugin)
end

#loggerObject



130
131
132
# File 'lib/flexo/manager.rb', line 130

def logger
  return @logger
end

#mutexObject



118
119
120
# File 'lib/flexo/manager.rb', line 118

def mutex
  @mutex.synchronize { yield }
end

#nicknameObject



158
# File 'lib/flexo/manager.rb', line 158

def nickname; return @nickname; end

#nickname=(nick) ⇒ Object



159
# File 'lib/flexo/manager.rb', line 159

def nickname=(nick); @nickname = nick; nick; end

#pluginObject



146
147
148
# File 'lib/flexo/manager.rb', line 146

def plugin
  return @pluginmanager
end

#pluginmutexObject



122
123
124
# File 'lib/flexo/manager.rb', line 122

def pluginmutex
  @pluginmutex.synchronize { yield }
end

#senderObject



138
139
140
# File 'lib/flexo/manager.rb', line 138

def sender
  return @sender
end

#serverObject



142
143
144
# File 'lib/flexo/manager.rb', line 142

def server
  return @server
end

#setupObject

Create everything



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flexo/manager.rb', line 35

def setup
  @config        = Flexo::Config.new
  raise InvalidConfigurationException unless @config.valid?

  @nickname      = nil
  @threadqueue   = Queue.new
  @threadgroup   = ThreadGroup.new
  @threadmax     = 100
  @mutex         = Mutex.new
  @pluginmutex   = Mutex.new

  @logger        = Flexo::Logger.new
  @logger.loglevel = @config['log.level']
  @logger.logfile  = @config['log.file']

  @logger.debug "Creating Dispatcher"
  @dispatcher    = Flexo::Dispatcher.new

  @logger.debug "Creating PluginManager"
  @pluginmanager = Flexo::PluginManager.new(@pluginmutex)
  
  @config['plugin.autoload'] ||= []
  @config['plugin.autoload'].each do |plugin|
    @pluginmanager.load_plugin(plugin)
  end

  @logger.debug "Creating Sender"
  @sender        = Flexo::Sender.new

  @logger.debug "Creating Server"
  @server        = Flexo::Server.new
  @server.setup

  @logger.debug "Created all Flexo-classes. Going to create threads"
  manager = Thread.new do        
    loop do
      if @threadqueue.size < @threadmax
        @threadgroup.add(@threadqueue.shift.wakeup)
      else
        sleep 0.1
      end
    end
    @threadgroup.add manager
  end

  @logger.info "Setting up default handlers"
  dispatcher.subscribe(:PING) { |ping| ping.pong }
  dispatcher.subscribe(Flexo::Events::PrivmsgEvent) { |msg|
    if msg.ctcp?
      case msg.text.split(' ')[0].upcase
      when 'VERSION'
        l  = "\001VERSION Flexo #{Flexo::Constants::FLEXO_VERSION} "
        l += "(#{Flexo::Constants::FLEXO_RELEASE})\001"
        @sender.notice(msg.data.hostmask.nickname, l)
      when 'PING'
        arg = msg.text.split(' ')[1]
        cmd = "\001PING #{arg}\001"
        @sender.notice(msg.data.hostmask.nickname, cmd)
      end
    end
  }
  
  while(!@server.welcomed) do
    @logger.debug "Not welcomed, sleeping"
    sleep 0.5
  end

  @logger.info "Joining channels"
  @config['core.channels'].each do |channel|
    @sender.join(channel[:channel], channel[:key])
  end

  @logger.debug "Joining server-thread"
  @server.thread.join
end

#thread(&block) ⇒ Object

Spawns a thread to handle the block



112
113
114
115
116
# File 'lib/flexo/manager.rb', line 112

def thread(&block)
  thread = Thread.new { sleep; block.call }
  @threadqueue << thread
  return thread
end