Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 1 | ---- Licensed to the Apache Software Foundation (ASF) under one |
| 2 | -- or more contributor license agreements. See the NOTICE file |
| 3 | -- distributed with this work for additional information |
| 4 | -- regarding copyright ownership. The ASF licenses this file |
| 5 | -- to you under the Apache License, Version 2.0 (the |
| 6 | -- "License"); you may not use this file except in compliance |
| 7 | -- with the License. You may obtain a copy of the License at |
| 8 | -- |
| 9 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | -- |
| 11 | -- Unless required by applicable law or agreed to in writing, |
| 12 | -- software distributed under the License is distributed on an |
| 13 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | -- KIND, either express or implied. See the License for the |
| 15 | -- specific language governing permissions and limitations |
| 16 | -- under the License. |
| 17 | -- |
| 18 | |
| 19 | require 'TTransport' |
| 20 | require 'libluasocket' |
| 21 | |
| 22 | -- TSocketBase |
| 23 | TSocketBase = TTransportBase:new{ |
| 24 | __type = 'TSocketBase', |
| 25 | timeout = 1000, |
| 26 | host = 'localhost', |
| 27 | port = 9090, |
| 28 | handle |
| 29 | } |
| 30 | |
| 31 | function TSocketBase:close() |
| 32 | if self.handle then |
| 33 | self.handle:destroy() |
| 34 | self.handle = nil |
| 35 | end |
| 36 | end |
| 37 | |
| 38 | -- Returns a table with the fields host and port |
| 39 | function TSocketBase:getSocketInfo() |
| 40 | if self.handle then |
| 41 | return self.handle:getsockinfo() |
| 42 | end |
| 43 | terror(TTransportException:new{errorCode = TTransportException.NOT_OPEN}) |
| 44 | end |
| 45 | |
| 46 | function TSocketBase:setTimeout(timeout) |
| 47 | if timeout and ttype(timeout) == 'number' then |
| 48 | if self.handle then |
| 49 | self.handle:settimeout(timeout) |
| 50 | end |
| 51 | self.timeout = timeout |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | -- TSocket |
| 56 | TSocket = TSocketBase:new{ |
| 57 | __type = 'TSocket', |
| 58 | host = 'localhost', |
| 59 | port = 9090 |
| 60 | } |
| 61 | |
| 62 | function TSocket:isOpen() |
| 63 | if self.handle then |
| 64 | return true |
| 65 | end |
| 66 | return false |
| 67 | end |
| 68 | |
| 69 | function TSocket:open() |
| 70 | if self.handle then |
| 71 | self:close() |
| 72 | end |
| 73 | |
| 74 | -- Create local handle |
| 75 | local sock, err = luasocket.create_and_connect( |
| 76 | self.host, self.port, self.timeout) |
| 77 | if err == nil then |
| 78 | self.handle = sock |
| 79 | end |
| 80 | |
| 81 | if err then |
| 82 | terror(TTransportException:new{ |
| 83 | message = 'Could not connect to ' .. self.host .. ':' .. self.port |
| 84 | .. ' (' .. err .. ')' |
| 85 | }) |
| 86 | end |
| 87 | end |
| 88 | |
| 89 | function TSocket:read(len) |
| 90 | local buf = self.handle:receive(self.handle, len) |
| 91 | if not buf or string.len(buf) ~= len then |
| 92 | terror(TTransportException:new{errorCode = TTransportException.UNKNOWN}) |
| 93 | end |
| 94 | return buf |
| 95 | end |
| 96 | |
| 97 | function TSocket:write(buf) |
| 98 | self.handle:send(self.handle, buf) |
| 99 | end |
| 100 | |
| 101 | function TSocket:flush() |
| 102 | end |
| 103 | |
| 104 | -- TServerSocket |
| 105 | TServerSocket = TSocketBase:new{ |
| 106 | __type = 'TServerSocket', |
| 107 | host = 'localhost', |
| 108 | port = 9090 |
| 109 | } |
| 110 | |
| 111 | function TServerSocket:listen() |
| 112 | if self.handle then |
| 113 | self:close() |
| 114 | end |
| 115 | |
| 116 | local sock, err = luasocket.create(self.host, self.port) |
| 117 | if not err then |
| 118 | self.handle = sock |
| 119 | else |
| 120 | terror(err) |
| 121 | end |
| 122 | self.handle:settimeout(self.timeout) |
| 123 | self.handle:listen() |
| 124 | end |
| 125 | |
| 126 | function TServerSocket:accept() |
| 127 | local client, err = self.handle:accept() |
| 128 | if err then |
| 129 | terror(err) |
| 130 | end |
| 131 | return TSocket:new({handle = client}) |
| 132 | end |