accutronic 0.2.1 → 0.2.2

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.
Files changed (2) hide show
  1. data/lib/accu-net.rb +190 -0
  2. metadata +3 -2
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ #--
5
+ # Copyright 2014 Luke Spangler
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #++
19
+
20
+ # Library containing two classes which make
21
+ # TCP connections significantly easier.
22
+ # Eventually the other libraries will call
23
+ # this one for network communications.
24
+ #
25
+ #
26
+ # Author:: Luke Spangler
27
+ # Email:: m27frogy.roblox@gmail.com
28
+
29
+ require "abc"
30
+ require "accu-encrypt"
31
+
32
+ # The wrapper for TCPServer.
33
+ class ServerWrapper
34
+ def initialize(port=2000)
35
+ @port = TCPServer.open port
36
+ catch :close do
37
+ loop do
38
+ @server = @port.accept
39
+ @password,@mode = "password",:double
40
+ yield self,@server if block_given?
41
+ @server.close
42
+ end
43
+ end
44
+ @port.close
45
+ end
46
+
47
+ # Listens for a generic network communication
48
+ # and yields to a passed block (if any).
49
+ def listen()
50
+ msg = @server.gets.chomp
51
+ yield msg
52
+ end
53
+
54
+ # Listens for multiple lines, appends them together
55
+ # and stops at END.
56
+ def listen_lines()
57
+ lines = []
58
+ while line = @server.gets.chomp and line != "END" do
59
+ lines << line
60
+ end
61
+ lines
62
+ end
63
+
64
+ # Listens for a network code.
65
+ def listen_code()
66
+ listen { |msg|
67
+ if msg.slice(0..1) == "C:" then
68
+ return msg.slice(2..msg.length).to_i
69
+ else
70
+ return msg.slice(0..1)
71
+ end
72
+ }
73
+ end
74
+
75
+ # Sends a plaintext message.
76
+ def send(text)
77
+ @server.puts text
78
+ end
79
+
80
+ # Sends multiple lines and appends
81
+ # END to end the send.
82
+ def send_lines(lines)
83
+ array = lines.split("\n")
84
+ array.each do |line|
85
+ send(line)
86
+ end
87
+ send("END")
88
+ end
89
+
90
+ # Send over an encrypted multiline message.
91
+ def send_encrypted(lines)
92
+ secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
93
+ send_code(3)
94
+ send(text)
95
+ send("END")
96
+ end
97
+
98
+ # Sends a code.
99
+ def send_code(code)
100
+ send("C:#{code}")
101
+ end
102
+
103
+ # Set encryption password.
104
+ def set_password(string)
105
+ warn "Argument 1 is not a string! Continuing anyway." if not string.is_a? String
106
+ @password = string.to_s
107
+ end
108
+
109
+ # Throws :close to end the serving process.
110
+ def close
111
+ throw :close
112
+ end
113
+ end
114
+
115
+ #--------------------------------------------
116
+ #--------------------------------------------
117
+ #--------------------------------------------
118
+ #--------------------------------------------
119
+
120
+ # The wrapper for TCPClient.
121
+ class ClientWrapper
122
+ def initialize(port=2000,ip="127.0.0.1")
123
+ @client = TCPSocket.open(ip, port)
124
+ @password,@mode = "password",:double
125
+ yield self,@client if block_given?
126
+ @client.close
127
+ end
128
+
129
+ # Listens for a generic network communication
130
+ # and yields to a passed block (if any).
131
+ def listen()
132
+ msg = @client.gets.chomp
133
+ yield msg
134
+ end
135
+
136
+ # Listens for multiple lines, appends them together
137
+ # and stops at END.
138
+ def listen_lines()
139
+ lines = []
140
+ while line = @client.gets.chomp and line != "END" do
141
+ lines << line
142
+ end
143
+ lines
144
+ end
145
+
146
+ # Listens for a network code.
147
+ def listen_code()
148
+ listen { |msg|
149
+ if msg.slice(0..1) == "C:" then
150
+ return msg.slice(2..msg.length).to_i
151
+ else
152
+ return msg.slice(0..1)
153
+ end
154
+ }
155
+ end
156
+
157
+ # Sends a plaintext message.
158
+ def send(text)
159
+ @client.puts text
160
+ end
161
+
162
+ # Sends multiple lines and appends
163
+ # END to end the send.
164
+ def send_lines(lines)
165
+ array = lines.split("\n")
166
+ array.each do |line|
167
+ send(line)
168
+ end
169
+ send("END")
170
+ end
171
+
172
+ # Send over an encrypted multiline message.
173
+ def send_encrypted(lines)
174
+ secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
175
+ send_code(3)
176
+ send(text)
177
+ send("END")
178
+ end
179
+
180
+ # Sends a code.
181
+ def send_code(code)
182
+ send("C:#{code}")
183
+ end
184
+
185
+ # Set encryption password.
186
+ def set_password(string)
187
+ warn "Argument 1 is not a string! Continuing anyway." if not string.is_a? String
188
+ @password = string.to_s
189
+ end
190
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accutronic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-26 00:00:00.000000000 Z
12
+ date: 2014-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abc
@@ -62,6 +62,7 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - lib/accu-window.rb
64
64
  - lib/accu-encrypt.rb
65
+ - lib/accu-net.rb
65
66
  - lib/accu-file.rb
66
67
  - lib/accu-cipher.rb
67
68
  - LICENSE.txt