Monday, 9 September 2013

Asynchronous server and client with named pipes

Asynchronous server and client with named pipes

I've came up with the follwing piece of code:
class IPCServer
{
private Thread ipcServerThread;
private NamedPipeServerStream pipeServer;
public IPCServer()
{
pipeServer = new NamedPipeServerStream("iMedCallInfoPipe",
PipeDirection.Out, 1, PipeTransmissionMode.Byte,
PipeOptions.Asynchronous);
}
public void SendMessage (String message) {
ThreadStart ipcServerThreadInfo = () => WriteToPipe(message);
ipcServerThread = new Thread(ipcServerThreadInfo);
ipcServerThread.Start();
}
private void WriteToPipe(String message)
{
if (pipeServer.IsConnected)
{
byte[] bytes = Encoding.UTF8.GetBytes(message);
pipeServer.Write(bytes, 0, bytes.Length);
pipeServer.WaitForPipeDrain();
pipeServer.Flush();
}
}
}
class ICPClient
{
public void Read(int TimeOut = 1000)
{
try
{
NamedPipeClientStream pipeStream = new
NamedPipeClientStream(".", "iMedCallInfoPipe",
PipeDirection.In, PipeOptions.None);
pipeStream.Connect(TimeOut);
using (StreamReader sr = new StreamReader(pipeStream))
{
string _buffer;
while ((_buffer = sr.ReadLine()) != null)
{
Console.WriteLine("Received from server: {0}", _buffer);
}
}
}
catch (TimeoutException)
{
}
}
}
and this is the client server for pipe-communicating solutiuon. But I need
the server to write messages asynchronously and client to read them
whenever they pop up, also asynchronously. How can I achieve this? There
is number of samples but most of them consider client writing to server
and i'm not sure how to achieve my goal, especially with my
already-written code...

Sunday, 8 September 2013

config.active_record.default_timezone = :local ... Doesn't seem to have any effect

config.active_record.default_timezone = :local ... Doesn't seem to have
any effect

The configuration for active_record.default_timezone does not seem to work
as advertised. I expected that after setting:
config.active_record.default_timezone = :local
(in config/application.rb) that outputting a time retrieved from the db
would show an offset in the local timezone.
2.0.0p247 :016 > user = User.new name: "Tom", email: "tom@gmail.com"
=> #<User id: nil, name: "Tom", email: "tom@gmail.com", created_at: nil,
updated_at: nil>
2.0.0p247 :017 > user.save
(0.3ms) SAVEPOINT active_record_1
SQL (0.8ms) INSERT INTO "users" ("created_at", "email", "name",
"updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at",
Sun, 08 Sep 2013 23:38:22 UTC +00:00], ["email", "tom@gmail.com"],
["name", "Tom"], ["updated_at", Sun, 08 Sep 2013 23:38:22 UTC +00:00]]
(0.7ms) RELEASE SAVEPOINT active_record_1
=> true
2.0.0p247 :018 > puts user.created_at
2013-09-08 23:38:22 UTC <----------+
=> nil |
|---- WHY DON'T THESE HAVE THE SAME
OFFSET???
2.0.0p247 :019 > Time.now |
=> 2013-09-08 17:38:36 +0200 <------+
2.0.0p247 :020 > Rails.application.config.active_record.default_timezone
=> :local
2.0.0p247 :021 >
2.0.0p247 :022 > Rails.application.config
=> #<Rails::Application::Configuration:0x00000100af1508
@root=#<Pathname:/Users/7stud/rails_projects/test_postgres>,
@generators=#<Rails::Configuration::Generators:0x00000100ff3128
@aliases={}, @options={:rails=>{:orm=>:active_record,
:test_framework=>:rspec, :integration_tool=>:rspec,
:stylesheet_engine=>:scss, :javascript_engine=>:coffee,
:scaffold_controller=>:jbuilder_scaffold_controller,
:json_template_engine=>:jbuilder}, :active_record=>{:migration=>true,
:timestamps=>true}, :test_unit=>{:fixture=>true,
:fixture_replacement=>nil}}, @fallbacks={}, @templates=[],
@colorize_logging=true, @hidden_namespaces=["sass"]>, @encoding="utf-8",
@allow_concurrency=nil, @consider_all_requests_local=true,
@filter_parameters=[:password], @filter_redirect=[],
@helpers_paths=["/Users/7stud/rails_projects/test_postgres/app/helpers"],
@serve_static_assets=true, @static_cache_control=nil, @force_ssl=false,
@ssl_options={}, @session_store=:cookie_store,
@session_options={:key=>"_test_postgres_session", :cookie_only=>true},
@time_zone="UTC", @beginning_of_week=:monday, @log_level=:debug,
@middleware=#<ActionDispatch::MiddlewareStack:0x00000100fa5158
@middlewares=[ActionDispatch::Static, Rack::Lock,
#<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000102a2e6c8>,
Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId,
Rails::Rack::Logger, ActionDispatch::ShowExceptions,
ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp,
ActionDispatch::Reloader, ActionDispatch::Callbacks,
ActiveRecord::Migration::CheckPending,
ActiveRecord::ConnectionAdapters::ConnectionManagement,
ActiveRecord::QueryCache, ActionDispatch::Cookies,
ActionDispatch::Session::CookieStore, ActionDispatch::Flash,
ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet,
Rack::ETag]>, @cache_store=[:file_store,
"/Users/7stud/rails_projects/test_postgres/tmp/cache/"],
@railties_order=[:all], @relative_url_root=nil,
@reload_classes_only_on_change=true,
@file_watcher=ActiveSupport::FileUpdateChecker, @exceptions_app=nil,
@autoflush_log=true,
@log_formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00000100af8c18
@datetime_format=nil>, @eager_load=false, @secret_token=nil,
@secret_key_base="123f640f8d665d9a20bc49ae045b1a0edd41972db82091ec8bd3ff0836d805698fbe70ca75d2a78c3cd088774412b67f789d74b7a2422ad30acc589487f49ce7",
@assets={:enabled=>true, :paths=>[],
:precompile=>[#<Proc:0x00000100b02970@/Users/7stud/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/application/configuration.rb:54>,
/(?:\/|\\|\A)application\.(css|js)$/], :prefix=>"/assets",
:version=>"1.0", :debug=>false, :compile=>true, :digest=>false,
:cache_store=>[:file_store,
"/Users/7stud/rails_projects/test_postgres/tmp/cache/assets/development/"],
:js_compressor=>nil, :css_compressor=>nil,
:initialize_on_precompile=>true, :logger=>nil},
@paths=#<Rails::Paths::Root:0x00000100b1b6a0 @current=nil,
@path=#<Pathname:/Users/7stud/rails_projects/test_postgres>,
@root={"app"=>#<Rails::Paths::Path:0x00000100b1b2e0 @paths=["app"],
@current="app", @root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>,
@glob="*", @autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "app/assets"=>#<Rails::Paths::Path:0x00000100b1aef8
@paths=["app/assets"], @current="app/assets",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="*",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"app/controllers"=>#<Rails::Paths::Path:0x00000100b1ac50
@paths=["app/controllers"], @current="app/controllers",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "app/helpers"=>#<Rails::Paths::Path:0x00000100b1a840
@paths=["app/helpers"], @current="app/helpers",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "app/models"=>#<Rails::Paths::Path:0x00000100b1a110
@paths=["app/models"], @current="app/models",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "app/mailers"=>#<Rails::Paths::Path:0x00000100b19da0
@paths=["app/mailers"], @current="app/mailers",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "app/views"=>#<Rails::Paths::Path:0x00000100b19940
@paths=["app/views"], @current="app/views",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"app/controllers/concerns"=>#<Rails::Paths::Path:0x00000100b193a0
@paths=["app/controllers/concerns"], @current="app/controllers/concerns",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>,
"app/models/concerns"=>#<Rails::Paths::Path:0x00000100b19058
@paths=["app/models/concerns"], @current="app/models/concerns",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=true, @autoload=false,
@load_path=false>, "lib"=>#<Rails::Paths::Path:0x00000100b18978
@paths=["lib"], @current="lib",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=true>, "lib/assets"=>#<Rails::Paths::Path:0x00000100b181a8
@paths=["lib/assets"], @current="lib/assets",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="*",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "lib/tasks"=>#<Rails::Paths::Path:0x00000100b238a0
@paths=["lib/tasks"], @current="lib/tasks",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="**/*.rake",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "config"=>#<Rails::Paths::Path:0x00000100b22f90
@paths=["config"], @current="config",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/environments"=>#<Rails::Paths::Path:0x00000100b22090
@paths=["config/environments"], @current="config/environments",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="development.rb",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/initializers"=>#<Rails::Paths::Path:0x00000100b21bb8
@paths=["config/initializers"], @current="config/initializers",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="**/*.rb",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/locales"=>#<Rails::Paths::Path:0x00000100b212a8
@paths=["config/locales"], @current="config/locales",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="*.{rb,yml}",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/routes.rb"=>#<Rails::Paths::Path:0x00000100b20d58
@paths=["config/routes.rb"], @current="config/routes.rb",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "db"=>#<Rails::Paths::Path:0x00000100b20858
@paths=["db"], @current="db", @root=#<Rails::Paths::Root:0x00000100b1b6a0
...>, @glob=nil, @autoload_once=false, @eager_load=false,
@autoload=false, @load_path=false>,
"db/migrate"=>#<Rails::Paths::Path:0x00000100b20510
@paths=["db/migrate"], @current="db/migrate",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "db/seeds.rb"=>#<Rails::Paths::Path:0x00000100b20088
@paths=["db/seeds.rb"], @current="db/seeds.rb",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "vendor"=>#<Rails::Paths::Path:0x00000100b2bf78
@paths=["vendor"], @current="vendor",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=true>, "vendor/assets"=>#<Rails::Paths::Path:0x00000100b2b8e8
@paths=["vendor/assets"], @current="vendor/assets",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob="*",
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/database"=>#<Rails::Paths::Path:0x00000100b2b320
@paths=["config/database.yml"], @current="config/database",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"config/environment"=>#<Rails::Paths::Path:0x00000100b2add0
@paths=["config/environment.rb"], @current="config/environment",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "lib/templates"=>#<Rails::Paths::Path:0x00000100b2a1c8
@paths=["lib/templates"], @current="lib/templates",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "log"=>#<Rails::Paths::Path:0x00000100b29f70
@paths=["log/development.log"], @current="log",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "public"=>#<Rails::Paths::Path:0x00000100b299a8
@paths=["public"], @current="public",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"public/javascripts"=>#<Rails::Paths::Path:0x00000100b29390
@paths=["public/javascripts"], @current="public/javascripts",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>,
"public/stylesheets"=>#<Rails::Paths::Path:0x00000100b28af8
@paths=["public/stylesheets"], @current="public/stylesheets",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>, "tmp"=>#<Rails::Paths::Path:0x00000100b28418
@paths=["tmp"], @current="tmp",
@root=#<Rails::Paths::Root:0x00000100b1b6a0 ...>, @glob=nil,
@autoload_once=false, @eager_load=false, @autoload=false,
@load_path=false>}>, @autoload_paths=[],
@eager_load_paths=["/Users/7stud/rails_projects/test_postgres/app/assets",
"/Users/7stud/rails_projects/test_postgres/app/controllers",
"/Users/7stud/rails_projects/test_postgres/app/helpers",
"/Users/7stud/rails_projects/test_postgres/app/mailers",
"/Users/7stud/rails_projects/test_postgres/app/models",
"/Users/7stud/rails_projects/test_postgres/app/controllers/concerns",
"/Users/7stud/rails_projects/test_postgres/app/models/concerns"],
@autoload_once_paths=[], @cache_classes=false>
config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module TestPostgres
class Application < Rails::Application
# Settings in config/environments/* take precedence over those
specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record
auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
# The default locale is :en and all translations from
config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales',
'*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end

Reshape R: split a column

Reshape R: split a column

A really simple question but I could'nt find a solution: I have a
data.frame like
V1 <- c("A","A","B","B","C","C")
V2 <- c("D","D","E","E","F","F")
V3 <- c(10:15)
df <- data.frame(cbind(V1,V2,V3))
i.e.
V1 V2 V3
A D 10
A D 11
B E 12
B E 13
C F 14
C F 15
And I would like
V1 V2 V3.1 V3.2
A D 10 11
B E 12 13
C F 14 15
I try reshape{stats} and reshape2

How to get a record from two columns

How to get a record from two columns

I have a table name Vouchers in which i have three columns named
"GroupName","LedgerName" and "Amount".

I have another Table named Accounts in which i have two columns "Name" and
"Amount", now i want the column "Name" to have the the GroupName from the
Vouchers Table and then all the LedgerName that have same GroupName to
fall under that Record. The images explain my question asked above

Saturday, 7 September 2013

Unexpected namespace prefix "xmlns" found for tag fragment

Unexpected namespace prefix "xmlns" found for tag fragment

I'm getting an error:
Unexpected namespace prefix "xmlns" found for tag fragment
for lines
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
in
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.example.storeitemfinder.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />
</FrameLayout>
</TabHost>
Does anyone know what's wrong?

Combinging "icon" and "apple-touch-icon" link types

Combinging "icon" and "apple-touch-icon" link types

Mobile Safari requires the use of the following incantation for favicons
of a higher resolution than the traditional 16x16:
<link rel="shortcut icon" href="old-16x16-favicon.ico" />
<link rel="apple-touch-icon" sizes="158x158"
href="my-new-158x158-icon.png" />
However, Firefox requires the use of the HTML5 syntax, e.g.:
<link rel="shortcut icon" href="old-16x16-favicon.ico" />
<link rel="icon" type="image/png" sizes="158x158"
href="my-new-158x158-icon.png" />
Now, my expectation is that it should be possible to combine these into a
single line, as follows.
<link rel="shortcut icon" href="old-16x16-favicon.ico" />
<link rel="icon apple-touch-icon" type="image/png" sizes="158x158"
href="my-new-158x158-icon.png" />
Does anyone know of any problems with this? We all know things are rarely
this simple, and I don't have an iOS device to test against, let alone all
the other devices out there that have no-doubt copied the Apple syntax.

C++ constructor that deep copies a linked list

C++ constructor that deep copies a linked list

I am a noob to C++ and programming in general and am trying to make a
constructor which will duplicate a linked list. The idea is that I can use
Individual* copyOfList = new Individual(originalList->getFirstBit());
to make a deep copy of the original list.
But my cose below seems not to be doing a deep copy. When I edit the
copyOfList the originalList is efected aswell. And I don't understand
linked lists enough to make it deep copy. Can someone help me please.
Individual::Individual(BinaryNode* copyHead)
{
head = copyHead;
NodePtr last = NULL;
NodePtr temp = NULL;
curr = head;
while (curr != NULL)
{
temp = new BinaryNode(curr->data, NULL);
if(last != NULL)
{
last->next = temp;
}
last = temp;
if (head == NULL)
{
head = temp;
}
curr = curr->next;
}
}