Archiving a bunch of old stuff
This commit is contained in:
1
oldstuff/ruby-sockets/marco-polo/.gitignore
vendored
Normal file
1
oldstuff/ruby-sockets/marco-polo/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/*.sqlite3*
|
3
oldstuff/ruby-sockets/marco-polo/MarcoPoloClient/.gitignore
vendored
Normal file
3
oldstuff/ruby-sockets/marco-polo/MarcoPoloClient/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*.userprefs
|
||||
*.pidb
|
||||
/MarcoPoloClient/bin/
|
@@ -0,0 +1,2 @@
|
||||
MarcoPoloClient/bin/Debug/MarcoPoloClient.exe: $(wildcard MarcoPoloClient/*.cs)
|
||||
mdtool build --f --buildfile:MarcoPoloClient.sln
|
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||
# Visual Studio 2010
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarcoPoloClient", "MarcoPoloClient\MarcoPoloClient.csproj", "{6728C605-D295-48C7-8634-C9D06A3325F0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{6728C605-D295-48C7-8634-C9D06A3325F0}.Debug|x86.ActiveCfg = Debug|x86
|
||||
{6728C605-D295-48C7-8634-C9D06A3325F0}.Debug|x86.Build.0 = Debug|x86
|
||||
{6728C605-D295-48C7-8634-C9D06A3325F0}.Release|x86.ActiveCfg = Release|x86
|
||||
{6728C605-D295-48C7-8634-C9D06A3325F0}.Release|x86.Build.0 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(MonoDevelopProperties) = preSolution
|
||||
StartupItem = MarcoPoloClient\MarcoPoloClient.csproj
|
||||
EndGlobalSection
|
||||
EndGlobal
|
@@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("MarcoPoloClient")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("me")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Text;
|
||||
|
||||
namespace MarcoPoloClient
|
||||
{
|
||||
class MainClass
|
||||
{
|
||||
const int MARCO_POLO_PORT = 22000;
|
||||
const string MARCO_POLO_SERVER = "127.0.0.1";
|
||||
|
||||
public static void Main (string[] args)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
SocketClient client = new SocketClient();
|
||||
|
||||
string result = client.Connect(MARCO_POLO_SERVER, MARCO_POLO_PORT);
|
||||
result = client.Send("polo doreen 25 75\n");
|
||||
Console.WriteLine(String.Format("'polo' got back: '{0}'", result));
|
||||
string response = client.Receive();
|
||||
Console.WriteLine(String.Format("'polo' response: '{0}'", response));
|
||||
client.Close();
|
||||
|
||||
Console.WriteLine("sleeping for half a second...");
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<ProductVersion>10.0.0</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{6728C605-D295-48C7-8634-C9D06A3325F0}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>MarcoPoloClient</RootNamespace>
|
||||
<AssemblyName>MarcoPoloClient</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Release</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="SocketClient.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Text;
|
||||
|
||||
namespace MarcoPoloClient
|
||||
{
|
||||
public class SocketClient
|
||||
{
|
||||
Socket _socket = null;
|
||||
static ManualResetEvent _clientDone = new ManualResetEvent (false);
|
||||
const int TIMEOUT_MILLISECONDS = 5000;
|
||||
const int MAX_BUFFER_SIZE = 2048;
|
||||
|
||||
public SocketClient ()
|
||||
{
|
||||
}
|
||||
|
||||
public string Connect (string hostName, int portNumber)
|
||||
{
|
||||
string result = string.Empty;
|
||||
DnsEndPoint hostEntry = new DnsEndPoint (hostName, portNumber);
|
||||
_socket = new Socket (
|
||||
AddressFamily.InterNetwork,
|
||||
SocketType.Stream,
|
||||
ProtocolType.Tcp
|
||||
);
|
||||
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
|
||||
socketEventArg.RemoteEndPoint = hostEntry;
|
||||
|
||||
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (delegate(object s, SocketAsyncEventArgs e)
|
||||
{
|
||||
result = e.SocketError.ToString ();
|
||||
_clientDone.Set ();
|
||||
}
|
||||
);
|
||||
|
||||
_clientDone.Reset ();
|
||||
_socket.ConnectAsync (socketEventArg);
|
||||
_clientDone.WaitOne (TIMEOUT_MILLISECONDS);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public string Send (string data)
|
||||
{
|
||||
string response = "Operation Timeout";
|
||||
|
||||
if (_socket != null) {
|
||||
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
|
||||
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
|
||||
socketEventArg.UserToken = null;
|
||||
|
||||
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (delegate(object s, SocketAsyncEventArgs e)
|
||||
{
|
||||
response = e.SocketError.ToString ();
|
||||
_clientDone.Set ();
|
||||
}
|
||||
);
|
||||
|
||||
byte[] payload = Encoding.UTF8.GetBytes (data);
|
||||
socketEventArg.SetBuffer (payload, 0, payload.Length);
|
||||
|
||||
_clientDone.Reset ();
|
||||
_socket.SendAsync (socketEventArg);
|
||||
_clientDone.WaitOne (TIMEOUT_MILLISECONDS);
|
||||
} else {
|
||||
response = "Socket is not initialized";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public string Receive ()
|
||||
{
|
||||
string response = "Operation Timeout";
|
||||
|
||||
if (_socket != null) {
|
||||
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs ();
|
||||
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
|
||||
|
||||
socketEventArg.SetBuffer (new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
|
||||
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs> (delegate(object s, SocketAsyncEventArgs e)
|
||||
{
|
||||
if (e.SocketError == SocketError.Success) {
|
||||
response = Encoding.UTF8.GetString (e.Buffer, e.Offset, e.BytesTransferred);
|
||||
response = response.Trim ('\0');
|
||||
} else {
|
||||
response = e.SocketError.ToString ();
|
||||
}
|
||||
_clientDone.Set ();
|
||||
}
|
||||
);
|
||||
|
||||
_clientDone.Reset ();
|
||||
_socket.ReceiveAsync (socketEventArg);
|
||||
_clientDone.WaitOne (TIMEOUT_MILLISECONDS);
|
||||
} else {
|
||||
response = "Socket is not initialized";
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
if (_socket != null) {
|
||||
_socket.Close ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
79
oldstuff/ruby-sockets/marco-polo/client.rb
Normal file
79
oldstuff/ruby-sockets/marco-polo/client.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
require 'optparse'
|
||||
require 'socket'
|
||||
|
||||
def main(args = ARGV)
|
||||
options = {
|
||||
:type => :write,
|
||||
:sleep_interval => 1.0,
|
||||
:name => 'sally',
|
||||
:port => 22000
|
||||
}
|
||||
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = "Usage: ruby #{File.basename($0)} [options]"
|
||||
opts.on('-t [TYPE]', '--type [TYPE]', [:read, :write],
|
||||
"Client type (read/write), default=#{options[:type].to_s}") do |t|
|
||||
options[:type] = t
|
||||
end
|
||||
opts.on('-n [NAME]', '--name [NAME]',
|
||||
"Client (player) name, default='#{options[:name]}'") do |n|
|
||||
options[:name] = n
|
||||
end
|
||||
opts.on('-s [SLEEP_INTERVAL]', '--sleep [SLEEP_INTERVAL]',
|
||||
"Sleep interval between requests, " <<
|
||||
"default=#{options[:sleep_interval]}") do |s|
|
||||
options[:sleep_interval] = s.to_f
|
||||
end
|
||||
opts.on('-p [PORT]', '--port [PORT]', Integer,
|
||||
"Server port, default=#{options[:port]}") do |p|
|
||||
options[:port] = p
|
||||
end
|
||||
end.parse!(args)
|
||||
|
||||
run_client(options)
|
||||
end
|
||||
|
||||
def run_client(options)
|
||||
client_type = options.fetch(:type, :read)
|
||||
client_name = options.fetch(:name, 'sally')
|
||||
sleep_interval = options.fetch(:sleep_interval, 1.0)
|
||||
port = options.fetch(:port, 22000)
|
||||
|
||||
puts "Starting #{client_type} client for '#{client_name}', " <<
|
||||
"interval=#{sleep_interval}, port=#{port}"
|
||||
|
||||
successes = 0
|
||||
backoff = 0.5
|
||||
nextloc = [rand(0..99), rand(0..99)]
|
||||
loop do
|
||||
begin
|
||||
TCPSocket.open('127.0.0.1', port) do |sock|
|
||||
case client_type
|
||||
when :read
|
||||
sock.send("marco #{client_name}\n", 0)
|
||||
location = sock.recv(100).to_s.split[2,4].map(&:to_i)
|
||||
puts "marco #{client_name} -> #{location.first} #{location.last}"
|
||||
when :write
|
||||
sock.send("polo #{client_name} #{nextloc.first} #{nextloc.last}\n", 0)
|
||||
recv_loc = sock.recv(100).to_s.split.map(&:to_i)
|
||||
puts "polo #{client_name} #{nextloc.first} #{nextloc.last} " <<
|
||||
"-> #{recv_loc.first} #{recv_loc.last}"
|
||||
nextloc = recv_loc
|
||||
end
|
||||
sock.close
|
||||
end
|
||||
successes += 1
|
||||
backoff = 0.5
|
||||
sleep sleep_interval
|
||||
rescue
|
||||
puts "made it #{successes} times. sleeping #{backoff} secs"
|
||||
successes = 0
|
||||
sleep backoff
|
||||
backoff *= 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
main
|
||||
end
|
102
oldstuff/ruby-sockets/marco-polo/server.rb
Normal file
102
oldstuff/ruby-sockets/marco-polo/server.rb
Normal file
@@ -0,0 +1,102 @@
|
||||
require 'eventmachine'
|
||||
require 'sequel'
|
||||
|
||||
class MarcoPolo
|
||||
attr_reader :locations, :db
|
||||
|
||||
def initialize
|
||||
@pool_range = 0..99
|
||||
# I'm not using this (yet?) ... but don't want to forget how I did it
|
||||
# @pool = [].tap do |board|
|
||||
# @pool_range.each do |y|
|
||||
# board << [].tap do |row|
|
||||
# @pool_range.each do |x|
|
||||
# row << "#{y}-#{x}"
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
@db = Sequel.connect("postgres://#{ENV['USER']}@localhost/marco_polo",
|
||||
:max_connections => 10)
|
||||
unless @db.table_exists?(:locations)
|
||||
@db.create_table :locations do
|
||||
primary_key :id
|
||||
String :client
|
||||
DateTime :timestamp
|
||||
Integer :x
|
||||
Integer :y
|
||||
constraint(:in_the_pool, 'x > -1 and x < 100 and y > -1 and y < 100')
|
||||
end
|
||||
end
|
||||
@locations = @db[:locations]
|
||||
puts "LET GAME BEGIN NOW"
|
||||
end
|
||||
|
||||
def polo(client, x, y)
|
||||
@locations.insert(:client => client, :x => x, :y => y,
|
||||
:timestamp => Time.now)
|
||||
nextloc(x, y)
|
||||
end
|
||||
|
||||
def marco(client = nil)
|
||||
if client
|
||||
@locations.where(:client => client).order_by(:timestamp.desc).first
|
||||
else
|
||||
@locations.order_by(:timestamp.desc).first
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def nextloc(x, y)
|
||||
# we allow exiting the pool as long as you get back in on the opposite side
|
||||
# a la pac man
|
||||
move_x, move_y = rand(-2..2), rand(-2..2)
|
||||
new_x = x + move_x
|
||||
new_x = 100 + new_x if new_x <= -1
|
||||
new_y = y + move_y
|
||||
new_y = 100 + new_y if new_y <= -1
|
||||
[
|
||||
new_x > 99 ? new_x % 100 : new_x,
|
||||
new_y > 99 ? new_y % 100 : new_y
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
class MarcoPoloServer < EventMachine::Connection
|
||||
class << self
|
||||
attr_accessor :game
|
||||
end
|
||||
|
||||
def receive_data(data)
|
||||
parts = data.to_s.split
|
||||
case parts.first
|
||||
when 'polo'
|
||||
client, x, y = parts[1,4]
|
||||
newloc = self.class.game.polo(client, x.to_i, y.to_i)
|
||||
send_data("#{newloc.first} #{newloc.last}\n")
|
||||
when 'marco'
|
||||
if loc = self.class.game.marco(parts[1])
|
||||
send_data("polo #{loc[:client]} #{loc[:x]} #{loc[:y]}\n")
|
||||
else
|
||||
send_data("polo NULL 0 0\n")
|
||||
end
|
||||
else
|
||||
send_data("#{parts.first}???\n")
|
||||
end
|
||||
rescue => e
|
||||
STDERR.puts("#{e.class.name} #{e.message}: #{e.backtrace.join("\n")}")
|
||||
end
|
||||
end
|
||||
|
||||
def main
|
||||
MarcoPoloServer.game = MarcoPolo.new
|
||||
EventMachine.run do
|
||||
host, port = '0.0.0.0', 22000
|
||||
EventMachine.start_server(host, port, MarcoPoloServer)
|
||||
puts "Listening on #{host}:#{port}"
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
main
|
||||
end
|
Reference in New Issue
Block a user