use master
go

drop database simplestbroker
go
create database simplestbroker
go


use SimplestBroker
go

-- simple service broker messaging application
-- it is a door. Knocking on the down causes
-- a timestamp, indicationg when the door was
-- knocked on, to be inserted into the DoorLog
-- table

delete DoorLog
delete DoorSlammedShut
drop table doorlog
drop table doorslammedshut
-- create the door log
Create Table DoorLog
( 
id int identity,
conv uniqueidentifier,
occured datetime
)
go 


Create Table DoorSlammedShut
(
id int identity,
conv uniqueidentifier,
occured datetime
)
go 


delete DoorLog
go
-- create a sproc to be used as a service program
drop PROCEDURE KnockOnDoor

select * from sys.service_contracts

ALTER PROCEDURE KnockOnDoor
as
	declare @dialog uniqueidentifier
	declare @service_id uniqueidentifier
	--WAITFOR (GET SERVICE INSTANCE @service_id FROM CanvaserQueue)
	declare @m nvarchar(max)
	set @m = NCHAR(0xFEFF) + N'<root/>'
	BEGIN DIALOG @dialog
		FROM SERVICE Canvas
		TO SERVICE 'HomeOwner'
		ON CONTRACT DoorXML
	print 'knock knock'
	-- get things started
	--;SEND ON CONVERSATION @dialog MESSAGE TYPE SDoor (@m)
	;SEND ON CONVERSATION @dialog MESSAGE TYPE SlamDoor
	-- print 'sent'
	declare @conversation uniqueidentifier
	set @service_id = null

	print 'got svs'
	declare @mt nvarchar(256)
	declare @cont nvarchar(256)
	declare @svsname nvarchar(256)
	declare @body varbinary(max)
	WAITFOR (RECEIVE TOP(1)
		@svsname = service_name,
		@cont = service_contract_name, 
		@body = message_body,
		@mt = message_type_name,
		@conversation = conversation_handle FROM CanvaserQueue), TIMEOUT 1000
	--	WHERE service_instance_id = @service_id), TIMEOUT  -1
	print @svsname
	print @cont
	print @mt
	print convert(nvarchar(max), @body)
	print @@ROWCOUNT
	print 'dia ' + convert(varchar(max), @dialog)
	print 'conv ' +  convert(varchar(max), @conversation)
	print 'svs ' +  convert(varchar(max), @service_id)
	print 'rcv'
	Insert into DoorLog VALUES (@conversation, GETUTCDATE())
	print 'insert'
	END CONVERSATION @conversation 

go


drop PROCEDURE answerdoor
CREATE PROCEDURE AnswerDoor
as
	declare @service_id uniqueidentifier
	declare @conversation uniqueidentifier
	WAITFOR (GET SERVICE INSTANCE @service_id FROM KnockKnockQueue), TIMEOUT 10000
	
	;RECEIVE TOP(1) @conversation = conversation_handle FROM KnockKnockQueue
	  WHERE service_instance_id = @service_id
	;SEND ON CONVERSATION @conversation MESSAGE TYPE SlamDoor
	Insert into DoorSlammedShut VALUES (@conversation, GETUTCDATE())
	END CONVERSATION @conversation 

go



drop queue DoorKnocker
-- specify a queue that can be used 
-- to knock on

DROP QUEUE KnockKnockQueue
DROP QUEUE CanvaserQueue


CREATE QUEUE KnockKnockQueue
WITH STATUS = ON,
ACTIVATION
(
	STATUS=ON,
	PROCEDURE_NAME = AnswerDoor,
	MAX_QUEUE_READERS = 5,
	EXECUTE AS SELF
)


-- specify a Queue that can be
-- used by a Canvaser
CREATE QUEUE CanvaserQueue
WITH STATUS = ON


-- Create a message type you can use
-- to knock door

CREATE MESSAGE TYPE KNOCK
validation = EMPTY


CREATE MESSAGE TYPE SlamDoor
validation = EMPTY

CREATE MESSAGE TYPE SDoor validation = WELL_FORMED_XML

-- create a contract that
-- can be used by door knockers
DROP CONTRACT Door

CREATE CONTRACT Door
( KNOCK SENT BY INITIATOR,
  SlamDoor SENT BY TARGET )

drop contract Door2

CREATE CONTRACT Door2
( KNOCK SENT BY INITIATOR,
  SlamDoor SENT BY TARGET )

CREATE CONTRACT DoorXML
( SDoor SENT BY ANY,
 SlamDoor SENT BY ANY) 

CREATE CONTRACT [urn:Door2]
( KNOCK SENT BY INITIATOR,
  SlamDoor SENT BY TARGET )


CREATE CONTRACT [http://www.develop.com/Door2]
( KNOCK SENT BY INITIATOR,
  SlamDoor SENT BY TARGET )


-- create a service, i.e. a door
-- that can be knocked on
DROP SERVICE Canvas 
CREATE SERVICE Canvas 
ON QUEUE CanvaserQueue

DROP SERVICE HomeOwner
CREATE SERVICE HomeOwner
ON QUEUE KnockKnockQueue

exec dbo.KnockOnDoor
select * from sys.service_contracts




-- start a dialog between canver
-- an home owner


declare @dialog uniqueidentifier
BEGIN DIALOG CONVERSATION @dialog
FROM SERVICE Canvas
TO SERVICE 'HomeOwner'
ON CONTRACT Door
print @dialog
;SEND ON CONVERSATION @dialog MESSAGE TYPE SlamDoor


-- get things started
;SEND ON CONVERSATION @dialog MESSAGE TYPE KNOCK
go

select * from DoorLog
select * from DoorSlammedShut
select count(*) from DoorSlammedShut

delete DoorLog
delete DoorSlammedShut
