s41c 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in s41c.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
File without changes
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+
3
+ module S41C
4
+
5
+ require 'win32ole'
6
+ require 'socket'
7
+
8
+ class Server
9
+
10
+ def initialize(host='localhost', port=1421, log_file=nil)
11
+ @host, @port = host, port
12
+ @logger = log_file ? ::STDOUT.reopen(log_file, 'a') : ::STDOUT
13
+ @ole_object = 'V77.Application'
14
+ end # initialize
15
+
16
+ def ole_object=(name)
17
+ @ole_object = name
18
+ end # ole_object
19
+
20
+ def at_exit(&block)
21
+ ::Kernel::at_exit do
22
+ yield
23
+ end
24
+ end # at_exit
25
+
26
+ def start
27
+
28
+ ["INT", "TERM"].each do |signal|
29
+ ::Kernel::trap(signal) {
30
+ log "\n*** Exiting"
31
+ exit
32
+ }
33
+ end
34
+
35
+ server = TCPServer.open(@host, @port)
36
+
37
+ log "*** Server has been started on #{@host}:#{@port}"
38
+ log "*** Ctrl+C for stopping"
39
+
40
+ loop do
41
+
42
+ begin
43
+ session = server.accept_nonblock
44
+ rescue IO::WaitReadable, Errno::EINTR
45
+ IO.select([server])
46
+ retry
47
+ end
48
+
49
+ session.print "Welcome\r\n"
50
+
51
+ loop {
52
+ attrs = S41C::Utils.to_utf8(session.gets || '').chomp.split('|')
53
+ cmd = attrs.shift
54
+ case cmd
55
+ when "connect"
56
+ session.puts S41C::Utils.to_bin(connect_to_1c(attrs))
57
+ session.puts "+OK"
58
+ when "create"
59
+ session.puts S41C::Utils.to_bin(create(attrs))
60
+ session.puts "+OK"
61
+ when "eval_expr"
62
+ session.puts S41C::Utils.to_bin(eval_expr(attrs))
63
+ session.puts "+OK"
64
+ when "invoke"
65
+ session.puts S41C::Utils.to_bin(invoke(attrs))
66
+ session.puts "+OK"
67
+ when "disconnect"
68
+ session.puts "Goodbye"
69
+ session.close
70
+ break
71
+ when "shutdown"
72
+ session.puts "Server is going down now"
73
+ session.close
74
+ exit
75
+ else
76
+ session.puts "Bad command"
77
+ session.puts "+OK"
78
+ end
79
+ }
80
+
81
+ end
82
+ end # start
83
+
84
+ private
85
+
86
+ def log(msg)
87
+ @logger.puts msg
88
+ @logger.flush
89
+ end # log
90
+
91
+ def connect_to_1c(attrs)
92
+ @conn.ole_free unless @conn.nil?
93
+ options = attrs.shift || ''
94
+ begin
95
+ @conn = ::WIN32OLE.new('V77.Application')
96
+ res = @conn.Initialize(
97
+ @conn.RMTrade,
98
+ options,
99
+ ''
100
+ )
101
+ "Connected"
102
+ rescue => e
103
+ @conn.ole_free
104
+ "Error: #{e.message}"
105
+ end
106
+ end
107
+
108
+ def create(attrs)
109
+ return "Error: not connected" unless @conn
110
+ obj_name = attrs.shift || ''
111
+ begin
112
+ @obj = @conn.CreateObject(obj_name)
113
+ "Created"
114
+ rescue => e
115
+ "Error: #{e.message}"
116
+ end
117
+ end
118
+
119
+ def eval_expr(attrs)
120
+ return "Error: not connected" unless @conn
121
+ expr = attrs.shift || ''
122
+ begin
123
+ @conn.invoke("EvalExpr", expr).to_s
124
+ rescue => e
125
+ "Error: #{e.message}"
126
+ end
127
+ end
128
+
129
+ def invoke(attrs)
130
+ return "Error: working object not found. You must create it before" unless @conn
131
+ begin
132
+ @obj.invoke(*attrs).to_s.encode("UTF-8", "IBM866", :invalid => :replace, :replace => "?")
133
+ rescue => e
134
+ "Error: #{e.message}"
135
+ end
136
+ end
137
+
138
+ end # Server
139
+
140
+ end # S41C
data/lib/s41c/utils.rb ADDED
@@ -0,0 +1,15 @@
1
+ module S41C
2
+
3
+ module Utils
4
+
5
+ def Utils.to_bin(str)
6
+ str.force_encoding("BINARY")
7
+ end
8
+
9
+ def Utils.to_utf8(str)
10
+ str.force_encoding("UTF-8")
11
+ end
12
+
13
+ end # Utils
14
+
15
+ end # S41C
@@ -0,0 +1,5 @@
1
+ module S41C
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end # S41C
data/lib/s41c.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 's41c/version'
2
+ require 's41c/utils'
3
+ require 's41c/server'
4
+ require 's41c/client'
data/s41c.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "s41c/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "s41c"
7
+ s.version = S41C::VERSION
8
+ s.authors = ["redfield"]
9
+ s.email = ["up.redfield@gmail.com"]
10
+ s.homepage = "https://github.com/dancingbytes/s41c"
11
+ s.summary = %q{}
12
+ s.description = %q{TCP-socket сервер и клиент для платформы "1С:Предприятие"}
13
+
14
+ s.rubyforge_project = "s41c"
15
+
16
+ s.files = Dir['**/*']
17
+ s.require_paths = ["lib"]
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s41c
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - redfield
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: TCP-socket сервер и клиент для платформы "1С:Предприятие"
15
+ email:
16
+ - up.redfield@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - s41c.gemspec
22
+ - s41c-0.0.1.gem
23
+ - lib/s41c/version.rb
24
+ - lib/s41c/utils.rb
25
+ - lib/s41c/server.rb
26
+ - lib/s41c/client.rb
27
+ - lib/s41c.rb
28
+ - Gemfile
29
+ - Rakefile
30
+ homepage: https://github.com/dancingbytes/s41c
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: s41c
50
+ rubygems_version: 1.8.10
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: ''
54
+ test_files: []