-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 04 07 Add Traditional Bridge Host
The traditional Synergy side of your Traditional Bridge environment is almost complete. All that remains is to write the code for the Traditional Bridge host program.
The host program typically performs these steps:
- Opens and configures the terminal channel environment.
- Configures and initializes logging.
- Creates an instance of the master dispatcher class.
- Sends a "READY" message on STDOUT to tell Harmony Core that the host program is running.
- Instructs the master dispatcher class to start listening for and dispatching messages.
- Closes the log file when the master dispatcher exits.
- Gracefully deals with any unexpected exceptions.
The code that you will include in the program will manipulate classes from several namespaces, so we'll start by importing those namespaces to make the classes easier to access:
-
Open
TraditionalBridgeHost.dbl
and add the following code right at the top of the file, before themain
compiler directive:;;***************************************************************************** ;; ;; Title: TraditionalBridgeHost.dbl ;; ;; Description: A program to host a Harmony Core Traditional Bridge environment ;; that exposes traditional Synergy business logic to a Harmony ;; Core web service. ;; ;;***************************************************************************** import Harmony.TraditionalBridge import Json import TraditionalBridge import TraditionalBridge.Dispatchers
The code that you will be adding to the program will use several variables, so let's add them all now:
-
In the data division, add the following record:
record ttChan, i4 ;;Terminal channel number length, int ;;Length of a string tmpa, a10 ;;Temporary alpha variable logLevel, int ;;Logging level. logFileName, string ;;Log file name dispatcher, @MethodDispatcher ;;Message dispatcher instance endrecord
If any code in this environment fails, we want to trap and log the exception, and exit gracefully. So we'll wrap everything in one big TRY/CATCH statement:
-
Add the following code to the procedure division:
try begin end catch(ex, @Exception) begin end endtry
Harmony Core will send us JSON-RPC request messages on STDIN, and expects us to respond by sending JSON-RPC response messages on STDOUT. In traditional Synergy, these are both represented by a terminal channel
. We need to open the terminal, and configure the environment:
-
Add the following code in the TRY block of the TRY/CATCH statement:
;;Configure the environment and open the terminal chanel. ;; - STDIN is the mechanism by which Harmony Core sends us requests ;; - STDOUT is the mechanism by which we will issue responses xcall flags(1907050020) open(ttChan=0,o,"TT:")
Although it is possible to debug both sides of a Traditional Bridge environment using Visual Studio, logging is always a valuable debugging tool, particularly in client/server environments. Traditional Bridge incorporates a comprehensive logging subsystem that is configurable to operate at six levels. Normal logging is considered to be level 2, and full verbose logging is achieved at level 6.
-
Add the following code, in the TRY body, below the code you just added:
;;Does the environment define the logging level? getlog("HARMONY_LOG_LEVEL", tmpa, length) if (length) then begin logLevel = %integer(tmpa) end else begin ;Levels are 1 to 6, 2 is normal logging, 6 is highest logging logLevel = 2 end ;;Define the log file name logFileName = "BRIDGE_" + %string(%jbno) + ".LOG" ;;Initiate logging Logger.Instance = new Logger(logFileName, logLevel, false)
You're almost there, it's time to create an instance of the master dispatcher class and let it loose to do its work.
-
Add the following code, in the TRY body, below the logging code you just added:
;;Create a new instance of the main "dispatcher" class dispatcher = new MethodDispatcher() ;;Issue the "READY" message. ;;Harmony Core looks for this and considers the connection active when it sees it puts(ttChan, "READY" + %char(13)+ %char(10)) ;;Start dispatching requests dispatcher.Dispatch(ttChan)
As you can see, this is where the action happens. We create an instance of the
MethodDispatcher
class, we tell Harmony Core that we're ready to go, and we hand control over to theDispatch
method.From this point, we're out of the picture. The Traditional Bridge library code will take care of receiving requests, dispatching to routines, and issuing responses.
If anything goes wrong we should deal with it gracefully. That's what the CATCH block of the TRY/CATCH statement is for:
-
Add the following code to the
CATCH
block:;;If anything failed, log an error if we can if (Logger.Instance != ^null) begin Logger.Instance.Log("DISPATCHER EXITED with exception" + ex.ToString()) end
There are two scenarios that can cause the program to terminate:
- The master dispatcher decides to exit from the
Dispatch()
method. - An exception is trapped and logged.
In both cases, we should close the log file and terminate the application.
-
Add this code after the
TRY/CATCH
block, before theendmain
compiler directive:;;Close the log if (Logger.Instance != ^null) begin Logger.Instance.CloseLog() end stop
Before we move on, let's make sure the project builds:
-
Right-click on the
TraditionalBridge
project and selectBuild
. -
Check the
Output
window and verify that the build was successful.1>------ Build started: Project: TraditionalBridge, Configuration: Debug x64 ------ ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Let's also make sure the project runs. We can't actually make any requests right now, but we can make sure the host program starts, initializes, and reports that it is ready:
- Right-click on the
TraditionalBridge
project and selectDebug > Start New Instance
.
You should see a DBR process start, and display the READY
message:
Next topic: Add Helper Service Class
-
Tutorial 2: Building a Service from Scratch
- Creating a Basic Solution
- Enabling OData Support
- Configuring Self Hosting
- Entity Collection Endpoints
- API Documentation
- Single Entity Endpoints
- OData Query Support
- Alternate Key Endpoints
- Expanding Relations
- Postman Tests
- Supporting CRUD Operations
- Adding a Primary Key Factory
- Adding Create Endpoints
- Adding Upsert Endpoints
- Adding Patch Endpoints
- Adding Delete Endpoints
-
Harmony Core Code Generator
-
OData Aware Tools
-
Advanced Topics
- CLI Tool Customization
- Adapters
- API Versioning
- Authentication
- Authorization
- Collection Counts
- Customization File
- Custom Field Types
- Custom File Specs
- Custom Properties
- Customizing Generated Code
- Deploying to Linux
- Dynamic Call Protocol
- Environment Variables
- Field Security
- File I/O
- Improving AppSettings Processing
- Logging
- Optimistic Concurrency
- Multi-Tenancy
- Publishing in IIS
- Repeatable Unit Tests
- Stored Procedure Routing
- Suppressing OData Metadata
- Traditional Bridge
- Unit Testing
- EF Core Optimization
- Updating a Harmony Core Solution
- Updating to 3.1.90
- Creating a new Release
-
Background Information