Class: Ucp::Util::UcpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ucp/util/ucp_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, authcreds = nil) ⇒ UcpClient

Returns a new instance of UcpClient.



31
32
33
34
35
36
37
38
39
# File 'lib/ucp/util/ucp_client.rb', line 31

def initialize(host,port, authcreds=nil)
  @host=host
  @port=port
  @connected=false
  @authcreds=authcreds
  @trn=0
  @mr=0
  connect()
end

Instance Method Details

#closeObject



72
73
74
75
76
77
78
# File 'lib/ucp/util/ucp_client.rb', line 72

def close
  begin
    @socket.close
  rescue
  end
  @connected=false
end

#connectObject



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
# File 'lib/ucp/util/ucp_client.rb', line 41

def connect
  #puts "connect()"
  
  begin
      @socket = TCPSocket.new(@host, @port)
      @connected=true
      @trn=0
  rescue
      @connected=false
      return false
  end


  if !@authcreds.nil?
    auth_ucp=Ucp60Operation.new
    auth_ucp.basic_auth(@authcreds[:login],@authcreds[:password])
    auth_ucp.trn="00"
    answer=send_sync(auth_ucp)
    #puts "Crecv: #{answer.to_s}\n"
    
    if !answer.nil? && answer.is_ack?
      inc_trn()
      return true
    else
      close()
      return false
    end
  end

end

#connected?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/ucp/util/ucp_client.rb', line 80

def connected?
  if !@socket.nil? && !@socket.closed? && @connected
    @connected=true
  else
    @connected=false
  end
  return @connected
end

#inc_mrObject



202
203
204
205
# File 'lib/ucp/util/ucp_client.rb', line 202

def inc_mr
 @mr+=1
 @mr=0 if @mr>255
end

#inc_trnObject



197
198
199
200
# File 'lib/ucp/util/ucp_client.rb', line 197

def inc_trn
 @trn+=1
 @trn=0 if @trn>99
end

#readObject



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ucp/util/ucp_client.rb', line 149

def read
  answer=nil
  begin
      answer = @socket.gets(3.chr)
  rescue
      puts "error: #{$!}"
      # deu erro, vamos fechar o socket
      close()
  end

  return answer
end

#send(ucp) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ucp/util/ucp_client.rb', line 126

def send(ucp)
  if !connected?
    connect()
    # se nao foi possivel ligar, retornar imediatamente com erro
    if !connected?
      return false
    end
  end

  begin
      @socket.print ucp.to_s
  rescue
      puts "error: #{$!}"
      # deu erro, vamos fechar o socket
      close()
      # error
      return false
  end

  # OK
  return true
end

#send_alert(recipient, pid) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ucp/util/ucp_client.rb', line 181

def send_alert(recipient,pid)
  ucp=Ucp31Operation.new
  ucp.basic_alert(recipient,pid)

  trn=UCP.int2hex(@trn)
  ucp.trn=trn
  ans=send_sync(ucp)
  inc_trn()
#      puts "e um ack: #{ans.nil?} ; #{ans.is_ack?}"
  return false if ans.nil? || !ans.is_ack?

  return true
end

#send_message(originator, recipient, message) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ucp/util/ucp_client.rb', line 162

def send_message(originator,recipient,message)
  #ucp=Ucp51Operation.new(originator,recipient,message)

  ucps=UCP.make_multi_ucps(originator,recipient,message,@mr)
  inc_mr()

  ucps.each { |ucp|      
    ucp.trn=UCP.int2hex(@trn)
    ans=send_sync(ucp)
    inc_trn()
#      puts "e um ack: #{ans.nil?} ; #{ans.is_ack?}"
    return false if ans.nil? || !ans.is_ack?
  }


  return true
end

#send_sync(ucp) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ucp/util/ucp_client.rb', line 89

def send_sync(ucp)
  #puts "XXXX0"

  if !connected?
    connect()
    # se nao foi possivel ligar, retornar imediatamente com erro
    if !connected?
      return nil
    end
  end

  #puts "XXXX1"
  
  answer=nil
  begin
      @socket.print ucp.to_s
      puts "Csent: #{ucp.to_s}\n"
      answer = @socket.gets(3.chr)
      #answer = @socket.gets("#")
      puts "Crecv: #{answer.to_s}\n"
  rescue
      puts "error: #{$!}"
      @connected=false
  end

  # verificar o trn da resposta face a submissao
  replyucp=UCP.parse_str(answer)
  return nil if replyucp.nil?

  if !ucp.trn.eql?(replyucp.trn)
    puts "unexpected trn #{replyucp.trn}. should be #{ucp.trn}"
    return nil
  else
    return replyucp
  end
end