Contents

Mos Protocol Gateway With GOLANG

MOS (Media Object Server) Protocol: Technical Analysis and Implementation

Table of Contents

  1. Introduction
  2. Basic Structure of MOS Protocol
  3. MOS Protocol Implementation with Go
  4. Code Analysis and Examples
  5. Best Practices and Recommendations

1. Introduction

The MOS (Media Object Server) Protocol is a standardized protocol used for communication between systems in news broadcasting. This article examines the technical details of the protocol and its implementation using the Go programming language.

2. Basic Structure of MOS Protocol

MOS protocol uses an XML-based messaging system running over TCP/IP. The fundamental features of the protocol include:

  • Dual port structure (10540 and 10541)
  • XML format messaging
  • UCS-2 character encoding
  • Heartbeat mechanism
  • Acknowledgment (ACK) system

3. MOS Protocol Implementation with Go

3.1. Basic Configuration

Configuration structure used for protocol implementation:

1
2
3
4
5
6
7
type Config struct {
    MsgID             int    `json:"msgID"`
    MosID             string `json:"MosID"`
    NcsID             string `json:"NcsID"`
    MosServerIP       string `json:"MosServerIP"`
    RundownExportPath string `json:"RundownExportPath"`
}

3.2. Heartbeat Mechanism

The heartbeat function that monitors the continuity of connections between systems:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func HeartBeatEvent() {
    logFile := &lumberjack.Logger{
        Filename:   "Logs/HeratBeatEvent.log",
        MaxSize:    1,    // Rotate limit of 1MB
        MaxBackups: 250,  // Maximum number of log files
        MaxAge:     28,   // Log retention period (days)
        Compress:   true, // Gzip compression
    }
    defer logFile.Close()
    
    logger := log.New(logFile, "", log.LstdFlags)
    
    // Upper port connection
    conn, err := net.Dial("tcp", MosServerIP+":10541")
    if err != nil {
        fmt.Printf("Error connecting to Upperport %d: %v\n", Upperport, err)
        logger.Printf("Error connecting to Upperport %d: %v\n", Upperport, err)
        return
    }
    defer conn.Close()
    
    // Lower port connection and heartbeat loop implementation...
}

3.3. Rundown Operations

Basic structures used for rundown management in MOS protocol:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
type Story struct {
    XMLName             xml.Name               `xml:"story"`
    StoryID             string                 `xml:"storyID"`
    StorySlug           string                 `xml:"storySlug"`
    StoryNum            string                 `xml:"storyNum"`
    MosExternalMetadata []*MosExternalMetadata `xml:"mosExternalMetadata"`
    Item                []*Item                `xml:"item"`
}

type RoCreate struct {
    XMLName             xml.Name               `xml:"roCreate"`
    RoID                string                 `xml:"roID"`
    RoSlug              string                 `xml:"roSlug"`
    RoChannel           string                 `xml:"roChannel"`
    RoEdStart           string                 `xml:"roEdStart"`
    RoEdDur             string                 `xml:"roEdDur"`
    RoTrigger           string                 `xml:"roTrigger"`
    MacroIn             string                 `xml:"macroIn"`
    MacroOut            string                 `xml:"macroOut"`
    MosExternalMetadata []*MosExternalMetadata `xml:"mosExternalMetadata"`
    Story               []*Story               `xml:"story"`
}

3.4. Message Processing Mechanism

Main function used for processing incoming messages:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func checkData(data []byte) {
    var mos Structs.Mos
    err := xml.Unmarshal(data, &mos)
    if err != nil {
        fmt.Println("Error unmarshalling XML:", err.Error())
        logx("Error unmarshalling XML:" + err.Error())
        return
    }

    switch true {
    case mos.RoCreate != nil:
        RunID = mos.RoCreate.RoID
        RecordRundown(RunID, data, err)
        sendAck(RunID)
        break
    case mos.RoList != nil:
        RunID = mos.RoList.RoID
        RecordRundown(RunID, data, err)
        sendAck(RunID)
        break
    // Operations for other message types...
    }
}

4. Important Implementation Details

4.1. XML Processing

XML processing is crucial in the MOS protocol. Structures are defined using Go’s xml package:

1
2
3
4
5
6
7
8
9
type Mos struct {
    XMLName           xml.Name   `xml:"mos"`
    VersionAttr       string     `xml:"version,attr,omitempty"`
    ChangeDateAttr    string     `xml:"changeDate,attr,omitempty"`
    MosID            string     `xml:"mosID"`
    NcsID            string     `xml:"ncsID"`
    MessageID        int        `xml:"messageID"`
    // Other fields...
}

4.2. Logging System

Logging mechanism used for tracking system events:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func logx(string string) {
    logFile := &lumberjack.Logger{
        Filename:   "Logs/MosEvent.log",
        MaxSize:    1,    
        MaxBackups: 250,  
        MaxAge:     28,   
        Compress:   true, 
    }
    defer logFile.Close()
    logger := log.New(logFile, "", log.LstdFlags)
    logger.Println(string)
}

4.3. ACK (Acknowledgment) Mechanism

ACK messages indicating successful operations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
func sendAck(RunID string) {
    conn, err := net.Dial("tcp", MosServerIP+":10541")
    if err != nil {
        logx("Error connecting to Upperport: " + err.Error())
        return
    }
    defer conn.Close()

    var mos = &Structs.Mos{
        MosID:     MosID,
        NcsID:     NcsID,
        MessageID: msgID,
        RoAck: &Structs.RoAck{
            RoID:     RunID,
            RoStatus: "OK",
        },
    }
    // Creation and sending of ACK message...
}

5. Best Practices and Recommendations

5.1. Error Handling

  • Use timeout mechanisms for every network operation
  • Handle connection interruptions
  • Implement detailed logging

5.2. Performance Optimization

  • Optimize buffer sizes
  • Avoid unnecessary XML parsing
  • Implement connection pooling

5.3. Security Measures

  • Validate all incoming data
  • Consider SSL/TLS usage
  • Implement access control

6. Conclusion

The MOS protocol implementation has a complex but well-organized structure. Go’s powerful concurrency features and XML processing capabilities enable efficient implementation of the protocol. The code examples and explanations above demonstrate the fundamental building blocks and implementation details of the protocol.

For a successful MOS implementation:

  1. Thoroughly understand the protocol specification
  2. Establish a robust error handling mechanism
  3. Implement detailed logging
  4. Consider performance optimizations
  5. Plan security measures from the start

This approach enables the implementation of a reliable and high-performance MOS protocol system.

7. Technical Specifications

7.1. Protocol Details

  • Protocol Name: MOS (Media Object Server)
  • Transport Layer: TCP/IP
  • Ports: 10541 (Upper Port), 10540 (Lower Port)
  • Message Format: XML
  • Character Encoding: UCS-2

7.2. Key Components

  • Heartbeat System
  • Message Queue Management
  • Rundown Operations
  • Story Management
  • Item Processing
  • Metadata Handling

7.3. Message Types

  1. Basic Messages:
    • Heartbeat
    • ACK
    • Error
  2. Rundown Operations:
    • RoCreate
    • RoDelete
    • RoReplace
    • RoList
  3. Story Operations:
    • RoStoryAppend
    • RoStoryDelete
    • RoStoryMove
    • RoStorySwap

8. Implementation Considerations

8.1. System Architecture

The implementation should consider:

  • Scalability requirements
  • High availability needs
  • Network reliability
  • Data persistence
  • System monitoring

8.2. Development Guidelines

  • Follow Go best practices
  • Implement comprehensive error handling
  • Use proper logging mechanisms
  • Maintain clean code structure
  • Document all critical components

8.3. Testing Strategy

  • Unit testing for individual components

  • Integration testing for message flows

  • Load testing for performance validation

  • Error scenario testing

  • Network failure recovery testing

  • You can access the complete source code of the project from this link Github Mos Protocol gateway with Golang